/**
 * Define some variables
 */
//var siteURL = 'http://localhost/zippix';
var siteURL = 'http://www.zippix.co.uk';
var salmonLoader = '<img src="'+siteURL+'/images/salmon-loading.gif" alt="" name="Please be patient" />';
var refnum = 0;

$(document).ready(function() {	
	/**
	 * Define some variables
	 */
	//var siteURL = 'http://localhost/zippix';
	var siteURL = 'http://www.zippix.co.uk';
	var salmonLoader = '<img src="'+siteURL+'/images/salmon-loading.gif" alt="" name="Please be patient" />';
	
	// Photos popup menu when mouseover
	if($('#photos')) {
		$('#photos').hover(function() {
			$('#photos-menu').css('display','block');
			$('#photos').css('cursor','pointer');
			$('#photos').addClass("itemRO");
		},function() {
			$('#photos').removeClass("itemRO");
			
			$('#photos-menu').css('display','none');		
		});
	}
	
	var whilecount = 0;
	
	// For photo sub items
	while(whilecount<10) { //whileloop == true
		whilecount++;
		
		if($('#location'+whilecount)) { 
			$('#location'+whilecount).hover(function() {
				$(this).addClass("menu-subitemRO");
			},function() {
				$(this).removeClass("menu-subitemRO");
			});
		}
		
		if($('#special'+whilecount)) { 
			$('#special'+whilecount).hover(function() {
				$(this).addClass("menu-subitemRO");
			},function() {
				$(this).removeClass("menu-subitemRO");
			});
		}
	}
	
	// Tooltips for images
	$('.stockImage').tooltip({
		delay: 0,
		track: true,
		showURL: false,
		showBody: "::",
		extraClass: "tooltip",
		fade: 600 
	});
	
	// Searchbox...clears itself when user clicks it
	$('#searchbox,#searchbox-home').click(function() {
		$(this).attr("value","");
		$(this).css("color","#000000");
	});
	
	// Rollovers for the 'Add to cart' and 'Add to wishlist' for x_zippix.php
	$('.addTo').hover(function() {
		$(this).addClass("addToRO");
	},function() {
		$(this).removeClass("addToRO");
	});
	
	// Click events for the 'Add to cart' and 'Add to wishlist' for x_zippix.php
	$('.addTo').click(function() {
		var str = $(this).attr('id').split("-");
		var action = str[0];
		var refnum = str[1];
		
		switch(action) {
			case 'alightbox': addToLightbox(refnum, 'alightbox-'+refnum,'x_zippix'); break;
			case 'login': openwindow(this,'please-login'); break;
			case 'rlightbox': removeFromLightbox(refnum,'rlightbox-'+refnum,'x_zippix'); break;
			case 'acart': selectSize(this,refnum); break;
			case 'rcart': removeFromCart(refnum,'rcart-'+refnum,''); break;
		}
	});
	
	/**
	 * Button Rollovers underneath images on results pages
	 */
	$('.addToBox').hover(function() {
		$(this).addClass("addToBoxRO");
	},function() {
		$(this).removeClass("addToBoxRO");
	});
	
	/**
	 * Button Events for when clicking adding to box
	 */
	$('.addToBox').click(function() {
		var str = $(this).attr('id').split("-");
		var action = str[1];
		
		$('.closeoverlay').click();
		
		switch(action) {
			case "small": addToCart(refnum,'acart-'+refnum,'','S'); break;
			case "large": addToCart(refnum,'acart-'+refnum,'','L'); break;
		}
	});
	
	$('.wishlistcell').click(function() {
		var str = $(this).attr('id').split("-");
		var action = str[0];
		var refnum = str[1];
		
		switch(action) {
			case "alightbox": addToLightbox(refnum, 'alightbox-'+refnum,'infopage'); break;
			case "rlightbox": removeFromLightbox(refnum, 'rlightbox-'+refnum,'infopage'); break;
		}
	});
	
	$('.buycell').click(function() {
		var str = $(this).attr('id').split("-");
		var action = str[0];
		var refnum = str[1];
		
		switch(action) {
			case "alightbox": addToLightbox(refnum, 'alightbox-'+refnum,'infopage'); break;
			case "rlightbox": removeFromLightbox(refnum, 'rlightbox-'+refnum,'infopage'); break;
		}
	});
	
	$('.dload-link').click(function() {
		var str = $(this).attr('id').split("-");
		var refnum = str[1];
		var size = str[2];
		
		var filename = refnum+".jpg";
		
		var i = $('#dloaddiv-'+refnum);
		
		var h = i.outerHeight();
		var w = i.outerWidth();
		var t = i.position().top;
		var l = i.position().left;
		
		var element = '<div id="loadingobj'+refnum+'" class="loadingobj" align="center"><img src="'+siteURL+'/images/black-loading.gif" alt="LOADING..." /><br /><br />Now uploading your image to the server in order for you to download the image, this may take upto 10 minutes for a large image...please be patient.<br /><br />If a problem occurs, please email <a href="mailto:help@zippix.co.uk">help@zippix.co.uk</a></div>';

		$('#dloaddiv-'+refnum).append(element);
		
		$('#loadingobj'+refnum).css({
			'opacity'		: '0.9',
			'position'		: 'absolute',
			'height'		: h,
			'width'			: w,
			'top'			: t,
			'left'			: l
		});
		
		$.post(siteURL+'/shop/DownloadNow.php', { 'size' : size, 'filename' : filename },
		function(data){
			$('#loadingobj'+refnum).css("display","none");
			$('#dloadcontent-'+refnum).html(data);
		});
	});
	
	addXEvents('cart');
	addXEvents('lightbox');
});

