$(document).ready(function(){							
	
	//-------------------						
	//----- PNG FIX -----
	//-------------------
	//$(document).pngFix(); 




  
/*------------------ */
/*----- Gallery ---- */
/*------------------ */
         
      $('#gallery a').lightBox(); 


	
	//------------------
	// ---- MENU -------
	//------------------
	
	var subSpan = $(".sub").parent().children('a');
  	var arrowGif = "<span><img src='/img/MenuArrow.gif'/></span>"
  
  	 $(subSpan).append(arrowGif); //Only shows drop down trigger when js is enabled (Adds empty span tag after ul.subnav*)  
  	 
	 
	 //$("ul.topnav li a span").fadeOut(3000);
	
	
	//----------------------
	//----On Hover Over-----
	//----------------------
	
function megaHoverOver(){
    $(this).find(".sub").stop().fadeTo('fast', 1).show(); //Find sub and fade it in
    (function($) {
        //Function to calculate total width of all ul's
        jQuery.fn.calcSubWidth = function() {
            rowWidth = 0;
            //Calculate row
            $(this).find("ul").each(function() { //for each ul...
                rowWidth += $(this).width(); //Add each ul's width together
            });
        };
    })(jQuery); 

    if ( $(this).find(".row").length > 0 ) { //If row exists...

        var biggestRow = 0;	

        $(this).find(".row").each(function() {	//for each row...
            $(this).calcSubWidth(); //Call function to calculate width of all ul's
            //Find biggest row
            if(rowWidth > biggestRow) {
                biggestRow = rowWidth;
            }
        });

        $(this).find(".sub").css({'width' :biggestRow}); //Set width
        $(this).find(".row:last").css({'margin':'0'});  //Kill last row's margin

    } else { //If row does not exist...

        $(this).calcSubWidth();  //Call function to calculate width of all ul's
        $(this).find(".sub").css({'width' : rowWidth}); //Set Width

    }
}
//On Hover Out
function megaHoverOut(){
  $(this).find(".sub").stop().fadeTo('fast', 0, function() { //Fade to 0 opactiy
      $(this).hide();  //after fading, hide it
  });
}


//Set custom configurations
var config = {
     sensitivity: 1, // number = sensitivity threshold (must be 1 or higher)
     interval: 100, // number = milliseconds for onMouseOver polling interval
     over: megaHoverOver, // function = onMouseOver callback (REQUIRED)
     timeout: 100, // number = milliseconds delay before onMouseOut
     out: megaHoverOut // function = onMouseOut callback (REQUIRED)
};


$("ul#topnav li .sub").css({'opacity':'0'}); //Fade sub nav to 0 opacity on default
$("ul#topnav li").hoverIntent(config); //Trigger Hover intent with custom configurations
	 
	 
	 
	//------------------
	// ---- SLIDER -----
	//------------------	 
	 
	 
	var currentPosition = 0;
  	var slideWidth = 851;
  	var slides = $('.slide');
	var numberOfSlides = slides.length;

	
	// Remove scrollbar in JS
	$('#slidesContainer').css('overflow', 'hidden');
		// Wrap all .slides with #slideInner div
		slides.wrapAll('<div id="slideInner"></div>')
		// Float left to display horizontally, readjust .slides width
		.css({
		'float' : 'left',
		'width' : slideWidth
	});
	
	// Set #slideInner width equal to total width of all slides
	$('#slideInner').css('width', slideWidth * numberOfSlides);
	
	// Insert left and right arrow controls in the DOM
	$('#slideshow')
	.prepend('<span class="control" id="leftControl"></span>')
	.append('<span class="control" id="rightControl"></span>');
	
	// Hide left arrow control on first load
	manageControls(currentPosition);
	
	// Create event listeners for .controls clicks
	$('.control')
	.bind('click', function(){
	// Determine new position
	  currentPosition = ($(this).attr('id')=='rightControl')
	? currentPosition+1 : currentPosition-1;
	
	  // Hide / show controls
	  manageControls(currentPosition);
	  
	  
	  // Move slideInner using margin-left
	  $('#slideInner').animate({
		'marginLeft' : slideWidth*(-currentPosition)
	  });
	});
	
	// manageControls: Hides and shows controls depending on currentPosition
	function manageControls(position){
		// Hide left arrow if position is first slide
		if(position==0){ $('#leftControl').hide() }
		else{ $('#leftControl').show() }
		// Hide right arrow if position is last slide
		if(position==numberOfSlides-1){ $('#rightControl').hide() }
		else{ $('#rightControl').show() }
	}// end slider
	 
	 
	 
	 
	 /* -- loop sider -- */
	 
	 
	 
	 
	 
	 //  ------------------ //
	 //  chapters accordion //
	 //  ------------------ //

 $(".accordion").accordion();
 
 
 
 	 //  ---------------------------- //
	 //  Smooth scroll to top of page //
	 //  ---------------------------- //
 
/*
 $('.topBtn').click(function() {
  $("html").animate({ scrollTop: 0 }, "slow");
  return false;
});
*/


$(".topBtn").scrollToTop({
   speed: "slow",
 /*   ease:  "easeOutBounce", */
   start: 700
});





	 // ---------------------------- //
	 //         PAYPAL DUES          //
	 //  --------------------------- //


	/* store prices */
		
	var US_price = $(".USPrice").text();
	var INT_price = $(".intPrice").text();
	var regionSelect = $('.regionList :selected').text();
		
	var payPalLink = $(".addToCartBtn a").attr('href');
	
	
	var US_payPalLink = $(".addToCartBtn a").attr('href').replace(INT_price,US_price);  
	var INT_payPalLink = $(".addToCartBtn a").attr('href').replace(US_price,INT_price);  
	
	
	function changePrice(whatRegion){	
		
				
		if (whatRegion =="US"){
			$(".cost").text(US_price);
			changeUrl(US_payPalLink);
		}
	
		
		
		if (whatRegion =="International"){
			$(".cost").text(INT_price);
			changeUrl(INT_payPalLink);
		}

	
	}
	
			
	/* if drop down changed update prices */
	$('.regionList').change(function(){
		
		var regionSelect = $('.regionList :selected').text();
	    changePrice(regionSelect);
    });
		
	  
      
   
   
   /* change the link of the button */
   function changeUrl(link){
   		
   		$(".addToCartBtn a").attr('href',link);  
   		
   
   }
     
   
    changePrice(regionSelect);
  

   

	
   
 });
