jQuery.noConflict();

jQuery(document).ready(function() {
	jQuery(".collapse").click(function () {
		jQuery("#adminmenu").animate({left: "-150px"}, {duration: 800});
		jQuery(".collapse").fadeOut();
	});

	jQuery(".expand").click(function () {
		jQuery("#adminmenu").animate({left: "0px"}, {duration: 800});
		jQuery(".collapse").fadeIn();
	});

	// Rounded Corners
	jQuery(".subpage").corner("round 14px");
	jQuery(".subpage img").corner("round 10px");
	jQuery(".productitem").corner("round 7px");
	jQuery("#contactright").corner("round 10px");
	jQuery("#popupContent").corner("round 10px");
	jQuery(".pagegallery").corner("round 10px");
	jQuery("blockquote").corner("round 7px");
	jQuery("#facebook a").corner("round 6px left");

	///////////////////////////
	// PRODUCT POPUP DISPLAY //
	///////////////////////////

	///////////////////////////
	//
	// FEATURES LIST
	//
	// 1. Popup repositions when window is resized
	// 2. Popup has minimum margin of 20px to keep darkness around it
	// 3. If popup is bigger than window when opened, the popup will resize to fit
	// 4. Can be closed by pressing the ESC key
	//
	///////////////////////////

	// 0 = Deactivated and 1 = Activated
	var popupStatus = 0;

	function activatePopup(productId) {
		// GET DATA TO POSITION POPUP
		var winWidth	= jQuery(window).width();
		var winHeight	= jQuery(window).height();
		var popWidth	= jQuery("#popupContent").width();
		var popHeight	= jQuery("#popupContent").height();

		// CALCULATE POPUP POSITION
		var topMargin	= winHeight/2-popHeight/2;
		var leftMargin	= winWidth/2-popWidth/2;

		jQuery("#popupContent").css({
			"position": "fixed",
			"top": winHeight/2-popHeight/2,
			"left": winWidth/2-popWidth/2
		});

		// SETUP MINIMUM MARGIN OF 20PX AROUND POPUP
		if (topMargin < 20) {
			jQuery("#popupContent").css({ "top": "20px" });
		}
		if (leftMargin < 20) {
			jQuery("#popupContent").css({ "left": "20px" });
		}

		// IF POPUP BIGGER THAN WINDOW, SET MAX SIZE TO FIT
		if (winHeight < popHeight) {
			jQuery("#popupContent").css({ "max-height": winHeight-70 });
		}
		if (winWidth < popWidth) {
			jQuery("#popupContent").css({ "max-width": winWidth-70 });
		}

		// ACTIVATE POPUP
		if (popupStatus==0) {
			jQuery("#popupBackground").css({"opacity": "0.7"});
			jQuery("#popupBackground").fadeIn("slow");
			jQuery("#popupContent").fadeIn("slow");
			popupStatus = 1;
			jQuery.get("http://mowersgalore.com.au/product_display.php", "id="+productId, function (data) {
				jQuery("#popupContent").html(data);
			});
		}
	};

	function deactivatePopup() {
		if (popupStatus==1) {
			jQuery("#popupBackground").fadeOut("slow");
			jQuery("#popupContent").fadeOut("slow");
			popupStatus = 0;
		}
	};

	jQuery(window).resize(function() {
		// GET INFO TO DETERMINE POPUPS NEW POSITION WHEN WINDOW IS RESIZED
		var newWidth	= jQuery(window).width();
		var newHeight	= jQuery(window).height();
		var popWidth	= jQuery("#popupContent").width();
		var popHeight	= jQuery("#popupContent").height();

		// CALCULATE POPUP POSITION
		var topMargin	= newHeight/2-popHeight/2;
		var leftMargin	= newWidth/2-popWidth/2;

		// IF POPUP BIGGER THAN WINDOW, SET MAX SIZE TO FIT
		jQuery("#popupContent").css({ "max-height": newHeight-70 });
		jQuery("#popupContent").css({ "max-width": newWidth-70 });

		jQuery("#popupContent").css({
			"position": "absolute",
			"top": newHeight/2-popHeight/2,
			"left": newWidth/2-popWidth/2
		});

		// SETUP MINIMUM MARGIN OF 20PX AROUND POPUP
		if (topMargin < 20) {
			jQuery("#popupContent").css({ "top": "20px" });
		}
		if (leftMargin < 20) {
			jQuery("#popupContent").css({ "left": "20px" });
		}
	});

	jQuery(".openProduct, .productimage, .producttitle").click(function() {
		var productId = jQuery(this).attr('id');
		activatePopup(productId);
	});

	jQuery("#popupBackground").click(function() {
		deactivatePopup();
	});

	jQuery(document).keypress(function(e) {
		// Close popup if Esc key is pressed
		if (e.keyCode==27 && popupStatus==1) {
			deactivatePopup();
		}
	});


	/////////////////////////////
	// PRODUCT IMAGE SWITCHING //
	/////////////////////////////

	function changeImage(largePath) {
	//	jQuery('#popupContent').animate({ scrollTop: jQuery('.productdisplaylarge').offset().top }, 500);
		jQuery('.productLarge').fadeOut('slow');
		jQuery('.productLarge').queue(function() { jQuery(this).attr( 'src', largePath ).dequeue(); });
		jQuery('.productLarge').queue(function() { jQuery(this).attr( 'src', largePath ).dequeue(); });
		jQuery('.productLarge').fadeIn('slow');
	}

	// If a thumbnail is clicked
	jQuery('.productThumb').live('click', function() {
		var largePath = jQuery(this).attr('id');
		imgPre = new Image();
		imgPre.src = largePath;
		changeImage(largePath);
	return false;
	});
});

