var sitesection;

$(document).ready(function(){

	//make the sub-nav animate and slide down when loading
	//$(".list-subnav").slideDown(500);

	//remove javascriptonly (hidden)
	removejavascriptonly();

	//create a zoom image for the product images
	setzoom();
	//setzoom2();
	
	//attach the zoom function to a change of product image
	$(".product-thumb").bind("click", function(e){
	  setTimeout("setzoom()", 100)
    });
	
	//create an image rotator on the home page
	initiaterotator();
	initiateslider();

	//expand/collapse the basket contents in the shopping cart
	showhidebasket();
	
	//where did i use this?
	//setmodal();

	//capture the context menu
	contextmenu();

	//load the promos for the current page
	loadpromos(sitesection);

	//apply the png fix for ie6
	pngFix();


});


//remove the javascriptonly class. allows us to say if an element is only avaialble when javascript can run
//input:
function removejavascriptonly() {
	$("#product-more-images").removeClass("javascriptonly");
}


//rotate images in a div with the #rotator id
//input:
function initiaterotator() {
	var rotatoroptions = {
		fx:     'fade',
		timeout:  5000
	};

	$("#rotator img").removeClass("hide");
	$('#rotator').cycle(rotatoroptions);
}


//rotate images in a div with the #rotator id
//input:
function initiateslider() {
	var slideroptions = {
		auto: true,
		continuous: true,
		speed: 500,
		pause: 10000,
		controlsFade: true
	};

	$("#slider ul li").removeClass("hide");
	$("#slider").easySlider(slideroptions);

}


//show/hide shopping basket in checkout pages
//input:
function showhidebasket() {
    $(".reveal-basket").click(function () {
      $(".basket-details").slideToggle("slow");
    });
	
}


//attach a model window to an href with specific class
//input:
//function setmodal() {
//	$('a.nyroModal').nyroModal();
//}


////hide the all of the element with class msg_body
//$(".msg_body").hide();
////toggle the componenet with class msg_body
//$(".msg_head").click(function()
//{
//$(this).next(".msg_body").slideToggle(600);
//});


//set the image zoom options on an href with the .magnify zoom css class
//http://www.mind-projects.it/projects/jqzoom/
//jquery.jqzoom1.0.1.js
//input:
function setzoom() {
//alert("called setzoom");
	var magnifyoptions = {
		zoomType: 'reverse',
		position: 'right',
		xOffset: 13,
		yOffset: 0,
		zoomWidth: 376,
		zoomHeight: 357,
		title: false,
		showEffect: 'fadein',
		hideEffect: 'fadeout',
		fadeoutSpeed: 'slow',
		alwaysOn: false
	};
	
	if(magnifyoptions) {
		$('.magnify').jqzoom(magnifyoptions);
	}
	
	//re-call contextmenu when a new image is loaded
	contextmenu();
}


//set the image zoom options on an href with the .magnify zoom css class
//http://www.jnathanson.com/index.cfm?page=jquery/magnify/magnify
//jquery.magnify-1.0.2.js
//input:
//function setzoom2() {
//var magnifyoptions = {
//	showEvent: 'click',
//    hideEvent: 'click',
//    lensWidth: 150,
//    lensHeight: 150,
//    preload: false,
//    stagePlacement: 'right',
//    loadingImage: 'fulton-zoom-loader.gif',
//
//	lensCss: { 
//		border: '1px solid ##B2B2B2',
//		backgroundColor: '#fff',
//		border: '0px',
//		opacity: 0.5 },
//
//	stageCss: { 
//		border: '1px solid ##B2B2B2',
//		width: '376px',
//		height: '355px' }
//	
//	};
//
//	if(magnifyoptions) {
//		$('.magnify').magnify(magnifyoptions);
//	}
//}


//capture the context menu
//http://abeautifulsite.net/notebook/80
//jquery.contextMenu.js
//jquery.contextMenu.css
//input:
function contextmenu() {
	// Show menu when xyz is clicked
	$(".product-image a img, .magnify").contextMenu({
		menu: 'myMenu'
	},
	function(action, el, pos) {
//		alert(
//		'Action: ' + action + '\n\n' +
//		'Element ID: ' + $(el).attr('id') + '\n\n' +
//		'X: ' + pos.x + ' Y: ' + pos.y + ' (relative to element)\n\n' +
//		'X: ' + pos.docX + ' Y: ' + pos.docY+ ' (relative to document)'
//		);
	});
	
	//disable context menu
	//$(".product-image a img, .magnify").disableContextMenu();
	
}


