

//Mmmmm cookies....
function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((domain) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}


/*
 * Tooltip script 
 * powered by jQuery (http://www.jquery.com)
 * 
 * written by Alen Grakalic (http://cssglobe.com)
 * 
 * for more info visit http://cssglobe.com/post/1695/easiest-tooltip-and-image-preview-using-jquery
 *
 */
 


this.tooltip = function(){	
	/* CONFIG */		
		xOffset = 20;
		yOffset = 20;		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result		
	/* END CONFIG */		
	$("a.tooltip").hover(function(e){											  
		this.t = this.title;
		this.title = "";									  
		$("body").append("<p id='tooltip'>"+ this.t +"</p>");
		$("#tooltip")
			.css("top",($(this).offset().top + xOffset) + "px")
			.css("left",($(this).offset().left + yOffset) + "px")
			.fadeIn("fast");		
    },
	function(){
		this.title = this.t;		
		$("#tooltip").remove();
    });	
	/*$("a.tooltip").mousemove(function(e){
		$("#tooltip")
			.css("top",($(this).offset().top + xOffset) + "px")
			.css("left",($(this).offset().left + yOffset) + "px");
			
	});	*/
	//.css("top",(e.pageY - xOffset) + "px")
	//.css("left",(e.pageX + yOffset) + "px");		
};


this.stockicontip = function(){	/* Tooltip for stock icons, reduce pagesize and select status here */
	/* CONFIG */		
		xOffset = 20;
		yOffset = 20;	
		var storetypes = {'OP': 'Osborne Park / Online',
						  'MY': 'Myaree'};
		var qtytypes = {'A1': '20+ in stock',
								'A2': '10-20 in stock',
								'A3': '6-10 in stock',
								'A4': '2-5 in stock',
								'A5': 'Only 1 left, be quick',
								'OO': 'No stock available, more on the way',
								'UA': 'No stock available, Call for ETA',
								'NA': 'No stock available, Call for ETA'};	
	/* END CONFIG */		
	$("a.stockicon").hover(function(e){											  
		//this.t = this.title + "";
		var status = $(this).attr('status');
		var store = storetypes[status.substr(0,2)];
		var qty = qtytypes[status.substr(2,2)];
		if (typeof store != "undefined" && typeof qty != "undefined") {
			this.t = store + '<br>' + qty;
												  
			$("body").append("<p id='tooltip'>"+ this.t +"</p>");
			$("#tooltip")
				.css("top",(e.pageY - xOffset) + "px")
				.css("left",(e.pageX + yOffset) + "px")
				.fadeIn("fast");	
		}	
    },
	function(){ //guessing this is on hoverout
		//this.title = this.t;		
		$("#tooltip").remove();
    });	
	$("a.stockicon").mousemove(function(e){
		$("#tooltip")
			.css("top",($(this).offset().top + xOffset) + "px")
			.css("left",($(this).offset().left + yOffset) + "px");
	});			
};

this.loginpopup = function(){
	$('#loginbutton').click(function(){
		var position = $('#loginbutton').offset();
		position['top'] += 20; 
		position['left'] -= 50; 
		$('#loginpopup').css(position);
		$('#loginpopup').fadeToggle(50, "linear");
		$('#loginname').focus();
		return false;
	});
	//$('#loginbutton').mouseover(function(){};
	$('#loginpopupclose').click(function(){
		$('#loginpopup').fadeToggle(50, "linear");
		return false;
	});

};


