// jquery backstrech un-modified
(function($){$.backstretch=function(src,options,callback){var settings={centeredX:true,centeredY:true,speed:0},rootElement=("onorientationchange"in window)?$(document):$(window),imgRatio,bgImg,bgWidth,bgHeight,bgOffset,bgCSS;if(options&&typeof options=="object")$.extend(settings,options);$(document).ready(_init);return this;function _init(){if(src){var container=$("<div />").attr("id","backstretch").css({left:0,top:0,position:"fixed",overflow:"hidden",zIndex:-9999}),img=$("<img />").css({position:"relative",display:"none"}).bind("load",function(e){var self=$(this);imgRatio=$(e.target).width()/$(e.target).height();_adjustBG(function(){self.fadeIn(settings.speed,function(){if(typeof callback=="function")callback();});});}).appendTo(container);$("body").prepend(container);img.attr("src",src);$(window).resize(_adjustBG);}} function _adjustBG(fn){try{bgCSS={left:0,top:0}
bgWidth=rootElement.width();bgHeight=bgWidth/imgRatio;if(bgHeight>=rootElement.height()){bgOffset=(bgHeight-rootElement.height())/2;if(settings.centeredY)$.extend(bgCSS,{top:"-"+bgOffset+"px"});}else{bgHeight=rootElement.height();bgWidth=bgHeight*imgRatio;bgOffset=(bgWidth-rootElement.width())/2;if(settings.centeredX)$.extend(bgCSS,{left:"-"+bgOffset+"px"});}
$("#backstretch img").width(bgWidth).height(bgHeight).css(bgCSS);}catch(err){}
if(typeof fn=="function")fn();}};})(jQuery);

