var animationtime = 1000;

if(siteagent != 'mobile'){
	var siteorderlist = new Array();
	siteorderlist[0] = 'ourdiamonds';
	siteorderlist[1] = 'bespokecreations';
	siteorderlist[2] = 'about';
	siteorderlist[3] = 'contact';

	var geturl = window.location.pathname;
	geturl = geturl.split('/');
	geturl = geturl[(geturl.length - 1)];
	
	var lang = getUrlVars()['language'];
	if(lang != undefined){
		lang = 'language=' + lang;
	}else{
		lang = '';
	}
	
	$.each($('nav ul li'), function(){
		if(($(this).children('a').attr('href') == (base_url + geturl + '?' + lang)) || ($(this).children('a').attr('href') == (base_url + geturl)))		{
			$(this).addClass('active');
		}
	})
	if(lang.length != 0){
		lang = '&' + lang;
	}
	$.each($('nav ul li'), function(){
		$(this).addClass('lilink');
		$(this).attr('data-link', $(this).children('a').attr('href').split('?')[0])
		$(this).html($(this).children('a').html())
	})

	var currentpagenumber = $.inArray(geturl, siteorderlist);
	check_ends();
	var sectionanimating = false;
	
	
	$('nav ul li').click(function(){
		if(!sectionanimating || siteagent == 'ipad'){
			$(this).siblings().removeClass('active')
			$(this).addClass('active')
			var nexturl = $(this).attr('data-link');
			var nexturlpagenumber = nexturl.split('/');
			if(lang.length == 0){
				nexturlpagenumber = nexturlpagenumber[(nexturlpagenumber.length - 1)];
			}else{
				nexturlpagenumber = nexturlpagenumber[(nexturlpagenumber.length - 1)];
				nexturlpagenumber = nexturlpagenumber.split('?')[0];
			}
			var page = nexturlpagenumber;
			nexturlpagenumber = $.inArray(nexturlpagenumber, siteorderlist);
			
			if(siteagent == 'ipad'){
				myScroll.scrollToPage(nexturlpagenumber);
				if (typeof(window.history.pushState) == 'function') {
   	 				window.history.pushState(null, page, page);
				} else {
    				window.location.hash = '#!/' + page;
				}
			}else{
				sectionanimating = true;
				animate_sections(nexturlpagenumber, nexturl, page);
			}
		}
	})
	
	
	$('.hotspot').click(function(){
	if(($(this).attr('id') == 'righthotspot' && !$('nav ul li.active').hasClass('last')) || ($(this).attr('id') == 'lefthotspot' && !$('nav ul li.active').hasClass('first'))){
		if(!sectionanimating){
			sectionanimating = true;
			$('nav ul li.active').addClass('currentactivelink');
			if($(this).attr('id') == 'righthotspot'){
				var nexturl = $('nav ul li.active').next().attr('data-link');
				$('nav ul li.active').next().addClass('active');
			}else{
				var nexturl = $('nav ul li.active').prev().attr('data-link');
				$('nav ul li.active').prev().addClass('active');
			}
			
			$('.currentactivelink').removeClass('active');
			$('.currentactivelink').removeClass('currentactivelink');
			var nexturlpagenumber = nexturl.split('/');
			if(lang.length == 0){
				nexturlpagenumber = nexturlpagenumber[(nexturlpagenumber.length - 1)];
			}else{
				nexturlpagenumber = nexturlpagenumber[(nexturlpagenumber.length - 1)];
				nexturlpagenumber = nexturlpagenumber.split('?')[0];
			}
			var page = nexturlpagenumber;
			nexturlpagenumber = $.inArray(nexturlpagenumber, siteorderlist);
			animate_sections(nexturlpagenumber, nexturl, page);
		}
	}
	})
}
if($('html').hasClass('ie8')){
	var ie8 = true;
}else{
	var ie8 = false;
}
$(document).ready(function(){
	if(!ie8){
		app.init();
	}else{
		setTimeout(app.init(), 600)
	}
})

$(window).load(function(){
	app.init();
	app.fadein();
})

$(window).resize(function(){
	app.base();
})

