var main_width;
var picMargin = 22;
var $images;
$(document).ready(function() {
	$images = $("div#images>a>img");
						   
	$("#saveLink").click(function() { 
		$("#form_saveleech").toggle(); 
		$("#sl_Description").focus();
	});
	$("#button_saveLeech").click(function() { saveLeech(); return false; });
	
	$("#leech_search").click(function() {
		searchLeech();
		return false;
	});
	
	main_width = $(window).width() - 20;
	$("#images img").load(function() {
		checkImgSize($(this));
	});
	
	$("input[type=text]").focus(function() {
		$(this).addClass("focus");
	}).blur(function() {
		$(this).removeClass("focus");
	});
	
	$("#leechLink").focus(function() {
		if($(this).val() == "http://") $(this).val("");				   
	}).blur(function() {
		if($(this).val() == "") $(this).val("http://");
	});
	
	$("#form_leech").submit(function() {
		var leechLink = $("#leechLink").val();
		var match = /^https?:\/\//.test(leechLink)
		if(!match) {
			searchLeech();
			return false;
		}							 
	});
	
	$("#leechLink").focus();
	
	$("div.a a").attr("target", "_blank");
	$("div.a").hover(function() {$(this).addClass("hover");}, function() {$(this).removeClass("hover");});
	
	$images.load(function(){
		checkImgSize($(this));
	});
	
	$("#toggle_size").toggle(function() {oriImages();}, function() {fitImages();});
	
});//End of ready

window.onresize = function() {
	main_width = $(window).width() - 20;
	$("div#images>a>img").each(function() {
		checkImgSize($(this));		
	});
}

window.onload = function() {
	$("div#images>a>img").each(function() {
		checkImgSize($(this));
		/*if (!IsImageOk(this)) {
		  $(this).parent().remove().next().remove();
		}*/
	});
}

function IsImageOk(img) {
    if (!img.complete) {
        return false;
    }

    if (typeof img.naturalWidth != "undefined" && img.naturalWidth == 0) {
        return false;
    }
	
    return true;
}


function checkImgSize(img) {
	img.removeAttr("width");
	if(($(img).width() + picMargin) > main_width) {
		$(img).attr("width", main_width - picMargin);
		//$(img).toggle(function() { $(img).removeAttr("width"); }, function() { $(img).attr("width", main_width - picMargin); });
	}	
}

function saveLeech() {
	var sCategory = $("#sl_Category").val();
	var sDescription = $("#sl_Description").val();

	$.ajax({
		type: "POST",
		url: "/php/scripts/saveLeechAjax2.php",
		data:  { 
			sl_LeechLink: sLeechLink,
			sl_LeechType: sLeechType,
			sl_Description: sDescription,
			sl_Category: sCategory
		},
		beforeSend: function() {
			$("#form_saveleech input, #form_saveleech select").attr("disabled","disabled");
		},
		success: function(msg){
			$("#save_info").html(msg);
			$("#form_saveleech input").attr("disabled","disabled");
		},
		error: function(msg){
			$("#form_saveleech input, #form_saveleech select").removeAttr("disabled");
		},
		complete: function() {
			//
		}
	});	
}

function searchLeech() {
	var sKeywords = $("#leechLink").val();

	$.ajax({
		type: "POST",
		url: "/php/service/searchService.php",
		data:  { 
			q: sKeywords
		},
		beforeSend: function() {
			
		},
		success: function(msg){
			$("#search_results").html(msg);
		},
		error: function(msg){

		},
		complete: function() {
			//
		}
	});	
}


function oriImages() {
	$("#images img").each(function() {
		$(this).removeAttr("width");
	});
}

function fitImages() {
	main_width = $(window).width() - 20;
	$("#images img").each(function() {
		fitImage(this);
	});
}

function fitImage(img) {
	if(($(img).width() + picMargin) > main_width) {
		$(img).attr("width", main_width - picMargin);
		//$(img).toggle(function() { $(img).removeAttr("width"); }, function() { $(img).attr("width", main_width - picMargin); });
	}
}
