$(window).load(function() {  
	// Fade in the site
	$("div#wrapper").animate({opacity: 1}, 'fast');
 
});
$(window).resize(
	// Viewport resizing
	stretch_portal_content
);
$(document).ready(function() {
	//checkBrowser();
	var FoxyDomain = "various.foxycart.com/";
	vperams = getURLParam("confirm");
	check_cart_quant();
	boxesTotalWidth();
	// Shadowbox
	window.shadowOpen = 0;
	Shadowbox.init({
	    skipSetup: true,
		onClose: function() {
			window.shadowOpen = 0;
		}
	});
	Shadowbox.setup("p.subscribe-link a", {
		player: "iframe",
		content: "http://www.various.co.uk/index.php?id=7",
		height: 200,
		width: 400,
		displayCounter: false,
		onOpen: function() {
			window.shadowOpen = 1;
		}
	});
	
	// Viewport sizing
	stretch_portal_content();

	// URL Perams Shadowbox
	addEvent(window, 'load', signedUp);
	
	// Unbind FoxyBox 
	$('a.foxycart').unbind('click').attr('rel','shadowbox');
		if (typeof(fc_CookieFix) == "function") {
			fc_CookieFix();
	}

	Shadowbox.setup("a.foxycart.shadowbox", {
		player: "iframe",
		height: 500,
		width: 779,
		displayCounter: false,
		onOpen: function() {
			window.shadowOpen = 1;
		},
		onClose: function() {
			fc_UpdateCart(FoxyDomain);
			check_cart_quant();
			window.shadowOpen = 0;
		}
	});
	$('a.add-me').click(function() {
		get_fcsid();
		$.getJSON($(this).attr('href') + '&output=json&callback=?' + fc_AddSession(), function(data){
			fc_UpdateCart(FoxyDomain);
			fc_json = data;
			check_cart_quant();
		});
		return false;
	});
	Shadowbox.setup("a.dashboard", {
		content: '#dashboard',
		player: ["inline"],
		height: 380,
		width: 460,
		displayCounter: false,
		onOpen: function() {
			window.shadowOpen = 1;
		}
	});
	$("a.foxycart.shadowbox").click(function() {
		$('#sb-info-inner').addClass('narrow');
	});
	$("a.info").click(function() {
		$('#sb-info-inner').addClass('narrow');
	});
	$(".subscribe-link a").click(function() {
		$('#sb-info-inner').addClass('narrow');
	});
	$("a.dashboard").click(function() {
		$('#sb-info-inner').addClass('narrow');
	});
	$("a.artwork-button").click(function() {
		$('#sb-info-inner').addClass('narrow');
	});

	// Toggle release info
	$('div.toggle').toggle(function() {
		$(this).closest('.info').find('.hide-box').fadeIn(200);
	}, function() {
		$(this).closest('.info').find('.hide-box').fadeOut(200);
	});
	
	// IE fix
	$("div.frame").each(function() {
		$(this).find('div.fltr-img').css({"opacity": "0"});
	});
	
	// Frame controls
	$("div.frame").hover(function() {
	    $(this).find('div.fltr-img').stop().animate({ opacity: 1 }, '100');
	}, function() {
	    $(this).find('div.fltr-img').stop().animate({ opacity: 0 }, '100');
	});
	
	// Setup repeat sections for scrollTo
	$("li.section").each(function(i){
		var currentId = $(this).attr('id');
		$(this).find('li.sub-section').attr({ id: currentId });
	});
	$("li.section").each(function(){
		$(this).find('li.sub-section').each(function(i){
			var currentId = $(this).attr('id'); 
			$(this).attr({ id: currentId + ++i });
		});
	});
	$('div.pane').scrollTo( 0 );
	$.scrollTo( 0 );
	
	// Setup Nav
	IDs = $("li.section[id]").map(function() { return this.id; }).get();
	subIDs = $("#release li.sub-section[id]").map(function() { return this.id; }).get();
	rel = 1;
	sec = 1;
	$("li.right a").attr({ href: '#' + subIDs[1] });
	$("li.left a").attr({ href: '#' + subIDs[0] });
	$("li.down a").attr({ href: '#' + IDs[1] });
	$("li.up a").attr({ href: '#' + IDs[0] });
	
	$("li.left div").hide();
	$("li.up div").hide();	
});
// Mouse Nav & localScroll setup
jQuery(function( $ ){
	$.localScroll.defaults.axis = 'xy';
	$.localScroll.hash({
		target: '#container',
		duration:500
	});
	$.localScroll({
		target: '#container',
		duration:500,
		hash:false,
		onBefore:function( e, anchor, $target ){
		},
		onAfter:function( anchor, settings ){
		}
	});
	$("li.right a").click(function() {
		go_right();
	});
	$("li.left a").click(function() {
		go_left();
	});
	$("li.up a").click(function() {
		go_up();
	});
	$("li.down a").click(function() {
		go_down();
	});
	$("li.home a").click(function() {
		go_home();
	});
	$("#footer a").click(function() {
		go_down();
	});
});

