fancybox_options = {
	overlayShow:true,
	overlayColor:"000000",
	overlayOpacity:0.8,
	padding:0,
	titlePosition:'over',
	showCloseButton:false,
	scrolling:"no",
	onComplete:function() {
		$(".rollover").rollover();
	}
};
var searchParam = {};
currentSubmission = 0;
$(function () {
	$('img.fadein').load(function() {
    	$(this).fadeIn();
	}); 
	$(".rollover").rollover();
	$("#caption").maxLength(480);
	$(".fancybox").fancybox(fancybox_options);
//	$(window).scroll(function () { 
//		$("#help, #contact").slideUp();
//	});	
	
	$("#help_button").click(function() {
		$("#contact").slideUp({duration: 1000, easing:"easeInOutCirc"});
		if($("#help").is(":hidden")) {
			$("#help").slideDown({duration: 1000, easing:"easeInOutCirc"});
			$("#arrow_left,#arrow_right").fadeOut({duration: 1000, easing:"easeInOutCirc"});
		} else {
			$("#help").slideUp({duration: 1000, easing:"easeInOutCirc"});
			$("#arrow_left,#arrow_right").fadeIn({duration: 1000, easing:"easeInOutCirc"});
		}		
	});
	
	$("#contact_button").click(function() {
		$("#help").slideUp({duration: 1000, easing:"easeInOutCirc"});
		if($("#contact").is(":hidden")) {
			$("#contact").slideDown({duration: 1000, easing:"easeInOutCirc"});
			$("#arrow_left,#arrow_right").fadeOut({duration: 1000, easing:"easeInOutCirc"});
		} else {
			$("#contact").slideUp({duration: 1000, easing:"easeInOutCirc"});
			$("#arrow_left,#arrow_right").fadeIn({duration: 1000, easing:"easeInOutCirc"});
		}
	});
	
	if($("#searchResult")) {
		if(section != null && section != "") {
			searchParam = {section_id:section,random:"",notid:id};
			search(searchParam);
			$("#recent_btn").click(function() {
				$('#search_box').val("");
				searchParam = {section_id:section,notid:id};
				search(searchParam);
			});
			$('#search_box').keyup(function(e) {
				if(e.keyCode == 13) {
					searchParam = {section_id:section,keyword:$(this).val(),random:"",notid:id};
					search(searchParam);
				}
			});
			$("#random_btn").click(function() {
				$('#search_box').val("");
				searchParam = {section_id:section,random:"",notid:id};
				search(searchParam);
			});
		}
	}
	
	$("#arrow_left").click(function() { scrollSubmissions("previous"); }).hover(function() {
		$(this).fadeTo("normal", 1);
	},function() {
		$(this).fadeTo("normal", 0.3);
	});
  	$("#arrow_right").click(function() { scrollSubmissions("next"); }).hover(function() {
		$(this).fadeTo("normal", 1);
	},function() {
		$(this).fadeTo("normal", 0.3);
	});
	$("<img>").attr("src", "assets/medias/loading.gif");
});

function closeHelpContact() {
	$("#contact").slideUp({duration: 1000, easing:"easeInOutCirc"});
	$("#help").slideUp({duration: 1000, easing:"easeInOutCirc"});	
	$("#arrow_left,#arrow_right").fadeIn({duration: 1000, easing:"easeInOutCirc"});
}

function loading() {
	$.fancybox({content:'<div style="width:140px;height:60px;background:#1e1e1e;"><img style="margin-top:10px;" src="assets/medias/loading.gif" width="120" height="40" alt="Loading" /></div>',modal:true,overlayColor:"#1e1e1e",overlayOpacity:0.8})
}

function scrollSubmissions(delta) {
	maxSubmissions = $(".submission").length-1;
	if(maxSubmissions < 0) maxSubmissions = 0;
	currentSubmission += (delta == "next") ? 1 : -1 ;
	if(currentSubmission < 0) {
		currentSubmission = maxSubmissions;
	} else if (currentSubmission > maxSubmissions) {
		currentSubmission = 0;
	}
	$('#submissions').animate({"marginLeft":"-"+(currentSubmission*915)+"px"}, 1500, "easeInOutExpo");  
}

function searchLast(param) {
	var newParam = $.extend(searchParam, param);
	search(newParam);
}

