$(document).ready(function (){
	
	
	try{
		var id_image = $('#nav-image').prop('class').split('/'),
		$loader = '<img id="follow-loader" alt="" src="' + CI_ROOT + 'images/ajax-loader-2.gif" />';
		$('#comments').load(CI_ROOT + 'collection/show_comments/' +  id_image[1] + '/3' + '/' + new Date().getTime());
	}catch(s){
		
	}
	
        $(".imgCole").click(function(){
            $.fn.nextImage();
		return false;
        });
        
	$('#image-content').mouseover(function() { $('#nav-image .ni-next').removeClass('ni-next').addClass('ni-next-active'); });
	
	$('#image-content').mouseout(function() { $('#nav-image .ni-next-active').removeClass('ni-next-active').addClass('ni-next'); });
	
	
	
	
	$('#edit-description').live('click', function() 
	{
		var txt = $('pre.description').text();
		
		$('#tab-readOnly').hide();
		$('#tab-edit').show();
		if(!($('pre.description').prop('title') === 'nn'))
			$('textarea#to-description').val(txt);
		else
			$('textarea#to-description').val('');
	});
	
	$('#save-edit').live('click', function() 
	{
		var description = $('textarea#to-description').val(),
			id_image = $('pre.description').prop('id');
		
		loader = $('<div class="layer-stopEditTab">').prependTo('#container-description');
		
		$.post(CI_ROOT + 'collection/save_description/' + id_image,
			{description: description}, 
			function (e)
			{
				if(e=="nologin"){
					window.open(CI_ROOT+'login/index/collection/image/'+$('#xco').val()+'/name/'+$('#x3m').val()+'/1',"_self");
					
				}else{
					$('#tab-readOnly').html(loader).load(CI_ROOT + 'collection/refresh_description/' + id_image + '/' + new Date().getTime());
					$('#tab-readOnly').show();
					$('#tab-edit').hide();
					$('div.layer-stopEditTab').detach();
				}
				
		    });
	    return false;
	});
	
	$('#cancel-edit').live('click', function() 
	{
		$('#tab-readOnly').show();
		$('#tab-edit').hide();
		
		return false;
	});
	
	
	
	
	
	
	
	
	
	
	
	
	$('.ni-next').live('click', function() 
	{
		$.fn.nextImage();
		return false;
	});
	$('.ni-prev').live('click', function()
	{
		$.fn.prevImage();
		return false;
	});
	
	$('.ni-erase').bind('click', function() {
		$('#confirm-images-delete').modal();
	});
	
	$('#deleteImage').bind('click', function() 
	{
		$.modal.close();
		$.fn.deleteImage();
		return false;
	});
	$('.ni-right').bind('click', function() 
	{
		$.fn.rotateRigth();
		return false;
	});
	$('.ni-left').bind('click', function() 
	{
		
		$.fn.rotateLeft();
		return false;
	});
	$('.ni-main').bind('click', function() 
	{
		
		$.fn.mainImage();
		return false;
	});
	
	$.fn.deleteImage = function(){
		
		
		var id_image = $('#x3m').val();
		
		var id_collection = $('#xco').val();
		
		$.post(CI_ROOT + 'collection/delete_image/'+id_collection+'/'+id_image,
			function(ev)
			{
				
				if(ev=="nologin"){
					window.open(CI_ROOT+'login/index/collection/image/'+$('#xco').val()+'/name/'+$('#x3m').val()+'/1',"_self");
				}else if(ev=="not"){
						alert("Error inesperado");
				}else if(ev=="is_main"){
					
					$('#images-delete-principal').modal();
				}else{
					$.fn.nextImage();
				}
			}
		);
	};
	
	
	$.fn.mainImage =function(){
		var broken = $('#nav-image').prop('class').split('/');
		$temp = $('#image-content').html();
		$('#image-content').prepend('<div class="layer-stopNavImage">&nbsp;</div>');
		var id_collection = broken[0],id_image = broken[1];
		$.post(CI_ROOT + 'collection/change_main_image', {id_collection: id_collection, id_image: id_image}, 
		function(data)
		{
			$('#image-content').html($temp);
			if(data.isRegister){
				if(data.isMain){
					$("#main-info-alert").modal();
				}else{
					if(data.state){
						$("#main-ok-alert").modal();
					}else{
						$("#main-error-alert").modal();
					}
					
				}
			}
			else{
				window.location.href = CI_ROOT + 'login/index/collection/image/' + id_collection + '/name/' + broken[1] + '/' + broken[2]+'/' + new Date().getTime();
			}
		},"json");
	};
	
	$.fn.prevImage =function(){
		var broken = $('#nav-image').prop('class').split('/'),
		id_collection = broken[0],
		collection_name = "name",//$('#image-content').find('img').prop('alt'),
		id_image = broken[1],
		number = broken[2],
		image_name = $('.product-image > img').prop('id');

		//$('#image-content').prepend('<div class="layer-stopNavImage">&nbsp;</div>');
		$.post(CI_ROOT + 'collection/image_event/prev', {id_collection: id_collection, id_image: id_image, image_name: image_name, number: number}, 
			function(image)
			{
				data = image.split('|');
				number = data[0].split('/');
				$('#nav-image').prop('class', data[0]);
				$('#image-content').html(data[1]+"?"+Math.random()+"="+new Date().getTime());
				$('#image-content').find('img').prop('alt', collection_name);
				$('#title-pic span').html(number[2]);
				$('#tab-readOnly').load(CI_ROOT + 'gallery/refresh_description/' + number[1] + '/' + new Date().getTime());
				$('#comments').load(CI_ROOT + 'gallery/show_comments/' + number[1] + '/3' + '/' + new Date().getTime());
				window.location.href = CI_ROOT + 'collection/image/' + id_collection + '/' + collection_name + '/' + number[1] + '/' + number[2]+'/' + new Date().getTime();
			}
		);
	};
	
	$.fn.nextImage = function() {
		
		var broken = $('#nav-image').prop('class').split('/'),
			id_collection = broken[0],
			collection_name = "name",//escape($('#image-content').find('img').prop('alt')),
			id_image = broken[1],
			number = broken[2],
			image_name = $('.product-image > img').prop('id')?$('.product-image > img').prop('id'):"";
		
		$.post(CI_ROOT + 'collection/image_event/next', {id_collection: id_collection, id_image: id_image, image_name: image_name, number: number, date: new Date().getTime()}, 
			function(image)
			{
				
				data = image.split('|');
				number = data[0].split('/');
				$('#nav-image').prop('class', data[0]);
				$('#image-content').html(data[1]+"?"+Math.random()+"="+new Date().getTime());
				$('#image-content').find('img').prop('alt', collection_name);
				$('#title-pic span').html(number[2]);
				$('#tab-readOnly').load(CI_ROOT + 'gallery/refresh_description/' + number[1] + '/' + new Date().getTime());
				$('#comments').load(CI_ROOT + 'gallery/show_comments/' + number[1] + '/3' + '/' + new Date().getTime());
				window.location.href = CI_ROOT + 'collection/image/' + id_collection + '/' + collection_name + '/' + number[1] + '/' + number[2]+'/' + new Date().getTime(); 
			}
		);
		return false;
	};
	
	$.fn.rotateRigth = function(){
		var image_name = $('.product-image  > div').find('img').prop('id'),
		item_name = $('#image-content').find('img').prop('alt');
	
		$('#image-content').prepend('<div class="layer-stopNavImage">&nbsp;</div>');
		$.post(CI_ROOT + 'collection/rotate/-1/' + $('#nav-image').prop('class') + '/' + image_name, {}, 
			function(image)
			{
				if(image=="nologin"){
					window.open(CI_ROOT+'login/index/collection/image/'+$('#xco').val()+'/name/'+$('#x3m').val()+'/1',"_self");
				}else{
					//alert(image);
					data = image.split('|');
					$('#nav-image').prop('class', data[0]);
					$('#image-content').html('<div>'+data[1]+'</div>');
					$('#image-content').find('img').prop('alt', item_name);
				}
				
			}
		);
	};
	
	$.fn.rotateLeft = function(){
		var image_name = $('.product-image > div').find('img').prop('id'),
		item_name = $('#image-content').find('img').prop('alt');
	
	
		$('#image-content').prepend('<div class="layer-stopNavImage">&nbsp;</div>');
		
		$.post(CI_ROOT + 'collection/rotate/1/' + $('#nav-image').prop('class') + '/' + image_name, {}, 
			function(image)
			{
				if(image=="nologin"){
					window.open(CI_ROOT+'login/index/collection/image/'+$('#xco').val()+'/name/'+$('#x3m').val()+'/1',"_self");
					
				}else{
					
					data = image.split('|');
					$('#nav-image').prop('class', data[0]);
					$('#image-content').html('<div>'+data[1]+'</div>');
					$('#image-content').find('img').prop('alt', item_name);
				}
				
			}
		);
	};
	
	
});
