var helper_size_timeout;
var helper_info_timeout;
var helper_info_exit_timeout;
var helper_id = '';
var size_helper = 0;
if($.browser.msie){helper_fade = 0;}

if($.browser.msie){
	fadeOut = 0;
}else{
	fadeOut = 100;
}

$(document).ready(function ()
{
	//2Columns Layout fix
	fixColumnHeight();
	
	//Smooth Scroll
	$('a[href*=#]').click(function(e) {
		e.preventDefault();
		var target = $(this).attr('href').replace("#","");
		$('html, body').animate({
			scrollTop: $("*[name="+target+"]").offset().top
		}, 700);
	});	
	
	$("a[rel='colorbox']").colorbox({
		scalePhotos: true,
		maxWidth: "100%",
		maxHeight: "100%"
	});	

});	

/*Size Helper*/
$(document).ready(function()
{
	$("#search_field div").mouseenter(function()
	{
		clearTimeout(helper_size_timeout);
		$("#size_helper").removeClass("nopie");
		$("#size_helper").fadeIn(400);		
	});
	
	$("#search_field div").mouseleave(function(){
		helper_size_timeout = setTimeout(function()
		{
			$("#size_helper").addClass("nopie");
			$("#size_helper").fadeOut(fadeOut);
		},200);
	});
	
	$("#size_helper").mouseenter(function()
	{
		clearTimeout(helper_size_timeout);
	});	
	
	$("#size_helper").mouseleave(function()
	{
		helper_size_timeout = setTimeout(function()
		{
			$("#size_helper").addClass("nopie");
			$("#size_helper").fadeOut(fadeOut);
		},200);
	});	
});

$(document).ready(function()
{
	$("#size_helper img").click(function()
	{
		var size = ""+$(this).attr("data-size");
		tempSize(size);
	});

	$("#size_helper p.size_title").click(function()
	{
		var size = ""+$(this).attr("data-size");
		tempSize(size);
	});	
	
	$("#size_helper span").click(function()
	{
		var size = ""+$(this).attr("data-size");
		tempSize(size);
	});	
	
	function tempSize(size)
	{
		createCookie('bb_ratio', 0, 0);
		createCookie('bb_size',	 0, 0);
		
		if(size=='all'){
			//All images
			size = 'Any size';
		}else if(size.search(':')!=-1){
			//Ratio
			createCookie('bb_ratio',size,30);
		}else if(size.search('x')!=-1){
			//Resolution
			createCookie('bb_size',size,30);
		}	
		$("#search_field div").html(size);
		$("#size_helper").fadeOut(fadeOut);
		
		if($("input[name=search]").val()!=''){
			setTimeout(function() { window.location.replace(ROOT+"search"+SEARCH_SEP+$("input[name=search]").val()+"/") }, 50);
		}
	}
});



/*Info Helper*/
$(document).ready(function()
{
	$("#results .thumb img.popup").mouseenter(function()
	{
		//GET Image Data
		var id		= ''+$(this).attr("data-id");
		var res 	= ''+$(this).attr("data-res");
		var site	= ''+$(this).attr("data-site");
		var tags	= ''+$(this).attr("data-tags");	
	
		//Timeout
		clearTimeout(helper_info_timeout);
		if(helper_id == id){
			clearTimeout(helper_info_exit_timeout);
		}
	
		//GET Position
		var position = $(this).position();
		var top		= position.top+$(this).outerHeight(true)+10;
		var left	= position.left-130-10+($(this).outerWidth(true)/2);
		if($(window).width() < 1100)
		{
			if(left < 0){left = 0;}
			else if(left + $("#info_helper").outerWidth() > $(".inner").width()){left = $(".inner").width() - $("#info_helper").outerWidth();}
		}
		
		if(res == "undefined"){res = '?';}
		if(site == "undefined"){site = '?';}
		if(tags == "undefined"){tags = 'tagme';}
		
		var tags_html = '';
		var toks = tags.split(",");

		for(i=0; i< toks.length; i++){
			tag = trim(toks[i]);
			
			tag_temp = tag;
			tag_temp = tag_temp.replace(/\(\[\(/g,"<span>");
			tag_temp = tag_temp.replace(/\)\]\)/g,"</span>");
			tag_temp = tag_temp.replace(/<\/span> <span>/g," ");
			tag_temp = trim(tag_temp);
			
			tag = tag.replace(/\(\[\(/g,"");
			tag = tag.replace(/\)\]\)/g,"");
			tag = tag.replace(/[^a-zA-Z 0-9'-]+/g,' ');
			tag = tag.replace(/[ ]+/g,' ');
			tag = trim(tag);
			tag = tag.replace(/ /g,'+');
			
			tags_html += '<a href="'+ROOT+'search'+SEARCH_SEP+tag+'/">'+tag_temp+'</a> ';
		}		
		
		helper_info_timeout = setTimeout(function()
		{		
			//SET Position
			$("#info_helper").css("top", top);
			$("#info_helper").css("left", left);				
		
			//SET Image Data
			$("#info_helper #info_res").html(res);
			$("#info_helper #info_site").html(site);
			$("#info_helper #info_tags").html(tags_html);		
			helper_id = id;
			
			$("#info_helper").removeClass("nopie");
			$("#info_helper").fadeIn(200);	
			
		},500);	
	});
	
	$("#results .thumb img").mouseleave(function()
	{
		clearTimeout(helper_info_timeout);
		helper_info_exit_timeout = setTimeout(function(){
			$("#info_helper").addClass("nopie");
			$("#info_helper").fadeOut(fadeOut);
			helper_id = '';
		},250);
	});	
	
	$("#info_helper").mouseenter(function(){
		clearTimeout(helper_info_exit_timeout);
	});		
	
	$("#info_helper").mouseleave(function(){
		helper_info_exit_timeout = setTimeout(function(){
			$("#info_helper").addClass("nopie");
			$("#info_helper").fadeOut(fadeOut);
			helper_id = '';
		},250);
	});
	
	$("#results .thumb a").click(function(){
		clearTimeout(helper_info_timeout);
		$("#info_helper").hide();
	});
		
	
});

/*Settings*/
$(document).ready(function()
{
	$("#boards td.checkbox input").change(function(){
		var boards = '';
		$("#boards td.checkbox input:checked").each(function(){
			boards += $(this).val()+',';
		});
		createCookie('bb_boards', boards, 30);
	});

	$("select[name=settings_nImages]").change(function(){
		createCookie('bb_nImages', $(this).val(),30);
	});
	
	$("input[name=settings_dedupe]").change(function(){
		createCookie('bb_dedupe', $(this).is(':checked') ? '1' : '0',30);
	});	
	
	$("input[name=settings_popup]").change(function(){
		createCookie('bb_popup', $(this).is(':checked') ? '1' : '0',30);
	});		
	
	$("textarea[name=settings_blacklist]").keyup(function(){
		var blacklist = trim(removeNL($(this).val()));
		createCookie('bb_blacklist', blacklist,30);
	});	
	
});


function fixColumnHeight()
{
	var h1 = $("#body .sx").height();
	var h2 = $("#body .dx").height();

	if(h1 > h2){
		$("#body .dx").height(h1);
	}else{
		$("#body .sx").height(h2);
	}
}

jQuery.fn.center = function ()
{
    this.css("position","absolute");
    this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
    return this;
}

function trim(str){
	return str.replace(/^\s+|\s+$/g,"");
} 

function removeNL(s){ 
  return s.replace(/[\n\r\t]/g,""); 
}


/*===Cookie===*/
function createCookie(name,value,days)
{
	if(days){
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++){
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