var app = {
	init:function(){
		app.base();
		app.history();
	},
	fadein:function(){
		$('div#main').animate({'opacity':1}, animationtime);
	},
	base:function(){
		$.each($('.viewportheight'), function(){
			$(this).css({'height':($(window).height() - $('header').height() - $('footer').height() - parseFloat($(this).css('padding-top').split('px')[0]) - parseFloat($(this).css('padding-bottom').split('px')[0])) + 'px'})			
		})
		$.each($('.halfheight'), function(){
			$(this).css({'height':(($(window).height() - $('header').height() - $('footer').height() - parseFloat($(this).css('padding-top').split('px')[0]) - parseFloat($(this).css('padding-bottom').split('px')[0])) / 2) + 'px'})			
		})
		$.each($('.halfbrowserwidth'), function(){
			$(this).css({'width': ($(window).width() / 2) + 'px'})
		})
		$.each($('.browserwidth'), function(){
			$(this).css({'width':$(window).width() + 'px'})
		})
		$.each($('.browserheight'), function(){
			$(this).css({'height':$(window).height() + 'px'})
		})
		$.each($('.fitsize'), function(){
			var parentwidth = $(this).parent().width();
			var parentheight = $(this).parent().height();
			var parentratio = parentwidth / parentheight;			
			var thiswidth = $(this).width();
			var thisheight = $(this).height();
			var thisratio = thiswidth / thisheight;
			if(thisratio < parentratio){
				$(this).css({'position':'relative', 'width':parentwidth, 'top': ((parentheight - thisheight) / 2) + 'px', 'left':'0px', 'height': 'auto'})
			}else{
				$(this).css({'position':'relative', 'height':parentheight, 'left': ((parentwidth - thiswidth) / 2) + 'px', 'top':'0px', 'width': 'auto'})
			}
			
		})
		$.each($('.browsercentre'),function(){
			$(this).css({'left':(($(window).width() - $(this).width()) / 2) + 'px', 'top':(($(window).height() - $(this).height()) / 2) + 'px'})
		})
		$.each($('.horzcentre'),function(){
			$(this).css({'left':(($(this).parent().width() - $(this).width()) / 2) + 'px'})
		})
		$.each($('.vertcentre'),function(){
			$(this).css({'top':(($(this).parent().height() - $(this).height()) / 2) + 'px'})
		})
		$.each($('.scalefullwidth'), function(){
			$(this).css({'width':$(this).parent().width() + 'px'})
		})
		$.each($('.scalefullheight'), function(){
			$(this).css({'height':$(this).parent().height() + 'px'})
		})
		
		//$('html').css({'height': ($('div#main').height() + $('header').height() + $('footer').height()) + 'px'})
		if($(window).width() > 767){
			$('div#main').css({'height': ($(window).height() - $('header').height() - $('footer').height()) + 'px'})
		}else if($(window).width() > 481){
			$('div#main').css({'height': 'auto'})
		}
	},
	history:function(){
		if($('html').hasClass('history')){
			window.onpopstate = function(e){
				if(e.state){
					nexturl = e.state;
					var page = e.state;
					nexturlpagenumber = $.inArray(e.state, siteorderlist);
					animate_sections(nexturlpagenumber, nexturl, page);

				}else{
				}
			}
		}
	
	}
}

function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

function animate_sections(nexturlpagenumber, nexturl, page){
		$('#main').css({'width': ($(window).width() * 2) + 'px'});
		var it = 0;
		var loadervar;
		/*try{
		loadervar.abort();
		}catch(e){}*/
		if(currentpagenumber > nexturlpagenumber){
		$('#loading').show();
		$('.hotspot').hide();
			$('#main').css('left', (-$(window).width()) + 'px')
			$('#main').prepend('<div class="content-container browserwidth" style="width:'+ $(window).width() +'px; height:'+$('.content-container').height()+'px;"></div>');

			loadervar = $('#main .content-container:first-child').load(nexturl + '?ajax=true' + lang, function(){
				setTimeout('app.base()',400);
				app.base();
				if(!it){
				it = 1;
				$('#main').delay(1200).animate({'left': '0px'}, 1000, function(){
					app.base()
					
					$('#main').css({'width': 'auto', 'left':'0px'})
					if($('#main').children().length > 1){
						$('#main .content-container:last-child').remove();
					}
					sectionanimating = false;
					$('#loading').hide();
					it = 0;
					check_ends();	
					check_video();
				})
				}
			});
		}
		else if(currentpagenumber < nexturlpagenumber){
		$('#loading').show();
		$('.hotspot').hide();
			$('#main').append('<div class="content-container browserwidth" style="width:'+ $(window).width() +'px; height:'+$('.content-container').height()+'px;"></div>');
			loadervar = $('#main .content-container:last-child').load(nexturl + '?ajax=true' + lang, function(){
				setTimeout('app.base()',400);
				app.base();
				if(!it){
				it = 1;
				$('#main').delay(1200).animate({'left': (-$(window).width()) + 'px'}, 1000, function(){
					app.base();
					if($('#main').children().length > 1){
						$('#main .content-container:first-child').remove();
					}
					$('#main').css({'width': 'auto', 'left':'0px'});
					sectionanimating = false;
					$('#loading').hide();
					it = 0;
					check_ends();	
					check_video();
				})
				}
			});
		}else{
			sectionanimating = false;
		}
		if (typeof(window.history.pushState) == 'function') {
   	 		window.history.pushState(page, page, page);
		} else {
    		window.location.hash = '#!/' + page;
		}
		currentpagenumber = nexturlpagenumber;
}

function check_ends(){
	$('.hotspot').show();
	if(currentpagenumber == (siteorderlist.length - 1)){
		$('#righthotspot').hide();
	}else if(currentpagenumber == 0){
		$('#lefthotspot').hide();
	}
}

function check_video(){
	if($('video').length > 0){
		var vid = document.getElementById($('video').attr('id'));
		if(vid.paused){
			$('#loading').show();
			vid.addEventListener('play', hideTheGif, false)
		}
	}
}

function hideTheGif(){
	$('#loading').hide();
}

















