$(document).ready(function() {

	$(document).bind("contextmenu",function(e){
		return false;
	});
	
	if($("#left > h3:first").text() == 'Featured Manga'){
		$("#topnav > li > a").addClass("active")
	} else {
		var activeLink = $("#left > h3:first").text();
		$("#topnav > li > a:contains('"+activeLink.toLowerCase()+"')").addClass("active");
	}

	// Top navigation
	$(".subtopnav").css("opacity",0.5);

	$("#topnav > li").hover(function(){
		$("ul", this).animate({opacity: "show"}, "normal");
	}, function() {
		$("ul", this).animate({opacity: "hide"}, "fast");
	});
	

	// Image hovers
	$("img:not('.viewimage')").css("opacity",0.85);

	$("img:not('.viewimage')").hover(function(){
		$(this).fadeTo("fast", 1);
	}, function() {
		$(this).fadeTo("normal", 0.85);
	});

	// Welcome box
	$(".welcometext").css("opacity",0.8);


	// Gallery description toggles
	$("span.toggle").click(function(){
		$(this).next("span.description").toggle("fast");
	});
	$("span.toggle").toggle(function(){
		$(this).html("&uarr;Hide");
	}, function(){
		$(this).html("Show&darr;");
	});

	// Gallery comments
	$(".comments").bind("click",function(e){popwindow(e)});

	// Gallery view popup
	$(".viewimage").click(function(e){
		e.preventDefault();
 		var file = $(this).attr("href");
		var art_id = $(this).attr("id");
 		$(".displayimage").html('<img src="'+file+'" alt="'+file+'" />');
 		var imageWidth = $("span:first", this).attr("class");
 		var imageHeight = $("span:last", this).attr("class");
		var yPosition = e.pageY - e.clientY;
 		centerPopup(imageWidth, yPosition);
 		loadPopup(imageWidth, imageHeight);
 		$.ajax({
  			type: "POST",
 			url: "/increment_count.php",
			data: "item_id="+art_id+"&type=gallery",
			success: function(count){
				$(".viewimage[href='"+file+"']").text("View("+count+")");
			}
		});
	});
		
	$(".artPopupClose").click(function(){
		disablePopup();
	});
	$("#popupBG").click(function(){
		disablePopup();
	});
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});
	
	
	// View/download graphics
	$(".viewfile").click(function(e){
		var id = $(this).attr("id");
 		var type = $("span", this).attr("class");
 		if(type=="addedmerch"){
	 		e.preventDefault();
		}
		increment_count(id, type);
	});
	
	$(".modifyqty").click(function(e){
	 	e.preventDefault();
		var id = $(this).attr("id");
 		var type = $("span", this).attr("class");
		$.ajax({
			type: "POST",
			url: "/increment_count.php",
			data: "item_id="+id+"&type="+type,
			success: function(count){
				$(".modifyqty[id='"+id+"']").prev("span.modifiedqty").text(count);
			}
		});
	});
	
	
	
	/*	$(".addmerch").click(function(e){
	 	e.preventDefault();
		var id = $(this).attr("id");
		increment_count(id, "addedmerch");
//	 	var addedmerch = $("span", this).text();
//	 	$("span", this).text(id);
	 	
	 
	 	$.ajax({
		type: "POST",
		url: "/increment_count.php",
		data: "item_id="+id+"&type="+type,
		success: function(count){
			$(".viewfile[id='"+id+"']").find("span[class='"+type+"']").text(count);
		}
		});
	});*/

});


// Comments popup function
function popwindow(e){
	e.preventDefault();
	var href=$(e.target).attr("href");
	var windowWidth = document.documentElement.clientWidth;
	window.open(href,"","menubar=no,location=no,toolbar=no,scrollbars=yes,top=100,left="
	+parseInt(windowWidth/2-450/2)+",width=520,height=580");
}


// Gallery view popup function
var popupStatus = 0;
function loadPopup(width, height){
	if(popupStatus==0){
		$("#popupBG").css({"opacity": "0.6"});
		$("#artPopup").css({"width": width+"px", "height": height+"px"});
		$("#popupBG").fadeIn("normal");
		$("#artPopup").fadeIn("normal");
		popupStatus = 1;
	}
}

function disablePopup(){
	if(popupStatus==1){
		$("#popupBG").fadeOut("normal");
		$("#artPopup").fadeOut("normal");
		popupStatus = 0;
		$(".displayimage").attr({src: ""});
	}
}

function centerPopup(width, top){
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	$("#artPopup").css({"position": "absolute", "left": windowWidth/2-width/2, "top": top});
	$("#popupBG").css({"height": windowHeight});
}

// Increment count for file clicks
function increment_count(id, type){
	$.ajax({
		type: "POST",
		url: "/increment_count.php",
		data: "item_id="+id+"&type="+type,
		success: function(count){
			$(".viewfile[id='"+id+"']").find("span[class='"+type+"']").text(count);
		}
	});
}