/*
	global.js	
*/

// =========================================================
// Global Variables
// =========================================================
	var PHOTO_THUMB_NAME = "thumbnail";
	var PHOTO_MEDIUM_NAME = "big";
	var PHOTO_SMALL_NAME = "small";
	var PHOTO_LARGE_NAME = "large";
	var PHOTO_THUMB_DISPLAY_HEIGHT_BIG = 627;
	var PHOTO_THUMB_DISPLAY_HEIGHT_SMALL = 313;
	var PHOTO_THUMB_HEIGHT = 156;
	
	/* section colours */	
	/* Notes: - Use this convention:  var page_name_color = "color"; 
	          - The JS use the pages body ID (set in block body-id) to match up to the var you create. 
		      - It replaces '-' in your body ID with "_". See line ~515 on this page for where this happens.
	*/	
	var homepage_colour = "yellow";
	var default_colour = "hotpink";
	var style_me_colour = "yellow";
	var sgtv_colour = "darkblue";
	var events_colour = "hotpink";
	var style_snaps_colour = "darkpurple";
	var we_support_colour = "cyan";
	var freebies_colour = "green";
	var forums_colour = "lightpink";
	var blog_colour = "orange";
	var speak_up_colour = "hotpink";	
	var youniverse_colour = "peach";
	var dawn_till_dusk_colour = "darkpurple";	
	var accounts_colour = "hotpink";
	var culture_club_colour = "dustypink";
	var recycle_it_pop_up_shop_colour = "cyan";
	var live_it_give_it_colour = "darkgreen";
	var in_the_spotlight_colour = "red";	
	var collect_me_colour = "cyan";	
	var stripes_colour = "yellow";
	var standard_colour = "hotpink";
	var go_global_colour = "lightblue";
	var super_style_me_colour = "yellow";
	var gift_popup_shop_colour = "darkgreen";
	var mobile = "yellow";
	var richard_nicoll_colour = "red";
	
	var gallery_running = false;
	
	var large_photo_width = 634;
	
	var hideVotingOverlays = function() {
		$(".vote-overlay").fadeOut('fast');
	};

// =========================================================================================
// function applyVotingClick()
// =========================================================================================	
var applyVotingClick = function(panel) {
	
	// =========================================================================================
	// function clearStars()
	// =========================================================================================	
	var clearStars = function() {
		$.each($(panel + ' .stars a img'), function(i, img) {
			src = $(this).attr("lowsrc");
			//console.log("clearstars lowsrc", src);
			$(this).attr("src", src);
			$(this).attr("lowsrc", "");
			if ($(this).hasClass("selected")) { $(this).removeClass("selected"); }
		});	
	}
	// ===| end clearStars() |==================================================================
		
	$(panel + ' .vote-overlay .your-rating a').focus(function() { this.blur(); });
	
	// --| START: event |--------------------------------------------------------------------
	$(panel + ' .vote-overlay .your-rating a').click(function() {
		$.post($(this).attr('href'), function(data) {
			
			data = eval('[' + data + '][0]');
			if (data.success) {
				clearStars();			
				
				$(panel + ' .votes').html(data.score.num_votes + ' vote' + (Math.abs(data.score.num_votes) != 1 ? 's' : ''));

				$.each([panel + ' .stars a img', panel + ' a.stars img'], function(j, selector) {
					$.each($(selector), function(i, img) {
						i += 1;
						star = i <= data.stars ? 'full' : i - .5 == data.stars ? 'half' : 'empty';
						$(img).attr('src', '/site_media/img/ico-action-panel-heart-' + star + '.png');
						$(img).attr('lowsrc', '/site_media/img/ico-action-panel-heart-' + star + '.png');
					});
				});
				
				hideVotingOverlays();
			}
			
		});
		return false;
	});
	// --| END: event |----------------------------------------------------------------------		
		
	// --| START: event |--------------------------------------------------------------------
	$(panel + ' .vote-overlay .your-rating a img').hover(
		function() {
			// store the src of hovered image
			$(this).addClass("selected");
			found = false;

			// loop through all stars to highligh all below current hovered star
			$.each($(panel + ' .stars a img'), function(i, img) {

				src = $(this).attr("src");
				$(this).attr("lowsrc", src);
				
				//console.log("lowsrc",$(this).attr("lowsrc"));
				if (found) {
					src = src.replace(/full/, "empty"); 
					src = src.replace(/half/, "empty"); 											
				} else { 
					src = src.replace(/empty/, "full");
					src = src.replace(/half/, "full");					
				}
				
				if ($(this).hasClass("selected")) {
					found = true;
					src = src.replace(/empty/, "full");
				}
				
				$(this).attr("src", src);
				
			});				
			
		},
		function() { clearStars(); }
	);
	// --| END: event |----------------------------------------------------------------------		
			
	// --| START: event |--------------------------------------------------------------------
	$(panel + ' .vote').unbind('click');
	// --| END: event |----------------------------------------------------------------------				

	// --| START: event |--------------------------------------------------------------------
	$(panel + ' .vote').click(function() {

		hideVotingOverlays();
		
		var voting_overlay = $(panel + " .vote-overlay");
		if (voting_overlay.is(":visible")) { return false;	}

		var vote_panel = $(this).parent();
		var position = vote_panel.position();
		var height = vote_panel.height();
		var width = vote_panel.width();
		
		var voteOverlayY = 1;  // top
		var voteOverlayX = 22;  // left
		var overlayHeight = voting_overlay.height();
		var overlayWidth = voting_overlay.width();
		
		voteOverlayY += position.top - overlayHeight;
		voteOverlayX += position.left;
		
		if (width < overlayWidth) { 
			voting_overlay.css({top: voteOverlayY, left: (position.left + width) - overlayWidth});	
		} else { 
			voting_overlay.css({top: voteOverlayY, left: voteOverlayX});
		}		
		
		voting_overlay.fadeIn("fast");
		
		return false;
	});
	// --| END: event |----------------------------------------------------------------------				
};
// =========================================================================================		