// Keyboard Nav
$(document).keydown(function(event) {
    var key = event.keyCode || event.which;
    if(event.target.type !== 'textarea') {
		if(window.shadowOpen == 0) {
			// Right arrow
			if (key === 39) {
				var linkri = $("li.right a").attr( "href" );
				$('#container').stop().scrollTo( linkri, 500, {easing:'swing'});
				go_right();
				$("li.right a").addClass("over");
				setTimeout(function() {
					$("li.right a").removeClass("over");
				}, 50);
				return false;
			}
			// Left arrow
			if (key === 37) {
				var linkli = $("li.left a").attr( "href" );
				$('#container').stop().scrollTo( linkli, 500, {easing:'swing'});
				go_left();
				$("li.left a").addClass("over");
				setTimeout(function() {
					$("li.left a").removeClass("over");
				}, 50);
				return false;
			}
			// Up arrow
			if (key === 38) {
				var linkup = $("li.up a").attr( "href" );
				$('#container').stop().scrollTo( linkup, 500, {easing:'swing'});				
				go_up();
				$("li.up a").addClass("over");
				setTimeout(function() {
					$("li.up a").removeClass("over");
				}, 50);
				return false;
			}			
			// Down arrow
			if (key === 40) {
				var linkdo = $("li.down a").attr( "href" );
				$('#container').stop().scrollTo( linkdo, 500, {easing:'swing'});
				go_down();
				$("li.down a").addClass("over");
				setTimeout(function() {
					$("li.down a").removeClass("over");
				}, 50);
				return false;
			}				
			// Space
			if (key === 32) {
				var homebu = $(".home a").attr( "href" );
				$('#container').stop().scrollTo( homebu, 500, {easing:'swing'});
		
				go_home();
			
				$(".home a").addClass("over");
				setTimeout(function() {
					$(".home a").removeClass("over");
				}, 50);
				return false;
			}	
			// X
	        if (key === 88) {
				if ($('div.help').css("opacity") != "0") {
	                $('div.help').animate({opacity: 0}, 200).slideToggle(300);
					$('div.spacer').animate({opacity: 1}, 200).animate({height: 97}, 300);
				} else {
					$('div.help').slideToggle(300).animate({opacity: 1}, 200);
					$('div.spacer').animate({height: 149}, 300);
				}
	            return false;
	        }
	        // I
	        if (key === 73) {
				if ($('div.hide-box').is(':hidden'))  {
	                $('.hide-box').css({"opacity":0,"display":"block"}).animate({'opacity': 1}, 200);
				} else {
					$('.hide-box').animate({"opacity": 0}, 200).css({"display":"none"});
				}
	            return false;
	        }
			// V
			if (key === 86) {
				$('#sb-info-inner').addClass('narrow');
				window.shadowOpen = 1;
				Shadowbox.open({
					player: ["inline"],
					content: "#dashboard",
					height: 380,
					width: 460,
					displayCounter: false
				});
	            return false;
	        }
			// ESC
			if (key === 27) {
				$('#sb-info-inner').removeClass('narrow');
	            return false;
	        }
		}
	}
});