// begin on DOM ready
$(document).ready(function() {	
detectBackground();
footerDetect();

// landing page logo vertical center
var winHeight = $(window).height();
$('.view-landing-page').css('padding-top', winHeight / 3);
$(window).resize(function() {	
	footerDetect();	
});
	
// position main content center screen
var winWidth = $(window).width() - 700;
$('.not-front #holder').css('width', winWidth);

// scroll text up and down
if ($('#scroll-content, .view-stockists-aus .view-content, .view-stockists-int .view-content').height() <= 513){ 
	$('#up, #down').css('display', 'none');
} else {
	$('#down').css('display', 'block');
	$('#up').css('display', 'none');
}
$('#up').mousedown(function() {
	scrollInt = setInterval('scrollUp();', 300);	
	scrollUp(); 	
});
$('#down').mousedown(function() {
	scrollInt = setInterval('scrollDown();', 300); 	  
	scrollDown(); 
});
$('#down, #up').mouseup(function() {
	clearInterval (scrollInt); 	
});

// collection navigation
$('.southernhemisphere a:first, .northernhemisphere a:first, .global a:first').css('display','none'); 
var hemisphere = $('#hemisphere').text();
var southernLength = $('.southernhemisphere ul li').length;
if (hemisphere == 'south') {
	$('.northernhemisphere').css('display','none');
	var latestCollection = $('.southernhemisphere li:first a').attr('href');
	$('.collections a').attr('href', latestCollection);	
} else {
	$('.southernhemisphere').css('display','none');
	var latestCollection = $('.northernhemisphere li:first a').attr('href');
	$('.collections a').attr('href', latestCollection);
}
if (southernLength < 1) {
	var latestCollection = $('.global li:first a').attr('href');
	$('.collections a').attr('href', latestCollection);
}
var collectionSubHeight = $('#block-menu-menu-collections').height() + 16;
if ($('body').is('.node-type-collection-p, .node-type-collection')) {
	$('#block-menu-menu-collections').css('display', 'block');
	$('.collections').css('border-bottom', '1px solid #818181');	
	$('.collections').css('margin-bottom',collectionSubHeight);
	$('.collections a').addClass('active');
	$('#block-menu-menu-collections').css('display','block');
}

// contact hemisphere detection
var hemisphere = $('#hemisphere').text();
if (hemisphere == 'north') {	
	$('.contactus').css('display','none');	
} else {
	// $('.contactus a').attr('href', 'http://www.seventhwonderland.com/contact-us-international');
	$('.contactinternational').css('display','none');
}

// video page content height
if ($('.node-type-collection iframe').length) { 
	$('.node-type-collection article').css('height', '460px');
}	

// collection and editorial galleries
// thumbs goto full view
$('#gallery-thumbs .thumb, .view-editorial .thumb').css('display','block');
$('#gallery-thumbs .thumb, .view-editorial .thumb').click(function() {			
	// thumbs
	var thumbNumber = $('#gallery-thumbs .thumb, .view-editorial .thumb').index(this);	
	var thumbAmmended = thumbNumber + 1;
	$('#gallery-full li, .view-editorial .views-row').eq(thumbNumber).fadeIn(300, function () {
    	$('#gallery-full li, .view-editorial .views-row').eq(thumbNumber).addClass('active');
    });	
	$('#gallery-thumbs, #thumb-nav, .view-editorial .thumb').css('display','none');		
	// full		
	var collectionTotal = $('#gallery-thumbs .thumb, .view-editorial .thumb').length;	
	$('#collection-pages').append('<li><span>' + thumbAmmended + '</span> of ' + collectionTotal +'</li>');	
	$('<div id="collection-nav"><a id="collection-prev">Prev</a> <a id="collection-next">Next</a></div>').insertAfter('article');
	if (thumbNumber == 0) {
		$('#collection-prev').css('display','none');			
	} else if (thumbNumber == collectionTotal - 1) {
		$('#collection-next').css('display','none');		
	} else {
		$('#collection-prev, #collection-next').css('display','block');		
	}
	$('#collection-pages').css('display','block');	
	$('.page-editorial article, .node-type-collection-p article').css('width','362px');
	$('.node-type-collection-p article').css('height','557px');
	// position next and prev links
	var contentPosition = $('article').offset();
	var positionLeft = contentPosition.left;	
	$('.node-type-collection #collection-prev, .page-editorial #collection-prev, .node-type-collection-p #collection-prev').css('left', positionLeft - 393);
	$('.node-type-collection #collection-next').css('left', positionLeft + 460);
	$('.page-editorial #collection-next, .node-type-collection-p #collection-next').css('left', positionLeft + 6);
	$(window).resize(function() {
		var contentPosition = $('article').offset();
		var positionLeft = contentPosition.left;
		$('.node-type-collection #collection-prev, .page-editorial #collection-prev, .node-type-collection-p #collection-prev').css('left', positionLeft - 393);
		$('.node-type-collection #collection-next').css('left', positionLeft + 460);
		$('.page-editorial #collection-next, .node-type-collection-p #collection-next').css('left', positionLeft + 7);
	});	
	var cnt = $('.page-editorial .thumb-page').contents();
	$('.page-editorial .thumb-page').replaceWith(cnt);
});

// thumb paging
function wrap(elements, wrap, size) { 
  var x, i = 0, c = -1, l = elements.length, n = [];
  for(i; i < l; i++){
    (x = i % size) ? n[c][x] = elements[i] : n[++c] = [elements[i]];
  }
  l = n.length;
  for(i = 0; i < l; i++) {
    $(n[i]).wrapAll(wrap);
  }
}
wrap($('.node-type-collection #gallery-thumbs .thumb'), '<div class="thumb-page"></div>', 9);
wrap($('.view-editorial .views-row'), '<div class="thumb-page"></div>', 8);
wrap($('.node-type-collection-p #gallery-thumbs .thumb'), '<div class="thumb-page"></div>', 8);
$('.thumb-page:first').addClass('active');
$('.node-type-collection .thumb:nth-child(3), .node-type-collection .thumb:nth-child(6), .node-type-collection .thumb:nth-child(9)').css('margin-right','0px');
$('.view-editorial .thumb-page .views-row:nth-child(4), .view-editorial .thumb-page .views-row:nth-child(8)').addClass('end');
$('.node-type-collection-p .thumb:nth-child(4), .node-type-collection-p .thumb:nth-child(8)').css('margin-right','0px');

// thumb prev and next
$('#thumb-next').live('click', function() {		
	$('.thumb-page.active').fadeOut(300, function () {  		 
    	$('.thumb-page.active').next().addClass('active').fadeIn(300);
    	 edTitle();	  	
    	$('.thumb-page.active').prev().removeClass('active');
    	$('#thumb-prev').css('display','block');
    	if ($('.thumb-page:last').hasClass('active')) {
			$('#thumb-next').css('display','none');			
		} 
    });	
   
});
$('#thumb-prev').live('click', function() {		
	$('.thumb-page.active').fadeOut(300, function () {   
    	$('.thumb-page.active').prev().addClass('active').fadeIn(300);
    	edTitle();	
		$('.thumb-page.active').next().removeClass('active');
		$('#thumb-next').css('display','block');
		if ($('.thumb-page:first').hasClass('active')) {
			$('#thumb-prev').css('display','none');
		}
    });		
});
// display of prev and next for thumb paging
var thumbTotal = $('#gallery-thumbs div').length;
if (thumbTotal < 9) {	
	$('#thumb-nav').css('display','none');
} else {
	if (thumbTotal > 9) {
	$('<div id="thumb-nav"><a id="thumb-prev">Prev</a> <a id="thumb-next">Next</a></div>').insertAfter('article');
	}
}
var editorialTotal = $('.view-editorial .views-row').length;
if (editorialTotal < 8 && $('body').hasClass('.page-editorial')) {	
	$('#thumb-nav').css('display','none');
} else {
	if (editorialTotal > 8) {
		$('<div id="thumb-nav"><a id="thumb-prev">Prev</a> <a id="thumb-next">Next</a></div>').insertAfter('article');
	}
}

// prev and next buttons
$('#collection-prev').live('click', function() {
	$('#collection-next').css('display','block');
	var slideNumber = $('#gallery-full li.active, .view-editorial .views-row.active').index();		
	if (slideNumber == 1) {		
		$('#collection-prev').css('display','none');
	}
	$('#collection-pages li.active').prev().addClass('active').next().removeClass('active');
	var currentImg = parseFloat($('#collection-pages li span').text());
	var printImg = currentImg - 1;	
	$('#collection-pages li span').text(printImg);
	$('#gallery-full li.active, .view-editorial .views-row.active').fadeOut(300, function () {
    	$('#gallery-full li.active, .view-editorial .views-row.active').prev().fadeIn(300).addClass('active');
    	$('#gallery-full li.active, .view-editorial .views-row.active').next().removeClass('active');	
    });		
});
$('#collection-next').live('click', function() {
	$('#collection-prev').css('display','block');
	var slideNumber = $('#gallery-full li.active, .view-editorial .views-row.active').index();		
	var slideLast = $('#gallery-full li, .view-editorial .views-row').length;	
	if (slideNumber == slideLast - 2) {		
		$('#collection-next').css('display','none');
	}	
	$('#collection-pages li.active').next().addClass('active').prev().removeClass('active');
	var currentImg = parseFloat($('#collection-pages li span').text());
	var printImg = currentImg + 1;	
	$('#collection-pages li span').text(printImg);
	$('#gallery-full li.active, .view-editorial .views-row.active').fadeOut(300, function () {
    	$('#gallery-full li.active, .view-editorial .views-row.active').next().fadeIn(300).addClass('active');
    	$('#gallery-full li.active, .view-editorial .views-row.active').prev().removeClass('active');	
    });				
});

// stockists
$('.view-stockists-int .views-row, .view-stockists-aus .views-row').mouseover(function() {	
	$(this).children('div').css('display', 'block');
});	
$('.view-stockists-int .views-row, .view-stockists-aus .views-row').mouseout(function() {	
	$(this).children('div').css('display', 'none');
});	


$('.expanded.australia a:first').addClass('state-active');

// contact us
$('#nid-11 #scroll-content p:first').addClass('first'); 

// position thumb next and prev links
var contentPosition = $('article').offset();
var positionLeft = contentPosition.left;
$('#thumb-prev').css('left', positionLeft - 393);
$('#thumb-next').css('left', positionLeft + 460);
$(window).resize(function() {
	var contentPosition = $('article').offset();
	var positionLeft = contentPosition.left;
	$('#thumb-prev').css('left', positionLeft - 392);
	$('#thumb-next').css('left', positionLeft + 460);
});

edTitle();

// end DOM ready
});