/************************************************************
 *
 *
 * Functions
 *
 *
 ************************************************************/

/**
 * Function for selecting a size when adding to cart
 * @params
 * @object : object clicked on
 * @id : refnum of item
 */
function selectSize(object,id) {
	refnum = id;
	openwindow(object,'cart-sizes');
}


/**
 * Function for refreshing the cart/lightbox
 * @params
 * @object : cart or lightbox
 */
function refreshObject(object) {
	$.post(siteURL+'/general_includes/x_'+object+'.php', {  },
	function(data){
		$('#'+object+'-content').empty();
		$('#'+object+'-content').html(data);
		addXEvents(object);
	});
}

/**
 * Function for showing a loading box and symbol
 * @params
 * @object : cart or lightbox
 */
function showLoading(object) {
	// White transparent overlay with loading symbol
	var h = $("#"+object+'-container').outerHeight();
	var w = $("#"+object+'-container').outerWidth();
	var t = $("#"+object+'-container').position().top;
	var l = $("#"+object+'-container').position().left;
	
	var toppad = (h-8)/2;
	h -= toppad;

	var element = '<div id="loadingobj" align="center"><img src="'+siteURL+'/images/black-loading.gif" alt="LOADING..." /></div>';
	
	$("#"+object+'-content').append(element);
	
	$('#loadingobj').css({
		'opacity'		: '0.8',
		'height'		: h,
		'width'			: w,
		'top'			: t,
		'left'			: l,
		'padding-top'	: toppad
	});
}

/**
 * Function for opening a window
 * @params
 * @object : object clicked on, for positioning the window 
 * @id : this targets the id in general_includes/x_overlay.php
 */
function openwindow(object,id) {			
	$('#window-underlay').css("display","block");
	
	$('#window-underlay').click(function() {
		$("#"+id).css("display","none");
		$('#window-underlay').css("display","none");
	});
	
	//alert($("#"+object.id).position().top);
	//alert($("#"+object.id).position().left);
	
	$("#"+id).css({
		'top': ($(object).position().top+10)+"px",
		'left': ($(object).position().left+10)+"px",
		'display': "block"		  
	});
	 
	$('.closeoverlay').hover(function() {
		$(this).css("cursor","pointer");
	});
	
	$('.closeoverlay').click(function() {
		$("#"+id).css("display","none");
		$('#window-underlay').css("display","none");
	});
}

/**
 * Function for adding events to the X button in the lightbox & cart
 * @params
 * @object : lightbox || cart 
 */
function addXEvents(object) {
	$('.'+object+'-x').hover(function() {
		$(this).css("cursor","pointer");
	});
	
	$('.'+object+'-x').click(function() {
		var str = $(this).attr('id').split("-");
		var refnum = str[2];
		
		if($('#thispageis').length) {
			param2 = 'rlightbox-'+refnum;
			page = "infopage";
		} else {
			param2 = null;
			page = null;
		}
		
		// Converts cart to Cart
		var callfunc = eval('removeFrom'+object.substr(0,1).toUpperCase()+object.substr(1));
		callfunc(refnum,param2,page);
	});
	
	$('.'+object+'-item').hover(function() {
		$(this).css("cursor","pointer");
	});
}

/**
 * Function for selecting a size when adding to cart
 * @params
 * @id : refnum of item
 * @size : S or L (for Small or Large)
 * @object : HTML element
 */