//load the promos from an xml file
//input: sitesection, set on html page. defines which page to load promos for.
function loadpromos(sitesection) {	
	$.ajax({
	type: "GET",
	url: "/shop/fulton-promos.xml",
	dataType: "xml",
	success: parseXml
	});
}


//parse the promos, placing the correct ones of the current page
//some promos are randomised
//input: xml DOM object
function parseXml(xml)
{
	//get each group
	//iterate all groups until one with given sitesection is found
	//iterate through the group for the given sitesection

	//if promo has id
	//get given promo details based on id
	
	//if promo has no id but has randomise
	//search for randomise node with given id
	//get given promo details based on id

	//build prmo html using details in promo for given id
	
	//find every group
	$(xml).find("group").each(function()
	{
		//if the current group in loop == sitesection var set on html page	
		if ($(this).attr("id") == sitesection) {

			//find each promo within the group
			$(this).find("promo").each(function()
			{
				//if the promo has an id, fetch/build that promo
				if($(this).attr("id")) {
					buildpromo(xml, $(this).attr("id"));
				}

				//if the promo has a randomise, find that randomise group and iterate each rpomo within that group
				if($(this).attr("randomise")) {


					//store the name of the randomise group we're looking for
					randomisegroup = $(this).attr("randomise");

					//get all the randomise groups and find the one we want
					$(xml).find("randomise").each(function()
					{
						//if this is the randomise group we're looking for
						if ($(this).attr("id") == randomisegroup) {

							//get the count of promos within the randomise group
							//generate a random number between 1 and this count
							var promoArray = $(this).find("promo");
							var randomisepromocount = promoArray.size();
							var randompromotouse = generaterandom(randomisepromocount);
							
							buildpromo(xml, $(promoArray[randompromotouse-1]).attr("id"));						
						}
					
					})//end find randomise

				}//end if

			})//end find promo

		}//end if

	});//end find group

	pngFix();
	replaceFonts();

}


//build a spcified promo
//input: xml DOM object
function buildpromo(xml, id)
{
	var list = $("#promo-list");
	var promohtml = "";
	
	$(xml).find("promotionitem").each(function()
	{
		if ($(this).attr("id") == id) {

			//build up the html for a promo li
			promohtml += '<li>';

			if ($(this).find("link").attr("href") != '') {
				promohtml += '<a href="' + $(this).find("link").attr("href") + '"';
				if ($(this).find("link").attr("newwindow") == 'true') {
					promohtml += ' target="_blank"';
				}
				promohtml += '">';
			}
			
			promohtml += '<img src="/shop/' + $(this).find("image").text() + '" alt="' + $(this).find("title").text() + ' - ' + $(this).find("text").text() + '" title="' + $(this).find("title").text() + ' - ' + $(this).find("text").text() + '" />';

			if ($(this).find("link").attr("href") != '') {
				promohtml += '</a>';
			}
			
			promohtml += '</li>';
			list.append(promohtml);

		}//end if
	});//end find promotionitem

}


//styalised text replacement
//recall sifr replace to ensure promos (loaded by ajax) are re-styled
//input:
function replaceFonts() {
	//Cufon.replace('h1', { fontFamily: 'ApexSansMediumT' });
	//Cufon.replace('h2', { fontFamily: 'ApexSansMediumT' });
	
	//var apexsansmediumt = new Font('ApexSansMediumTSifr2.swf', { 
	//							   tags:'h1,h2',
	//							   });	
	//apexsansmediumt.replace();
	
	$('#promo-list>h2').removeClass("sIFR-replaced");
	sIFR.replace(apexsansmediumt, {
	selector: 'h2.darkgrey',
	css: [
		'.sIFR-root { color:#333333 }'
	],
	wmode: 'transparent',
	tuneWidth: 5
	});
	
	
	sIFR.replace(apexsansmediumt, {
	selector: 'h2',
	css: [
		'.sIFR-root { color:#ffffff }'
	],
	wmode: 'transparent',
	tuneWidth: 5
	});



}


//apply a png fix for ie6. done like this as conditional html (<!--[if lte IE 6]>...<![endif]-->) doesn't seem to work?
//input:
function pngFix() {
	$(document).pngFix(); 
}


//generate a random number between 1 and maxValue
//input: max value
function generaterandom(maxValue)
{
	day = new Date();
	hour = day.getHours();
	mins = day.getMinutes();
	sec = day.getSeconds();
	return (((hour + 1) + (mins + 1) + sec) % maxValue) + 1;
}




