function search(param) {
	var newParam = $.extend({section_id:section}, param);
	$.post(host+"search.php", param, function(data) {
		$("#searchResult").html(data);
		$(".fancybox").fancybox(fancybox_options);
		$(".search_result img").hover(function() {
			$(this).fadeTo("fast", 1);
		}, function() {
			$(this).fadeTo("fast", 0.5);
		});
		$('img.fadein').load(function() {
	        $(this).fadeIn();
	    });
	});
}

function textPage(param) {
	$.post(host+"submission_viewer.php", param, function(data) {
		$("#text").html(data);
	});
}

function love(id, el, suffix) {
	$.post(host+"love.php", {id:id}, function(data) {
		if(data == "love") $(el).find("img").attr("src", host+"assets/medias/heart_on"+suffix+".png");
		if(data == "hate") $(el).find("img").attr("src", host+"assets/medias/heart_off"+suffix+".png");
	});
}

function checkRows(textArea){
	if (navigator.appName.indexOf("Microsoft Internet Explorer") == 0) {
		textArea.style.overflow = 'visible';
		return;
	}
	while (textArea.rows > 1 && textArea.scrollHeight < textArea.offsetHeight){
		textArea.rows--;
	}
	while (textArea.scrollHeight > textArea.offsetHeight){
		textArea.rows++;
	}
	return;
}

jQuery(function() {
	emptyForm();
	changePoundLink();
});

function emptyForm() {	
	jQuery('input[type="text"], textarea').bind("focus", function() {
		if(jQuery(this).val() == this.defaultValue) jQuery(this).val("");
	});
	jQuery('input[type="text"], textarea').bind("blur", function() {
		if(jQuery(this).val() == "") jQuery(this).val(this.defaultValue);
	});
}
	
function changePoundLink() {
	jQuery("a[href='#']").attr("href", "javascript:void(0);");
}

function resetForm(formId) {
	jQuery("#"+formId+" :input").each(function(i, el) {
		if(el.defaultValue) {
			jQuery(el).val(el.defaultValue);
		} else {
			jQuery(el).val("");
		}
	});
	jQuery("#"+formId+" :checkbox").val([]);
	jQuery("#"+formId+" :radio").val([]);
}
/*
function print_r(theObj){
	ret = "";
	if(theObj.constructor == Array || theObj.constructor == Object){
		for(var p in theObj){
			ret += "\n";
			if(theObj[p].constructor == Array || theObj[p].constructor == Object){
				ret += "["+p+"] => "+typeof(theObj);
				ret += "    "+print_r(theObj[p]);
			} else {
				ret += "["+p+"] => "+theObj[p];
			}
		}
	}
}
*/

(function($){  
	$.fn.extend({   
		rollover: function() {   
			return this.each(function(i, el) {  
				if(el.nodeName == "SPAN") {
					if($(el).data("fileName") == undefined) {
						var tmp = el.css("filter").split("'")[1];
						$(el).data("extension", tmp.pop());
						$(el).data("fileName", tmp.join("."));
					}
					$(el).hover(
						function() { $(this).css("filter", "progid:DXImageTransform.Microsoft.AlphaImageLoader"+"(src=\'"+$(el).data("fileName")+"_hover."+$(el).data("extension")+"\', sizingMethod='scale')");	},
						function() { $(this).css("filter", "progid:DXImageTransform.Microsoft.AlphaImageLoader"+"(src=\'"+$(el).data("fileName")+"."+$(el).data("extension")+"\', sizingMethod='scale')"); }
					);
				} else {
					if($(el).data("fileName") == undefined) {
						var tmp = el.src.split(".");
						$(el).data("extension", tmp.pop());
						$(el).data("fileName", tmp.join("."));
					}
					$(el).hover(
						function() { $(this).attr("src", $(this).data("fileName")+"_hover."+$(this).data("extension")); },
						function() { $(this).attr("src", $(this).data("fileName")+"."+$(this).data("extension")); }
					);
				} 
			}); 			
		},
		maxLength: function(length, nextInput) {
			return this.each(function(i, el) {  
				$(el).keyup(function() {
					if($(this).val().length >= length) $(this).val( $(this).val().substring(0, length) );
					if(nextInput != null) if($(this).val().length >= length) $(nextInput).focus();
				});
			});
		}
	});       
})(jQuery);