function addToCart(id,object,page,size) {	
	if(object==undefined) {
		object = 'acart-'+id;
	}
	
	var HTMLobject = $("#"+object);
	var str = object.split("-");
	var action = str[0];
	
	var request = salmonLoader+'&nbsp;Adding to cart...';
	var newID = 'rcart-'+id;
	
	switch(page) {
		case "infopage": 
			//alert('infopage!!');
		break;
		default:
			var complete = '<span class="normal">Remove from </span><span class="salmon">Cart</span>';
		break;
	}
	
	HTMLobject.html(request);
			
	// Show the cart is loading
	showLoading('cart');
	
	$.post('http://www.zippix.co.uk/shop/control/shopcontrol.php', { 'dowhat' : 'ADD', 'refnum' : id, 'qty' : 1, 'size' : size, 'ajax' : 'yes' },
	function(data){
		HTMLobject.attr('id',newID);
		HTMLobject.html(complete);
			
		// Update cart display
		refreshObject('cart');
	});
}

/**
 * Function for removing from cart
 * @params
 * @id : refnum of item
 */
function removeFromCart(id,object,page) {		
	if(object==undefined) {
		object = 'rcart-'+id;
	}
	var HTMLobject = $("#"+object);
	var str = object.split("-");
	var action = str[0];
	
	var request = salmonLoader+'&nbsp;Removing from cart...';
	var newID = 'acart-'+id;
	
	switch(page) {
		case "infopage": 
			
		break;
		default:
			var complete = '<span class="normal">Add to </span><span class="salmon">Cart</span>';
		break;
	}
	
	HTMLobject.html(request);
			
	// Show the cart is loading
	showLoading('cart');
	
	$.post(siteURL+'/shop/control/H_checkorder.php', { 'dowhat' : 'REMOVE', 'refnum' : id },
	function(data){
		HTMLobject.attr('id',newID);
		HTMLobject.html(complete);
			
		// Update cart display
		refreshObject('cart');
	});
}

/**
 * Function for adding to Lightbox
 * @params
 * @id : refnum of item to add to Lightbox
 */
function addToLightbox(id,object,page) {
	var HTMLobject = $("#"+object);
	var str = object.split("-");
	var action = str[0];
	
	var request = salmonLoader+'&nbsp;Adding to Lightbox...';
	var newID = 'rlightbox-'+id;
	
	switch(page) {
		case "infopage": 
			var complete = '<img class="LightboxBtns" src="'+siteURL+'/images/buttons/remLightbox.gif" width="81" height="22" border="0" alt="- Lightbox" />';
		break;
		default: 
			var complete = '<span class="normal">Remove from </span><span class="salmon">Lightbox</span>';
		break;
	}
	
	HTMLobject.html(request);
			
	// Show the lightbox is loading
	showLoading('lightbox');
	
	$.post(siteURL+'/shop/control/H_2005_wishlist_add.php', { 'refnum' : id },
	function(data){
		HTMLobject.attr('id',newID);
		HTMLobject.html(complete);
			
		// Update cart display
		refreshObject('lightbox');
	});
}

/**
 * Function for removing from Lightbox
 * @params
 * @id : refnum of item to remove from Lightbox
 */
function removeFromLightbox(id,object,page) {
	if(object==undefined) {
		object = 'rlightbox-'+id;
	}
	
	var HTMLobject = $("#"+object);
	var str = object.split("-");
	var action = str[0];
	
	var request = salmonLoader+'&nbsp;Removing From Lightbox...';
	var newID = 'alightbox-'+id;
	
	switch(page) {
		case "infopage": 
			var complete = '<img class="LightboxBtns" src="'+siteURL+'/images/buttons/addLightbox.gif" width="81" height="22" border="0" alt="+ Lightbox" />';
		break;
		default:
			var complete = '<span class="normal">Add to </span><span class="salmon">Lightbox</span>';
		break;
	}
	
	HTMLobject.html(request);
			
	// Show the lightbox is loading
	showLoading('lightbox');
	
	$.post(siteURL+'/shop/control/H_2005_wishlist_rem.php', { 'refnum' : id },
	function(data){
		HTMLobject.attr('id',newID);
		HTMLobject.html(complete);
			
		// Update cart display
		refreshObject('lightbox');
	});
	
}

function limitchars(id,maxchars) {
	var l = maxchars - $('#'+id).attr('value').length;
	$('#'+id+'-chars').html(l);
	
	$('#'+id).keydown( function(e) {
		var l = maxchars - this.value.length;
		
		if(l <= 0 && e.which != 8) {
			return false;
		}
	});
	
	$('#'+id).keyup( function() {
		var l = maxchars - this.value.length;
		$('#'+id+'-chars').html(l); 
	});
}