// Nav Functions
function go_right() {
	if ((rel < (subIDs.length)) && (rel > 0) && ($("li.right a").attr("href") != "#contact")) {
		rel = ++rel;
		moveOn = (rel + 1);
		moveBack = (rel - 1);
		$("li.left a").attr({ href: '#release' + moveBack });
		if (moveOn <= (subIDs.length)) {
			$("li.right a").attr({ href: '#release' + moveOn });
		}
		if (moveOn > subIDs.length) {
			$("li.right div").fadeOut();
		}
		if (moveBack >= 1) {
			$("li.left div").fadeIn();
		}
		if (rel > 1) {
			$("li.down a").attr({ href: '#release' + rel });
			$("li.up a").attr({ href: '#release' + rel });
			$("li.down div").fadeOut();
			$("li.up div").fadeOut();
		}
	}
}
function go_left() {
	if ((rel <= (subIDs.length)) && (rel > 1)) {
		rel = --rel;
		moveOn = (rel + 1);
		moveBack = (rel - 1);
	
		if (moveBack > 0) {
			$("li.left a").attr({ href: '#release' + moveBack });
		}
		$("li.right a").attr({ href: '#release' + moveOn });

		if (moveBack <= 0) {
			$("li.left div").fadeOut();
		} 
		if (moveOn <= subIDs.length) {
			$("li.right div").fadeIn();
		}
		if (rel > 1) {
			$("li.down a").attr({ href: '#release' + rel });
			$("li.up a").attr({ href: '#release' + rel });
		}
		if (rel <= 1) {
			$("li.down a").attr({ href: '#contact' });
			$("li.up a").attr({ href: '#release' });
			$("li.down div").fadeIn();
		}
	}
}
function go_up() {
	if ($("li.up a").attr("href") == "#release") {
		$("li.down div").fadeIn();
		$("li.up div").fadeOut();
		$("li.right div").fadeIn();
		$("li.right a").attr({ href: '#release2' });
		$("li.left a").attr({ href: '#release' });
	}
}
function go_down() {
	if ($("li.down a").attr("href") == "#contact") {
		$("li.down div").fadeOut();
		$("li.up div").fadeIn();
		$("li.right div").fadeOut();
		$("li.right a").attr({ href: '#contact' });
		$("li.left a").attr({ href: '#contact' });
	}
}
function go_home() {
	rel = 1;
	sec = 1;
	$("li.left a").attr({ href: '#release1' });
	$("li.right a").attr({ href: '#release2' });
	$("li.up a").attr({ href: '#release' });
	$("li.down a").attr({ href: '#contact' });
	$("li.left div").fadeOut();
	$("li.right div").fadeIn();
	$("li.up div").fadeOut();
	$("li.down div").fadeIn();
}

// Viewport sizing setup
function stretch_portal_content() {
	$('ul#container').height($(window).height()).width($(window).width());
	$('li.section').each(function(){
		$(this).height($(window).height()).width($(window).width() * ($(this).find('li.sub-section').size()));
	});
	$('li.sub-section').height($(window).height()).width($(window).width());
}

// Disable keyboard shortcuts in text areas
$(document).keydown(function(event){
    var key = event.keyCode || event.which;
    if(event.target.type !== 'textarea'){
        
    }
});

// Check cart quantity 
function check_cart_quant() {
	fcprevquant = $('a #fc_quantity').html();
	$.getJSON('http://' + FoxyDomain + '/cart?output=json&cart=view&callback=?' + fc_AddSession(), function(data){
		fcquant = fc_json.product_count;
		if ((fcquant != fcprevquant) && (fcquant != 0)) { //if change not to 0 
			$('.minicart a').animate({opacity: 0}, 300);
			$('.empty').css("opacity", "0").hide();
		} else 	if ((fcquant != fcprevquant) && (fcquant == 0)) { //if change to 0 
			$('.minicart a').css("opacity", "0").hide();
			$('.empty').show().css("opacity", "1");
		}
		if (fcquant == 0) {
			$('.bag-link-container').animate({opacity: 0}, 200).hide(200);//Correct		
			if (fcquant == fcprevquant) {
				$('.empty').show().animate({opacity: 1}, 300);
			} else {
				$('.empty').show().animate({opacity: 0}, 300).animate({opacity: 1}, 300).animate({opacity: 0}, 300).animate({opacity: 1}, 300);
			}
		} else if (fcquant >= 1) {
			$('.bag-link-container').show(200).animate({opacity: 1}, 200);//Correct			
			if (fcquant == fcprevquant) {
				$('.minicart a').show().animate({opacity: 1}, 300);
			} else {
				$('.minicart a').show().animate({opacity: 1}, 300).animate({opacity: 0}, 300).animate({opacity: 1}, 300);
			}
		}
		if (fcquant == 1) {
			$('.ess').animate({opacity: 0}, 200).hide();
		} else {
			$('.ess').show().animate({opacity: 1}, 200);
		}
		
	});
}

