/*
Edited: 07 Sep 2009 (Daniel Bull @ 13:55)
Website: http://www.majestic.co.uk/
Created: 
Author: Snow Valley UX team
=============================
Snow Valley Javascript
=============================
*/

jQuery(document).ready(function ($) { // this is for a few files that need to run on dom ready
//console.log('ready getting initialised');


  changeButtons();
	jQuery('#shopping-trolley').css('cursor', 'pointer')
								.click(function() { window.location = '/checkout'; } );
	

	if ( jQuery('.paging div.paging-options').length) {

		setPanel( jQuery('.paging .show-or-hide-paging-options > a.view-options'), jQuery('.paging p.hide-it a.view-options'), jQuery('.paging div.paging-options'));
		HidePagingOptions();
	}

    Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(function() { 
      // fade out basket overlay after set time
      if ( $('.add .added-to-basket').length > 0 ) {
        setTimeout(function() {
          $('.add .added-to-basket').eq(0).fadeOut();
        }, 7000);
      };
      
      setupTasteFlyouts();
      changeButtons();
      initFlyOuts();
    });
    
    
    
    setupTasteFlyouts();

	jQuery('.star-rating').each(function() { 
		new StarRating(this, jQuery(this).prev().find('select').get(0));
  });

	jQuery('#promotional-items li.product').normaliseHeight();



function changeButtons () {
	jQuery('.linkbutton').each(function() {
		var button = this;
 		var link = jQuery('<a>' + jQuery(button).attr('value') + '</a>')
					.attr('title', jQuery(button).attr('title') || jQuery(button).attr('value') )
          .click(function() { jQuery(button).click() } )
					.keypress( function() { jQuery(button).click() } )
          .mouseover( function() { this.style.color = "#b74b87" } )
          .mouseout( function() { this.style.color = "#96a410" } )
          .css('cursor', 'pointer' );

		jQuery(button).removeClass('linkbutton').before(link)
						.hide();
	});
}


function initFlyOuts() {

	if ( jQuery('#product-list').length > 0 ) {
	
		jQuery('#product-list .flyout').hide();
		jQuery('#product-list .flyout-link')
			.click(function() {
				var flyout = jQuery('#' + jQuery(this).attr('rel'));
				var closeImage = '<img src="/assets/majestic/images/btn-close.gif" alt="Close this window" />';

				flyout.css({
					display: 	"block", 
					position: 	"absolute", 
					bottom: 	"1.5em", 
					right: 		"auto", 
					top: 		"auto", 
					left: 		"30%", 
					zIndex: 	"9999"
				});

				flyout.find('h5:first').html( jQuery(this).html() + " " + closeImage );
				flyout.find('h5:first').addClass('cursor').click(function() {
					flyout.id = flyout.attr('id');
					jQuery(this).parents('#' + flyout.id).hide();
					//console.log(flyout.id);
				});
				return false;
			});
		};
}

initFlyOuts();

// set option panel behaviour
function setPanel(showLink, hideLink, panel) {
	if ((showLink.length) && (hideLink.length) && (panel.length)) {

		showLink.show();
		hideLink.show();
	
		showLink.click(function(){ 
			ShowPagingOptions(false);
			return false;
		});
	
		hideLink.click(function(){ 
			ShowPagingOptions(true);
			return false;
		});
	
	} 
}


function ShowPagingOptions(makeInvisible) {
	jQuery('.paging div.paging-options').css('visibility', (makeInvisible ? 'hidden' : 'visible') ).show();
}

function HidePagingOptions() {
	ShowPagingOptions(true);
}


function toggleGiftMsgArea() {
	var checkOff = function () {
		if ($('.gift-msg').css('display') == 'none' && $('.gift-msg').slideUp('fast').children('textarea').val() != ''){
			$('.gift-msg').slideUp('fast').children('textarea').val('');
		}
	}
	$('input[@type=checkbox]','span.checkbox.gift').click( function() {
		$('.gift-msg').slideToggle('fast', checkOff);
	});
}
// init toggleGiftMsgArea
if (jQuery('input[@type=checkbox]','span.checkbox.gift').length > 0) {
	toggleGiftMsgArea();
}

//toggling the Card Issue Date to appear
function toggleStartDate () {
	var targetSelect = $('select.type[@id*="Card"]');
	var cardType = (targetSelect.length) ? true: false;
	if (cardType == true){
		targetSelect.change(
			function () {
				var cardTypeName = $('option:selected', targetSelect ).text().trim();
				var targetCards = (cardTypeName == 'Maestro') ? true : false;
				var divToShow = $('div#card-options, .threeds');
				if (targetCards && divToShow.css('display')== 'none'){
					divToShow.bGCOrig = divToShow.css('background-color');
					divToShow.bGColour = (divToShow.css('background-color') == 'transparent') ? "#fff" : divToShow.css('background-color');
					divToShow.parentBG = divToShow.parent().css('background-color');
					divToShow.slideDown('fast').css('background-color', divToShow.parentBG).queue( function(){
						divToShow.animate({backgroundColor: "#F7F8E1"}, "slow", function(){
							divToShow.animate({backgroundColor: divToShow.bGColour}, 5000, function (){
								divToShow.css('background-color', divToShow.bGCOrig);
							});
						});
						divToShow.dequeue();
					});
					
				}
				else if (!targetCards) {
					divToShow.slideUp('fast');
				};
			}
		);
	}
}
// init toggleStartDate 
if (jQuery('select.type','div.type').length > 0 && jQuery('input, select','div#card-options').length > 0) {
	toggleStartDate();
}

//slide order detail area in vieworder.aspx page
$(".web-table","#checkout").css("display","none");
$("#view-more","#checkout").css("cursor","pointer");
$("#view-more","#checkout").toggle(function(){
    $(".web-table","#checkout").slideDown('slow');
    $(this).addClass("up").removeClass("down");
    },
    function(){
    $(".web-table","#checkout").slideUp('slow');
    $(this).addClass("down").removeClass("up");
    });


});//end stuff that needs to run on dom ready


