/* Author: Powow AS http://powow.no

*/

/* Global functions and variables */


var $container = $("#container");
var $fillerTarget = $("#main");
var fillerPadding = 0, diff;
fillHeight = function(){
	fillerPadding = parseInt($fillerTarget.css("paddingBottom"),10);
	diff = $(window).height() - $container.height() + fillerPadding;
	diff > 0 && $fillerTarget.css("paddingBottom",diff+"px");
	diff < 0 && $fillerTarget.css("paddingBottom","0px");
};

//var $pbase = $("#showcase");
//var bgbase = parseInt($pbase.css("backgroundPosition").split(" ")[1],10);

/* On doc ready */


$(document).ready(function() {
	  if($('#showcart').length > 0) {
			// Load exchange rates data via the cross-domain/AJAX proxy:
			$.getJSON(
			    'http://openexchangerates.org/latest.json',
			    function(data) {
			        // Check money.js has finished loading:
			        if ( typeof fx !== "undefined" && fx.rates ) {
			            fx.rates = data.rates;
			            fx.base = data.base;
			        } else {
			            // If not, apply to fxSetup global:
			            var fxSetup = {
			                rates : data.rates,
			                base : data.base
			            };
							}

							convert();
					}
			);
	  }
	function convert(){
		$(["GBP","AUD","USD","EUR","SEK","DKK"]).each(function() {
			$('#currency-selector').append('<option value="'+this+'">'+this+': ' + Math.round(fx(CartTotal).from("GBP").to(this)*100)/100 + '</options>');
		});

	};
	//	$('#currency-convert').html("blabla " + fx(CartTotal).from("GBP").to("USD"));
	
	
	
	/* Window size specific function calls */
	$(window).resize(function(){
		fillHeight();
	});
	/* Initialise window size specific function calls */
	$(window).resize();
	
	/*$(window).scroll(function(){
		diff = $(this).scrollTop() / 3;
		$pbase.css("backgroundPosition","50% "+(bgbase+diff)+"px");
	});
	$(window).scroll();*/
	
	$('.narrow #anonnse').live("mouseover", function() {
		$(this).stop(true, false).animate({right: 0}, 200);// Act on the event
	});
	$('.narrow #anonnse').live("mouseout", function() {
		$(this).stop(true, false).animate({right: -150}, 200);// Act on the event
	});
	
	$("body").bind("changedWidth", function(){
		var $annonse = $("#anonnse");
		if($("body").hasClass("wide")){
			$annonse.stop(true, true).css({right: 0});
		} else {
			$annonse.stop(true, true).css({right: -150});
		}
	});
	
	$("nav > ul > li > a").hover(function(){
		$(this).stop(true,true).animate({opacity:0.7},400);
	}, function(){
		$(this).stop(true,true).animate({opacity:1},200);
	});
	
	if($('#cycle').length > 0) {
		$('#cycle').cycle({ 
		    fx:     'scrollLeft', 
		    speed:  '1200', 
		    timeout: 8000, 
		    //pager:  '#nav',
				slideExpr: 'article'
		});
	}
	if($('#cycle-2').length > 0) {
		$('#cycle-2').cycle({ 
		    fx:     'scrollLeft', 
		    speed:  '1200', 
		    timeout: 8000, 
		    //pager:  '#nav',
				slideExpr: 'article'
		});
	}
	var bodyClass = "";
	
	function adjustStyle(width) {
	    var width = parseInt(width, 10);
	    if (width < 1370) {
					if(bodyClass != "narrow"){
						$("body").removeClass(bodyClass);
						bodyClass = "narrow";
		        $("body").addClass(bodyClass);
						$("body").trigger("changedWidth");
					}
			} else {
					if(bodyClass != "wide"){
	        	$("body").removeClass(bodyClass);
						bodyClass = "wide";
						$("body").addClass(bodyClass);
						$("body").trigger("changedWidth");
					}
	    }
	}

	$(function() {
	    adjustStyle($(this).width());
	    $(window).resize(function() {
	        adjustStyle($(this).width());
	    });
	});
	
	$("ul.M1").parent().addClass("trigger"); //Only shows drop down trigger when js is enabled (Adds empty span tag after ul.subnav*)

	$("#mainnav ul li.trigger a").hover(function() { //When trigger is clicked...

		//Following events are applied to the subnav itself (moving subnav up and down)
		$(this).parent().find("ul.M1").fadeIn('fast').show(); //Drop down the subnav on click

		$(this).parent().hover(function() {
		}, function(){
			$(this).parent().find("ul.M1").fadeOut('fast'); //When the mouse hovers out of the subnav, move it back up
		});

		//Following events are applied to the trigger (Hover events for the trigger)
		}).hover(function() {
			$(this).addClass("subhover"); //On hover over, add class "subhover"
		}, function(){	//On Hover Out
			$(this).removeClass("subhover"); //On hover out, remove class "subhover"
	});
	
	$("#showcase-inner:empty").parent().remove();
	
	$("header").attr("id","stickyheader").children().wrapAll("<div class='container'>");
	$("#container").css("paddingTop","86px");
	$(window).scroll(function(){
		$("#stickyheader").css("left",($(window).scrollLeft()*-1));
	});
	
	$("a:urlExternal").attr( "target", "_blank" );
	

	if ($("#cart").length > 0){
		if ($.cookie('chosenlang') == null) {
			$('#EcomOrderDeliveryCountry').prepend('<option value="0">Choose country</option>');
			$("#EcomOrderDeliveryCountry").val($("#EcomOrderDeliveryCountry option:first").val());
		};

		$("#emptycart, .createtheorderandnullthecookie").click(function() {
			$.cookie('chosenlang', null, { path: '/' });
			
		});
	}
	
});