// begin functions outside DOM ready
// full screen background

function detectBackground() { 
	if ($('body').hasClass('front')) {			
		setInterval('switchBackground();', 4500); 
		$('.view-landing-page .item-list li:first').addClass('active');
		var landingBg = $('.view-landing-page .item-list li:first').text();		
		$.backstretch(landingBg, {speed: 800});		
	} else {
		var globalBg = $('.views-field-field-global-bg-fid span').text();
		$.backstretch(globalBg);		
	}	
}
function switchBackground() {	
	if ($('.view-landing-page .item-list li:last').hasClass('active')) {
		$('.view-landing-page .item-list li:last').removeClass('active');
		$('.view-landing-page .item-list li:first').addClass('active');	
		$('#backstretch img').fadeOut(600, function () {
			var landingBg = $('.view-landing-page .item-list li.active').text();		
			$('#backstretch img').attr('src', landingBg);					
		});					  	
	} else {
		$('.view-landing-page .item-list li.active').next().addClass('active').prev().removeClass('active');
		var landingBg = $('.view-landing-page .item-list li.active').text();		
		$('#backstretch img').fadeOut(600, function () {
			var landingBg = $('.view-landing-page .item-list li.active').text();		
			$('#backstretch img').attr('src', landingBg);						 
		});									
	}
}