// Add the FoxyCart session ID
function get_fcsid() {
	$.getJSON('http://' + FoxyDomain + '/cart?output=json&cart=view&callback=?' + fc_AddSession(), function(data) {
		fcsid = fc_json.session_id;
		$("a.foxycart").each(function () {
			var fclink = $(this).attr('href');
			if(fclink.indexOf("&fcsid=") == -1) {
				$(this).attr('href', fclink + '&fcsid=' + fcsid);
			};
		});
	});
}

// Get URL Perams
function getURLParam(strParamName) {
	var strReturn = "";
  	var strHref = window.location.href;
  	var bFound=false;
  	var cmpstring = strParamName + "=";
  	var cmplen = cmpstring.length;
  	if ( strHref.indexOf("?") > -1 ){
    	var strQueryString = strHref.substr(strHref.indexOf("?")+1);
    	var aQueryString = strQueryString.split("&");
    	for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
      		if (aQueryString[iParam].substr(0,cmplen)==cmpstring){
        		var aParam = aQueryString[iParam].split("=");
        		strReturn = aParam[1];
        		bFound=true;
        		break;
      		} 
    	}
	}
	if (bFound==false) return null;
  	return strReturn;
}

// Signed up shadowbox
function signedUp() {
	if(vperams == "yes") {
		$('#sb-info-inner').addClass('narrow');
		Shadowbox.open({
			player: "iframe",
			content: "http://www.various.co.uk/index.php?id=7#signedup",
			height: 170,
			width: 400,
			displayCounter: false,
			options: {
				onOpen: function() {
					window.shadowOpen = 1;
				},
				onClose: function() {
					window.location.replace('http://www.various.co.uk/')
				}
			}
		});
	}
}

function addCartSilent() {
	$.getJSON($('#sb-buy-me').attr('href') + '&output=json&callback=?' + fc_AddSession(), function(data){
		fc_UpdateCart(FoxyDomain);
		fc_json = data;
		check_cart_quant();
	});
	$('#sb-buy-me').animate({opacity: 0}, 300, function() {
		$(this).html("+1");
		$(this).animate({opacity: 1}, 300).animate({opacity: 0}, 300, function() {
			$(this).html("Buy");
			$(this).animate({opacity: 1}, 300);
		});
	});
	return false;
}

function boxesTotalWidth() {
	$('.boxes').each(function() {
		$sum = 0; 
		$(this).children('li').each(function() { 
			$sum += ($(this).width() + 8); 
		}); 
		$(this).width(($sum - 8)); 
	});
}

function addEvent( obj, type, fn ) {
  if ( obj.attachEvent ) {
    obj['e'+type+fn] = fn;
    obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
    obj.attachEvent( 'on'+type, obj[type+fn] );
  } else
    obj.addEventListener( type, fn, false );
}
// Temp Flash detection
function getFlashVersion(){
  // ie
  try {
    try {
      // avoid fp6 minor version lookup issues
      // see: http://blog.deconcept.com/2006/01/11/getvariable-setvariable-crash-internet-explorer-flash-6/
      var axo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.6');
      try { axo.AllowScriptAccess = 'always'; }
      catch(e) { return '6,0,0'; }
    } catch(e) {}
    return new ActiveXObject('ShockwaveFlash.ShockwaveFlash').GetVariable('$version').replace(/\D+/g, ',').match(/^,?(.+),?$/)[1];
  // other browsers
  } catch(e) {
    try {
      if(navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin){
        return (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]).description.replace(/\D+/g, ",").match(/^,?(.+),?$/)[1];
      }
    } catch(e) {}
  }
  return '0,0,0';
}
function getFlashWarning(){
	var version = getFlashVersion().split(',').shift();
	if(version < 9){
  		alert("You must have Flash player 9 or above to view this content");
	}
}