this.zoomimage = function(){	
	
	var popupsrcstart = "<div id='zoomimagepopup' onclick='$(\"#zoomimagepopup\").remove();'>\
	<table height=100% width=100% border=0><tr><td valign=center align=center><center>\
	<a onclick='$(\"#zoomimagepopup\").remove();'><img src='"
	var popupsrcend = "'><br><B>Click anywhere on this popup close.</b></a></td></tr></table></div>"
	$('.zoomimage').click(function(){
		if ($(this).height() == parseInt($(this).css("max-height")) || $(this).width() == parseInt($(this).css("max-width"))) {
			if ($('#backtotoplink').is(":visible")) {$('#backtotoplink').hide();};
			$('#zoomimagepopup').remove();
			//$("body").append("<div id='zoomimagepopup' onclick='$(\"#zoomimagepopup\").remove();'><a onclick='$(\"#zoomimagepopup\").remove();'><img src='" + $(this).attr('src') + "'><br><B>Click anywhere on this popup close.</b></a></div>");
			$("body").append(popupsrcstart + $(this).attr('src') + popupsrcend);
			$("#zoomimagepopup").fadeIn("fast");	
			
		}
	});
	$(".zoomimage").hover(function(e){	
		if ($(this).height() == parseInt($(this).css("max-height")) || $(this).width() == parseInt($(this).css("max-width"))) {
			//<img src='../images/products-icon-zoomimage.png'><									  			  
			$("body").append("<div id='tooltip'>Click to view larger image</div>");
			$("#tooltip")
				.css("top",($(this).offset().top + $(this).height()) + "px")
				.css("left",($(this).offset().left) + "px")
				.show();	
			$(this).css("box-shadow", "0px 0px 4px #BBBBBB");
		}
    },
	function(){
		$("#tooltip").remove();
		$(this).css("box-shadow", "none");
    });	
    $(document).scroll(function(){
   		$("#zoomimagepopup").remove()
   		});

};

this.showPopUpWarning = function(text){
	$('#popupwarning').remove();
	$("body").append("<div id='popupwarning'>"+ text +"</div>");
	$("#popupwarning")
		.fadeIn("fast")
		.delay(5000)
		.fadeOut(5000);	
}


// starting the script on page load
$(document).ready(function(){
	tooltip();
	zoomimage();
	stockicontip();
	loginpopup();
	var processingimages = false;
	var maxtop = 0;
	var mintop = 0;
	
	
	//Adjust product listing for small displays
	if($(window).width() < 1100){
		var w = $(window).width();
		
		setCookie("smallScreen", "1");

		//document.cookie = "smallscreen=1;path=/testing/";
		//if(w < 640){w = 640};
		//$('.productlist').css('width', w-240 + 'px');
		//$('.productlistcontainer').css('width', w-240 + 'px');
		//$('#featureTable .about').css('width', (w-240)*0.45 + 'px');
		//$('#featureTable .news').css('width', (w-240)*0.55 + 'px');
		//$('.featItemMain').css('width', (w-240)/3 + 'px');
		//$('#productMain').css('width', (w-240) + 'px');
		//$('.productsalesspec').css('width', (w-240) + 'px');
		//$('.productAboveLink').css('width', (w-240) + 'px');	
	} else {
	setCookie("smallScreen", "0");
	}
	if($(window).width() < 900){
		$('#onlinequote .item select').css('width', w-500 + 'px');
		$('#onlinequote .item').css('width', w-500 + 'px');
	}
	

	$(document).scroll(function(){
		if ($(document).scrollTop() > 1000) { 
			if (!$('#backtotoplink').is(":visible")) {$('#backtotoplink').fadeIn('fast');};
		} else {
			if ($('#backtotoplink').is(":visible")) {$('#backtotoplink').fadeOut('fast');};
		}
		if (!processingimages){ //Make sure not still running
			processingimages = true;
			maxtop = $(document).scrollTop() + 1000;
			mintop = $(document).scrollTop() + -500; //load images sooner when scrolling up
			$(".productlist").each( function() { //one .productlist per product category subgroup
				if ($(this).offset()['top'] > mintop && $(this).offset()['top'] < maxtop && !($(this).attr('imagesloaded') == '1')) { //is it visible?
					$(this).attr('imagesloaded', '1'); //Tag the group as done
					$("#"+$(this).attr('id')+" .img img").each( function() { //iterate though all images under this product table
				  		if (!($(this).attr('_src') == '')) {
				  			$(this).attr('src', $(this).attr('_src')); //swap in the image source
				  			$(this).attr('_src', '');
				  		}
					});
					
				}
			});
			
			/*$(".img img").each( function() {
		  		//restore the src
		  		if ($(this).offset()['top'] > mintop && $(this).offset()['top'] < maxtop && !($(this).attr('_src') == '')) {
		  			$(this).attr('src', $(this).attr('_src'));
		  			$(this).attr('_src', '')
		  		}
			});*/
			
			
			processingimages = false;
		}
	});

	//Change the source of all the images to point to the loading image
    /*$(".img img").each( function() {
      //Write the original source to a temporary location
      $(this).attr('_src', $(this).attr('src'));
      //Change the image source to the loading image
      $(this).attr('src', "images/blank.gif");
   });*/
});