function setupTasteFlyouts() {
	jQuery('div.taste-img').hover(function() {
		jQuery('div.popup', this).not(':animated')
			.fadeIn()
			.mouseout(function() {
				jQuery(this).not(':animated')
					.fadeOut();
			});
		}, function() {
			jQuery('div.popup', this).fadeOut();
		})
		.children('div').hide();
}

function StarRating(element, selectElement) { // used for Write a Review

	var elementW		= jQuery(element).width(),						// star-rating element
		offsetLeft		= jQuery(element).offset().left;
		ratingElement	= jQuery('.rating', element).eq(0),				// pinks stars element
		dropdown		= selectElement || jQuery(element).siblings().find('select'),
		currentStep		= 0,
		selectedStep	= 0;
		
	if (dropdown.length == 0) { // don't continue if the select element is not present
		return false;
	};

	jQuery(element).hover(function() {
	
	}, function() {
		jQuery(ratingElement).attr('class', 'rating is-' + selectedStep );
  });

	jQuery(element).click(function() { 
		selectedStep = currentStep; // store the rating the user selects
		jQuery('option', dropdown).eq( selectedStep ).attr('selected', 'selected'); // set the dropdown so the rating will be posted back to the server
	}).css('cursor', 'pointer'); 

	jQuery(element).mousemove(function(e) {
		var pos = e.clientX - offsetLeft;

		currentStep = Math.round( (pos / elementW) * 100 / 20 );  // calculate number of stars out of 5

		jQuery(ratingElement).attr('class', 'rating').addClass('is-' + currentStep); // set the class to show the number of stars

	});

	return element;
}

/*
	Function: normaliseHeight

	Forces the height of all matched elements to equal the tallest element


*/

var SV = SV || {};

SV.normaliseHeight = function normaliseHeights() { // find the tallest elements and force siblings to the same height
	var elementHeights = [];
	
	this.each(function() {
		elementHeights.push( $(this).height() );
	});
	
	elementHeights.sort(function(a, b) { 
		if (a > b) { return 1 };
		if (a < b) { return -1};
		return 0;
	}).reverse();

	this.css('height', elementHeights[0] + 'px');

	return this;
}

jQuery.fn.extend({
	normaliseHeight: SV.normaliseHeight
});

