/**
 * 
 */


$(document).ready(function ()
{
	
	/*$('#filter').submit(function() {
		
	});*/
	$('#orden_by, #country').change(function(){
		$.fn.launchFilter();
	});
	$('.categories_sel').click(function(e){
		$("#category_temp").val($(this).prop("rel"));
		$.fn.launchFilter();
		return false;
	});
	$('.link_follow').live("click",function(e){
		var that= eval('(' + $(this).prop("rel") + ')');
		var selLink = $(this);
		var url= CI_ROOT+'collection/following/'+that.id+"/"+that.state;
		var a= ($("#f-"+that.id).html()).split(" ");
		var number = parseInt(a[0]);
		$("#f-"+that.id).html("<img src='"+CI_ROOT+"images/ajax-loader.gif'/>");

		selLink.html("<img src='"+CI_ROOT+"images/ajax-loader.gif'/>");
		$.getJSON(url, function(data) {
			if(data.isRegister){
				if(data.code){
					selLink.text(data.textLink);
					selLink.prop("rel" , "{'id':"+that.id+", 'state': '"+data.state+"'}");
					
					var newText =(data.state==0)?(number+1)+" "+a[1]:(number-1)+" "+a[1];
					$("#f-"+that.id).html(newText);
				}else{
					//Error raro
					window.open(CI_ROOT+'login/index/collection/index',"_self");
					//window.open(CI_ROOT+'login/index/gallery/image/'+$('#id_item').val()+'/12/'+$('#id_image').val()+'/1',"_self");
				}
			}else{
				//llevar a pagina principal;
				//alert("Sin registro");
				window.open(CI_ROOT+'login/index/collection/index',"_self");
			}
			
		});
		return false;
	});
	
	
	//Detail collection
	
	
	
	
	
	
	
	
	
	
	$.fn.launchFilter = function() {
		
		var text = $("#search_text").val();
		var category=$("#category_temp").val();
		var from=$("#from_temp").val();
		var page=0;
		var order = $("#orden_by").val();
		var country= $("#country").val();
		var type = $("#typeList").val();
		if(type <= 1){
			window.open(CI_ROOT+'collection/search/'+page+'/'+order+'/'+country+'/'+category+'/?query='+text,"_self");
		}else if(type == 2){
			window.open(CI_ROOT+'collection/all/'+page+'/'+order+'/'+country+'/'+category,"_self");
		}else if(type == 3){
			window.open(CI_ROOT+'collection/my_collections/'+page+'/'+order+'/'+country+'/'+category,"_self");
		}else if(type == 4){
			window.open(CI_ROOT+'collection/from/'+from+'/'+page+'/'+order+'/'+country+'/'+category,"_self");
		}else if(type == 5){
			window.open(CI_ROOT+'collection/follow/'+page+'/'+order+'/'+country+'/'+category,"_self");
		} else if(type == 6){
			window.open(CI_ROOT+'collection/follower/'+from+'/'+page+'/'+order+'/'+country+'/'+category,"_self");
		} 

	};
	$.fn.followCollection = function(link) {
		//alert(link.prop('href'));
		/**/
	};

});