// =========================================================================================
// loadGallery()
// =========================================================================================
function loadGallery(gallery_id, box_size) {
	
	var thumb_panel_height = (box_size == "small") ? PHOTO_THUMB_DISPLAY_HEIGHT_SMALL : PHOTO_THUMB_DISPLAY_HEIGHT_BIG;
	
	if (parseInt($(gallery_id + ' ul').height()) > thumb_panel_height) {
		//$(gallery_id + ' .controls a.down').fadeIn('fast');	
		$(gallery_id + ' .controls a.down').removeClass('disable');
	}	

	// --| START: event |--------------------------------------------------------------------
	// photo hover handler
	$(gallery_id + " ul img").mouseover(function() {
		$(this).animate({"opacity": 0.7}, {duration: "fast"})
	}).mouseout(function() { 
		$(this).animate({"opacity": 1}, {duration: "fast"})
	});
	// --| START: event |--------------------------------------------------------------------
	
	// --| START: event |--------------------------------------------------------------------
	// call in parts from full page and update share/vote/comments
	$(gallery_id + " .listing ul a").click(function() {
														
		// THIS IS THE CALLBACK FUNCTION THAT PARSES THE VARIOUS
		// DATA FROM THE FULL DETAILS PAGE (SHARING/COMMENTS/VOTE)
		// WE PASS IT TO THE IMAGE CHANGE FUNCTION SO THAT IT DOESN'T
		// INTERFERE WITH THE SMOOTHNESS OF THE IMAGE TRANSITION
		var clickedImg = $(this);
		var photo_url = clickedImg.attr('href');
		var callback = function() {
			
			$.get(photo_url, function(data) {
											  
				data = data.split('<body')[1];
				data = data.split('>');
				data.shift();
				data = data.join('>').split('</body>')[0];
				data = $(data);
				
				// comments
				$(gallery_id + ' .num-comments').text('(' + data.find('#num-comments').text() + ')');
				
				// voting
				$(gallery_id + ' .vote').html($(data.find('#vote')[0]).html());
				
				// voting overlay
				$(gallery_id + ' .vote-overlay').html($(data.find('#vote-overlay')[0]).html());
				applyVotingClick(gallery_id);

				// sharing
				$(gallery_id + ' .sharing-panel').html($(data.find('.sharing-panel')[0]).html());
				$(gallery_id + ' .sharing-panel').find('input[name=next]')[0].value = location.href;
				
				//facebook and twitter sharing
				$(gallery_id + ' .social-media').html($(data.find('#social-media')[0]).html());
				
				
				var b_small_gallery = false;
				if (gallery_id.search('small') >= 0) {
					b_small_gallery = true;
				}
				
				if (!b_small_gallery) {
					// imagemap
					var photo_large = data.find('#photo-large img');
					var imagemap_name = '';
					var imagemap_id = null;
					var imagemap_code = '';
					var imagemap_html = '';
					// if photo has reference to imagemap
					if ((photo_large.attr('usemap') !== undefined) && photo_large.attr('usemap').length > 0) {
						
						// if imagemap already exists then remove it
						$(gallery_id).find('.display map').remove();					
						
						imagemap_name = photo_large.attr('usemap');	// set imagemap name
						imagemap_name = imagemap_name.replace(/#/,'');
						
						if (data.find('map[name=' + imagemap_name + ']').length < 0) {
							imagemap_name = '';
						} else {
							
							// get imagemap code
							imagemap_html = data.find('map[name=' + imagemap_name + ']');
							// Safari has a problem working with the imagemap id and name pulled from the detail page
							// postfixing with '-e' help remedy the problem
							imagemap_id = imagemap_html.attr('id') + '-e';
							imagemap_name = imagemap_name + '-e';
							
							// Both Safari and IE have trouble dynamically adding an imagemap and associating it to an existing image.
							// A work around is to dynamically rewrite the image as well.
							var image = $(gallery_id).find('.display img');
							var image_src = image.attr('src');
							var image_width = image.attr('width');
							if(!image_width) {
								image_width = image.width();
							}
							var image_height = image.attr('height');
							if(!image_height){
								image_height = image.height();
							}
							// calculate to ratio to scale the image map by gallery image / large detail image
							
							//var imagemap_ratio = Number(image_width) / Number(photo_large.attr('width'));
							var imagemap_ratio = Number(image_width) / large_photo_width;
							
							imagemap_html = ShopAPIUI.scale_imagemap_coords(imagemap_html, imagemap_ratio).html();						
							
							imagemap_html = '<map name="' + imagemap_name + '" id="' + imagemap_name + '">' + imagemap_html + '</map>';
							
							$(gallery_id).find('.display img.first').addClass('remove').removeClass('first');
							$(gallery_id).find('.display a').html('<img src="' + image_src + '" usemap="#' + imagemap_name + '" width="' + image_width + '" height="' + image_height + '" class="first">')
	
							// add imagemap
							$(gallery_id).find('.display').append(imagemap_html);
							// connect imagemap to image - this doesn't work properly under Safari and IE, refer to notes above.
							//$(gallery_id).find('.display img.first').attr('usemap', '#' + imagemap_name);
							
							ShopAPIUI.load_product_imagemap(imagemap_id);
						}
					}
				}
				
				formFieldHighlighting();

			});
			
			//add carousel
			var products_list = clickedImg.attr("rel");
			if (products_list) {
				ShopAPIUI.load_products_carousel("Shop her look", {style_id: products_list, limit: '15'}, gallery_id + " .carousel" );
			} else {
				$(gallery_id + " .carousel").hide();
			};
		};
		
		// click of first image is forced below to trigger ajax callback
		// when this happens just run the callback, don't do the image transition
		if ($(gallery_id).attr('loaded')) {
			var firstimg = $(this).find('img')[0];
			galleryThumbClick(firstimg, gallery_id, box_size, callback);
		
		} else {
			$(gallery_id).attr('loaded', 'true');
			callback();
		}

		return false;
	});
	// --| END: event |----------------------------------------------------------------------
	

	// --| START: event |--------------------------------------------------------------------
	// paging controls handler
	$(gallery_id + " .controls a.down").click(function(event) {
		event.preventDefault();
		triggerGalleryControl(gallery_id, "down", box_size);
	});
	// --| END: event |----------------------------------------------------------------------
	
	// --| START: event |--------------------------------------------------------------------
	// paging controls handler
	$(gallery_id + " .controls a.up").click(function(event) {
		event.preventDefault();
		triggerGalleryControl(gallery_id, "up", box_size);
	});
	// --| END: event |----------------------------------------------------------------------
	
	// lightbox trigger
/*	$(gallery_id + " .display a").lightBox({
		imageLoading: "/site_media/img/lightbox-loading.gif",
		imageBtnClose: "/site_media/img/lightbox-btn-close.gif",
		imageBtnPrev: "/site_media/img/lightbox-btn-prev.gif",
		imageBtnNext: "/site_media/img/lightbox-btn-next.gif",								
		galleryID: gallery_id + " .listing li a"
	});*/
	
	// do the ajax call for the initial image to load voting/share
	$(gallery_id + " .listing ul a:first").click();

	return false;	
}
// =========================================================================================


// =========================================================================================
// triggerGalleryControl()
// =========================================================================================
function triggerGalleryControl(gallery_id, direction, box_size) {
	
	var height = $(gallery_id + ' ul').height();
	var css_top = $(gallery_id + ' ul').css('top');	
	
	var thumb_panel_height = (box_size == "small") ? PHOTO_THUMB_DISPLAY_HEIGHT_SMALL : PHOTO_THUMB_DISPLAY_HEIGHT_BIG;
	
	if (direction == "up") {
		if (!gallery_running) {
			gallery_running = true;
			if (Math.abs(parseInt(css_top)) - (thumb_panel_height - PHOTO_THUMB_HEIGHT) >= 0) {
			//if (Math.abs(parseInt(css_top)) != 0) {
				css_top = parseInt(css_top) + (thumb_panel_height - PHOTO_THUMB_HEIGHT);
				//$(gallery_id + ' a.down').css('display', 'block');
				$(gallery_id + ' a.down').removeClass('disable');
				
				//console.log("css_top = ", Math.abs(parseInt(css_top)));
				//console.log("eq = ", Math.abs(parseInt(css_top))-PHOTO_THUMB_DISPLAY_HEIGHT);
				
				if ((Math.abs(parseInt(css_top))) <= 0) {
					//$(gallery_id + ' a.up').css('display', 'none');
					$(gallery_id + ' a.up').addClass('disable');
				}
			}
			
			$(gallery_id + ' ul').animate({ top: css_top }, "slow", "swing", function() {
				gallery_running = false;																										  
			});
		}
	} else { //down
		if (!gallery_running) {
			gallery_running = true;
			if (Math.abs(parseInt(css_top)) + thumb_panel_height < height) {
				css_top = parseInt(css_top) - (thumb_panel_height - PHOTO_THUMB_HEIGHT);
				//$(gallery_id + ' a.up').css('display', 'block');
				$(gallery_id + ' a.up').removeClass('disable');
				
				if (Math.abs(parseInt(css_top)-thumb_panel_height) > height) {
					//$(gallery_id + ' a.down').css('display', 'none');
					$(gallery_id + ' a.down').addClass('disable');
				}			
			}
			
			$(gallery_id + ' ul').animate({ top: css_top }, "slow", "swing", function() {
				gallery_running = false;
			});		
		}
	}
	
	return false;
}
// =========================================================================================

// =========================================================================================
// galleryThumbClick()
// =========================================================================================
function galleryThumbClick(img_obj, gallery_id, size, callback) {

	hideVotingOverlays();
	
	var thumbSrc = $(img_obj).attr('src');
	var image_size = (size == PHOTO_MEDIUM_NAME) ? PHOTO_MEDIUM_NAME : PHOTO_SMALL_NAME;
	var mediumSrc = thumbSrc.replace(/thumbnail/, image_size);
	var largeSrc = thumbSrc.replace(/thumbnail/, PHOTO_LARGE_NAME);
	var clickThrough = $(img_obj).parent("a").attr("href");
	var title = $(img_obj).attr('alt');	

	// set the sharing link href to the full photo page href
	$($(gallery_id + ' .share-with-friends')[0]).attr('href', clickThrough);
	
	//console.log($(gallery_id + ' .display img:first').attr('class'));
	
	$(gallery_id + ' .display img.first').fadeOut('fast', function() { 
		$(gallery_id + ' .display img.first').remove();
		$(gallery_id + ' .display map').remove();
		$(gallery_id + ' .display a.first').prepend('<img src="' + mediumSrc + '" style="display: none;" title="' + title + '" class="first">');
		//$('#' + gallery_id + ' .display a.first').attr('href', largeSrc);
		$(gallery_id + ' .display a.first').attr('href', clickThrough);
		$(gallery_id + ' .action-panel a').attr('href', clickThrough);
		$(gallery_id + ' .toolbar .description').html(title);			
		$(gallery_id + ' .display img.first').fadeIn('slow', callback);
	});	
	
	return false;	
}
// =========================================================================================

// =========================================================================================
// formFieldHighlighting()
// =========================================================================================
function formFieldHighlighting() {
	
	$("input:text, input:password, select, textarea").focus(function() { 
		$(this).addClass("hover");
	}).blur(function() { 
		$(this).removeClass("hover");	
	});

	$('input.required, select.required, textarea.required').prev('label').append('&nbsp;*');

	return false;	
}
// =========================================================================================

// =========================================================================================
// controlPoll()
// =========================================================================================
function controlPoll() {
	
	// hide submit button, submit will be handled by onclick of radio
	if ($("#poll input.submitinput").length > 0) {
		$("#poll input.submitinput").css("display", "none");
		
		//$("#poll input.radioinput").click(function() { /* ie bites on this like a little girl */
		$("#poll label").click(function() {
			$(this).siblings("input").click();
			$(this).parents("form").submit();
		});
	}
	
	return false;	
}
// =========================================================================================

// =========================================================================================
// ajaxDeleteComment()
// =========================================================================================
function ajaxDeleteComment(me) {
	
	if (confirm("Are you sure you want to delete this comment?")) {
		comment = $(me).parents(".comment");
		$.ajax({
			type: "POST",
			url: $(me).attr("action"),
			success: function() { 
				comment.slideUp("fast");
				comment.html("");
			}
		});
	} 
	return false;	
	
}
// =========================================================================================

// =========================================================================================
// preloadNavImages()
// =========================================================================================
function reverseLoginNav() {
	
	var section = $('body').attr('id');	
	var colour = default_colour;
	
	if (section.length > 0) {
		colour = window[section.replace(/-/g, "_") + '_colour'];
		if (!colour) {
			colour = default_colour;
		}
	}
	
	$('#header ul.secondary img.reverse').each(function() { 
		imgSrc = $(this).attr('src');
		imgSrc = imgSrc.replace(/base/, colour);
		$(this).attr("src", imgSrc);
	});
	
	$('#header ul.secondary img.reverse').mouseover(function() {
		var img = $(this);			  
		var currentSrc = img.attr("src");
		img.attr("lowsrc", currentSrc);
		var hoverSrc = currentSrc.replace(colour, "base");
		img.attr("src", hoverSrc);
	}).mouseout(function() { 
		var img = $(this);			  
		var oldSrc = img.attr("lowsrc");
		img.attr("lowsrc", "");
		img.attr("src", oldSrc);
	});	
	
	return false;	
	
}
// =========================================================================================

// =========================================================================================
// preloadNavImages()
// =========================================================================================
function preloadNavImages(colour) {
	
	$('#header ul img, #footer ul img').each(function() { 
		imgSrc = $(this).attr('src');
		imgSrc = imgSrc.replace(/base/, colour);
		$("<img>").attr("src", imgSrc);
	});
	return false;	
	
}
// =========================================================================================

// =========================================================================================
// function navRollOver()
// =========================================================================================
function navRollOver() {
	
	var section = $('body').attr('id');
	var colour = default_colour;	
	
	if (section.length > 0) {
		colour = window[section.replace(/-/g, "_") + '_colour'];
		if (!colour) {
			colour = default_colour;
		}
	}
	
	preloadNavImages(colour);
	
	$('.nav img').mouseover(function() {
		if (!$(this).hasClass("reverse")) {
			var img = $(this);
			var currentSrc = img.attr("src");
			img.attr("lowsrc", currentSrc);
			var hoverSrc = currentSrc.replace(/base/, colour);
			img.attr("src", hoverSrc);
		}
	}).mouseout(function() { 
		if (!$(this).hasClass("reverse")) {
			var img = $(this);			  
			var oldSrc = img.attr("lowsrc");
			img.attr("lowsrc", "");
			img.attr("src", oldSrc);
		}
	});
	
	return false;	
	
}
// =========================================================================================
	
// =========================================================================================
// function displayActionPanelPopup(panel, speedUp, speedDown)
// returns true if visible, false if not.
// =========================================================================================	
function displayActionPanelPopup(selector, speedUp, speedDown) {
	var panel = $(selector);
	
	if (panel.is(":hidden")) {
		
		panel.slideDown(speedDown, function() { 
			var scroll_element = panel.parent();
			var id = panel.attr('id');			
			var top = $('#' + id).position().top;
			$('html,body').animate({scrollTop:  scroll_element.position().top + top}, speedDown);	
			return true;			
		});
	} else {
		panel.slideUp(speedUp, function () {
			var scroll_element = panel.parent();
			$('html,body').animate({scrollTop: scroll_element.position().top}, speedUp);
			return false;			
		});
	}		
}
// =========================================================================================

// =========================================================================================
// function: sectionHighlight
// =========================================================================================
var sectionHighlight = function() {
	if ($('body').attr('id').length > 0) {
		var section = $('body').attr('id');
		$('#header ul img, #footer ul img').each(function() { 
			imgSrc = $(this).attr('src');
			
			if (imgSrc.search(section) >= 0) {
				if (section.length > 0) {
					colour = window[section.replace(/-/g, "_") + '_colour'];
					if (!colour) {
						colour = default_colour;
					}
				}
				imgSrc = imgSrc.replace(/base/, colour);
				$(this).attr('src', imgSrc);
			}
			
		});
	}
}
// =========================================================================================

// =========================================================================================
//
// jQuery main
//
// =========================================================================================
Cufon.set("hover", "true"); // this is very resource intensive
Cufon.replace('h2');
Cufon.replace('h3');
Cufon.replace('#overlay-product form legend span');
Cufon.replace('#banner a');
Cufon.replace('li.banner a');
Cufon.replace('.gpus-nav a, .gpus-head'); //For Pop Up Shop nav
$(document).ready(function() { 
	$("#boxes h2").addClass("cufon");
	$("#boxes h3").addClass("cufon");
	$("#overlay-product h3").addClass("cufon");
	$("#overlay-product form legend span").addClass("cufon");
	
	// remove dashed highlight around anchored image / there are accessibility issues with thsi.
	//$("#header a").focus(function() { this.blur(); });
	
	// highlight current nav 
	// section in the primary nav
	sectionHighlight();
	
	// toggle reply form
	$("#boxes .comments .reply").click(function() { 														  
		var form = "." + $(this).attr("id");
		$(form).is(":hidden") ? $(form).slideDown("slow") : $(form).slideUp("slow");			
		return false;
	});
	
	// toggle comments flag form
	$("#boxes .comments .flag").click(function() { 														  
		var form = "." + $(this).attr("id");
		$(form).is(":hidden") ? $(form).slideDown("slow") : $(form).slideUp("slow");			
		return false;
	});
	
	// toggle forum post flag form
	$("#boxes .forum a.flag").click(function() {
		var form = "." + $(this).attr("id");
		$(form).is(":hidden") ? $(form).slideDown("slow") : $(form).slideUp("slow");			
		return false;
	});	

	// ajax control for comment deletion.
	$("#boxes .comments .comment-delete").submit(function() { 
		return ajaxDeleteComment(this);
	});
	
	/* form validation */
	$("#post-comment").validate();
	$("#post-reponse").validate();
	$("#signup").validate();
	/*$("#login").validate();*/
	$("#contact").validate();
	$(".form-share-friend").each(function() { $(this).validate(); });
	$("#enter-comp").validate();
	$('#post-comment').validate();
	$('.comment-response').each(function() { $(this).validate(); });
	
	controlPoll();
	
	formFieldHighlighting();
	
	// header and footer nav image preloader
	//preloadNavImages();
	navRollOver();
	
	reverseLoginNav();

	// event handler for share with friends drop down.
	$('.share-with-friends').click(function() {	
		$(this).toggleClass('active');														 
		displayActionPanelPopup("#overlay-" + $(this).attr('id'), "slow", "slow");
		SGGoogleAnalytics.load_sharing_hooks($("#overlay-" + $(this).attr('id')));
		return false;
	});
	
	$('.sharing-close').click(function() {
		$('#share-with-friends-overlay').slideUp("fast");
	});
	
	$("body").click(hideVotingOverlays);
	
	if (location.href.indexOf("#") > 0) {
		$('.back-button a').hide('fast');
	}	
	
   $('.back-button a').click(function() {
		// temporary back button
		if (location.href.indexOf("#") > 0) {
			history.go(-2);
		} else {
			history.back(-1);
		}
		return false;
   });
   
	$('#boxes .box h2 a').mouseover(function() {
		$(this).parent('h2').addClass('hover');
	}).mouseout(function() {
		$(this).parent('h2').removeClass('hover');
	});
	
	$('dd.post blockquote').each(function() {
		$(this).prepend('<img src="/site_media/img/ico-quote-left.png" width="34" height="18" class="png" alt="">');
		$(this).append('<img src="/site_media/img/ico-quote-right.png" width="34" height="18" class="png" alt="">');
	});
	
	// dynamic load for find a store page.
	$("select#store").change(function() {
		store = $(this).val();
		
		$('div.store').each(function() {
			if ($(this).is(':visible')) {
				$(this).fadeOut('fast', function() {
					$('#' + store.toUpperCase()).fadeIn('fast');
				});
			}
		});
		
		if (!$('div.store').is(':visible')) {
			$('#' + store.toUpperCase()).fadeIn('fast');
		}
	});
	
	// Store Map - 05/12/2011
	$("a.map").click(function(e){
		$(".map-canvas").empty()
		$(".map-container").slideUp();
		
		var parent = $(this).parent().parent();
		var canvas = $(this).attr("id").replace("details","map-canvas");
		var lat = $(this).attr("rel");
		var long = $(this).attr("title");
		var storeName = parent.find(".storename").text();
		var address = parent.find(".location p").text();
		var phone = parent.find(".phone").text();
		
		if( $("#" + canvas).is(":empty") ) {
			parent.next(".map-container").slideDown();
			initialize(lat, long, canvas, storeName, address, phone);
		}
		else
		{
			parent.next(".map-container").slideUp();
		}
		
		e.preventDefault();
	});
	
	$(".map-container a.close").click(function(e){
		var parent = $(this).parent().parent();
		parent.slideUp();
		$(".map-canvas").empty();
		
		e.preventDefault();
	});
	// IE has a problem with the anchor on the main image displayed within a photo gallery
	// clicking it does not activate the link through to the detail page
	// the code below forces it
	if ($('.display a.first').length > 0 && $.browser.msie) {
		$('.display a.first').click(function() {
			document.location.href = $(this).attr('href');
		});	
	}	
	
	$("#banners").easySlider({
		loop: true,
		orientation: 'vertical',
		speed: 300,
		autoplayDuration: 5500,
		autogeneratePagination: false,
		restartDuration: 200,
		pauseable: true
	});
	
	// Setup for allowing generic image popup on CMS pages
	$("a.cms-gallery").lightBox({
		imageLoading: "/site_media/img/lightbox-loading.gif",
		imageBtnClose: "/site_media/img/lightbox-btn-close.gif",
		imageBtnPrev: "/site_media/img/lightbox-btn-prev.gif",
		imageBtnNext: "/site_media/img/lightbox-btn-next.gif"
	});
	
	SGGoogleAnalytics.load_page_hooks();
	
});
// =========================================================================================