// editorial title - add dots
function edTitle () {
$('.editorial-title').each(function () {
	var edWidth = $(this).width();
	if (edWidth > 110) {
		$('<span class="dots">...</span>').insertAfter(this);	
		$(this).css('width','105px');
	}
});
}

// scrolling text box
function scrollUp () {
	var scrollPosition = $('#scroll-content, .view-stockists-aus .view-content, .view-stockists-int .view-content').css('top');
	$('#down').css('display','block');	
	if (scrollPosition >= '0px') {
		clearInterval (scrollInt);
		$('#up').css('display','none');
	} else {	
		$('#scroll-content, .view-stockists-aus .view-content, .view-stockists-int .view-content').animate({ 
	 		top: '+=25'  
		}, 300, 'linear', function() { });		
	}
}
function scrollDown () {		
	var scrollPosition = parseFloat($('#scroll-content, .view-stockists-aus .view-content, .view-stockists-int .view-content').css('bottom')) - 0;
	var scrollHeight = $('#scroll-content, .view-stockists-aus .view-content, .view-stockists-int .view-content').height() - 513;	
	$('#up').css('display','block'); 	
	if (scrollHeight <= scrollPosition) {		
		$('#down').css('display','none');
		clearInterval (scrollInt);
	} else {
		$('#scroll-content, .view-stockists-aus .view-content, .view-stockists-int .view-content').animate({ 
	 		top: '-=25'  
		}, 300, 'linear', function() { });		
	}	
}

// postion footer based on screen size
function footerDetect () {
	var winHeight = $(window).height();	
	var docHeight = $(window).height();	
	$('#overlay').css('height', docHeight);
	$('.view-landing-page').css('padding-top', winHeight / 3);
	if(winHeight < 950) {
		$('footer').addClass('small-screen').removeClass('full-screen');		
	} else {
		$('footer').addClass('full-screen').removeClass('small-screen');	
			
	}
	var winWidth = $(window).width() - 700;
	$('.not-front #holder').css('width', winWidth);		
}


/* function hashDetect () {
	// detect # in url and reset gallery	
	var getUrl = $(location).attr('href');
	var checkUrl = getUrl.indexOf('#') != -1;
	if (checkUrl == true) {	
		//console.log('hash = true');		
	} else {
		// if no hash in URL	
		$('#gallery-thumbs, #thumb-nav, .view-editorial .thumb').css('display','block');
		$('#gallery-full li, .view-editorial .full, #collection-prev, #collection-next, #collection-pages').css('display','none');			
		$('#collection-prev, #collection-next, #collection-pages li').remove();		
	}			
} */
