/*================================================
-Main.js Document;
================================================*/

var globalSettings = {
	videoPlayerHeight: 406,
	videoPlayerWidth: 720,
	currentVideo: null,
	openStatus: false,
	colorData: "",
	smallImageName: "-150x150",
	currentColorName: "",
	nextSlide: null,
	prevSlide: null,
	contentOffset: 900,
	activeMenu: null,
	activeItem: null,
	currentLink: null
};


var globalFunction = {

	SetVideo: function(link){		
		
		if($(link).parents('.video-element').length > 0){
			if($(link).parents('.video-element').find('.video-data').attr('href').length > 0){
				globalSettings.currentVideo = $(link).parents('.video-element').find('.video-data').attr('href');
			}
			else{
				globalSettings.currentVideo = null;
			}
		}
		else if($(link).parents('.seasonal-trumb').length > 0){
			if($(link).find('input[type="hidden"]').attr('value').length > 0){
				globalSettings.currentVideo = $(link).find('input[type="hidden"]').attr('value');
			}
			else{
				globalSettings.currentVideo = null;
			}
		}
		else{
			globalSettings.currentVideo = null;
		}

	},


	ShowPlayButton: function(){

		if(globalSettings.currentVideo == null){
			$('.play-video-popup').addClass('hide-play-button');
		}

	},

	AddColorBox: function(imagePath){

		if((globalSettings.colorData).length > 0){
			$('.color-box').remove();
			$('.highslide-wrapper').append('<div class="color-box"><ul>'+ globalSettings.colorData +'</ul></div><span class="color-name">' + globalSettings.currentColorName + '</span>');
		}

		else{
			this.GetPopUpImages();
		}

	},

	GetPopUpImages: function(){

		$('.seasonal-box').each(function(){
			var currentImage = $(this).find('a').attr('href');
			var smallImage = currentImage.substring(0, currentImage.lastIndexOf('.')) + globalSettings.smallImageName + currentImage.substring(currentImage.lastIndexOf('.'), currentImage.length);
			globalSettings.colorData += '<li><a href="'+ currentImage +'" class="leather-color"><img src="' + currentImage + '" alt="#"/></a></li>';
		});

		$('.highslide-wrapper').append('<div class="color-box"><ul>'+ globalSettings.colorData +'</ul></div><span class="color-name">' + globalSettings.currentColorName + '</span>');

	},

	InitSlideValue: function(){

		if($('.next_posts').length > 0){
			globalSettings.nextSlide = $('.next_posts').attr('href');
			globalSettings.prevSlide = $('.arrow-prew-page').attr('href');
		}

		else if($('.prev_posts').length > 0){
			globalSettings.prevSlide = $('.prev_posts').attr('href');
			globalSettings.nextSlide = $('.arrow-page').attr('href');
		}

		else if($('.next_posts').length > 0 && $('.prev_posts').length > 0){
			globalSettings.nextSlide = $('.next_posts').attr('href');
			globalSettings.prevSlide = $('.prev_posts').attr('href');
		}

		else{
			globalSettings.nextSlide = $('.arrow-page').attr('href');
			globalSettings.prevSlide = $('.arrow-prew-page').attr('href');
		}

	},

	ContentAnimation: function(){
		$('.content-wrapper-content').animate({
			'left': globalSettings.contentOffset * (-1),
			'opacity': 0
		}, 800);
	},

	LoadDataContent: function(status){

		$('#load-content').addClass('old-content');

		if(status == 'next'){
			var request = globalSettings.nextSlide + ' #load-content';
			$('.content-wrapper').append('<div class="load-content-right"></div>');
			$('.load-content-right').load(request, function(){
				$(this).delay(1000).animate({
					'right': 0
				}, 800, function(){
					globalFunction.ResetUI();
					globalFunction.ReloadScript();
					globalFunction.AddVideo();
				});
			});
		}

		else if(status == 'previous'){
			var request = globalSettings.prevSlide + ' #load-content';
			$('.content-wrapper').append('<div class="load-content-left"></div>');
			$('.load-content-left').load(request, function(){
				$(this).delay(1000).animate({
					'left': 0
				}, 800, function(){
					globalFunction.ResetUI();
					globalFunction.ReloadScript();
					globalFunction.AddVideo();
				});
			});
		}
	},

	ResetUI: function(){
		$('.arrow-page').fadeIn('500');
		$('.arrow-prew-page').fadeIn('500');
		$('.load-content-right').css({
			'position':'relative'
		});
		$('.old-content').remove();
		this.InitSlideValue();
		$('#load-content').unwrap();
		$('#load-content').wrapInner('<div class="load-wrapper"></div>');
	},

	ReloadScript: function(){
		$.getScript(theme_dir+'/js/main-reload.js');
	},

	ActiveLinkChange: function(controlAction){
		
		globalSettings.activeMenu = $('#nav a.active').parent().attr('class');
		globalSettings.activeItem = $('.'+ globalSettings.activeMenu + ' a.current-item').parent().index();
		$('a.current-item').removeClass('current-item');
		if(controlAction == 'right'){
			globalSettings.activeItem += 1;
			$('.'+ globalSettings.activeMenu).find('li:eq('+ globalSettings.activeItem +') a').addClass('current-item');
			globalFunction.HashChanger();
		}

		else if(controlAction == 'left'){
			globalSettings.activeItem -= 1;
			$('.'+ globalSettings.activeMenu).find('li:eq('+ globalSettings.activeItem +') a').addClass('current-item');
			globalFunction.HashChanger();
		}
	
	},

	HashChecker: function(){

		if(window.location.hash != ''){
			this.GetContentByHash();
		}
	},

	GetContentByHash: function(){

		var hashData = window.location.hash.substr(1, window.location.hash.length);
		var currenURL = $('#nav a[ref="'+ hashData +'"]').attr('href') + ' #load-content';
		$('#nav a').removeClass('current-item');
		$('#nav a[ref="'+ hashData +'"]').addClass('current-item');
		$('#nav a[ref="'+ hashData +'"]').parents('ul').parents('li').find('a').first().addClass('active');
		
		$('#load-content').html('');
		$('#load-content').load(currenURL, function(){
			$('.content-wrapper-content .content-wrapper-content').unwrap();
			$('#load-content').wrapInner('<div class="load-wrapper"></div>');
			$('.arrow-page').fadeIn('500');
			$('.arrow-prew-page').fadeIn('500');
			globalFunction.InitSlideValue();
			globalFunction.ReloadScript();
			globalFunction.AddVideo();
		});

	},

	HashChanger: function(){
		if($('a.current-item').length > 0){
			window.location.hash = $('#nav a.current-item').attr('ref');
		}
		else{
			//console.log($('#nav a[href='+ globalSettings.currentLink +']'));
			window.location.hash = $('#nav a[href='+ globalSettings.currentLink +']').attr('ref');
		}
	},
	
	AddVideo: function(){
		if($('.video-content').length > 0){
			jwplayer("container").setup({
				flashplayer: $('input[name="flashplayer"]').attr('value'),
				file: $('input[name="file"]').attr('value'),
				image: $('input[name="image"]').attr('value'),
				skin: $('input[name="skin"]').attr('value'),
				autostart: true
			}); 
		}		
	},
	
	ChangeImage: function(newImage, newURL){
		$('.col-right .timeline-link img').fadeOut(300, function(){
			$('.col-right .timeline-link img').remove();
			$('.col-right .timeline-link').append('<img src="' + newImage + '" alt="#"/>');
			$('.col-right .timeline-link').attr('href', newURL);
		});
	}

};

$(document).ready(function(){


	if($('.video-content').length == 0){
		$('#load-content').wrapInner('<div class="load-wrapper"></div>');
	}

	$('.circle').last().addClass('circle-last');

	globalFunction.HashChecker();
	globalFunction.InitSlideValue();

	$('.images-row').each(function(){
		$(this).find('.image-item:last').addClass('last-image-row');
	});

	$('.links li:first').addClass('link-border');
        $('.widget-side:last').addClass('widget-side-last');
        $('.content-thumbs .thumb:last').addClass('content-thumbs-last');

	$('#nav li').not('#nav li li').each(function(index){
		$(this).addClass('nav-item-' + index);
	});

	$('.sub-top').each(function(){
		$(this).find('li:last').addClass('last-list-element');
	});

	if($('.con-img').length > 0){
		$('.con-img').prettyPhoto();
	}

	if($('.popup-slider').length > 0){
		$('.arrow-prew-page').addClass('arrow-left-popup');
	}

	if(typeof hs != 'undefined'){
		hs.graphicsDir = theme_dir+'/css/graphics/';
		hs.align = 'center';
		hs.transitions = ['expand', 'crossfade'];
		hs.wrapperClassName = 'dark borderless floating-caption';
		hs.fadeInOut = true;
		hs.dimmingOpacity = .75;

		if (hs.addSlideshow) hs.addSlideshow({
			interval: 5000,
			repeat: false,
			useControls: true,
			fixedControls:true,
			overlayOptions: {
				opacity: .1,
				position: 'bottom center',
				hideOnMouseOut: false
			}
		});

		hs.Expander.prototype.onBeforeExpand = function (sender, e) {

			globalFunction.ShowPlayButton();

			if(globalSettings.openStatus == true){
				globalFunction.AddColorBox();
			}
		}

		hs.Expander.prototype.onAfterClose = function (sender, e) {
			if(globalSettings.openStatus == true){
				globalSettings.openStatus = false;
			}
		}
	}

	$('.zoom-button').click(function(event){
		return hs.expand(null, {
			src: $('#slider li').find('a').attr('href')
		});
	});

	$('.open-popup').click(function(event){
		globalFunction.SetVideo($(this));
		var imagePath = $(this).parents('.video-element').find('.video-image').attr('href');
		return hs.expand(null, {
			src: imagePath
		});
	});
	
	$('.col-right .popup').click(function(){
		return hs.expand(null, {
			src: $('.col-right .timeline-link').attr('href')
		});
	});
	
	$('.col-left .popup').click(function(){
		return hs.expand(null, {
			src: $('.col-left .timeline-link').attr('href')
		});
	});

	$('a.highslide').each(function() {
		this.onclick = function() {
			return hs.expand(this);
		};
	});

	$('a.slide-popup').each(function() {
		this.onclick = function() {
			return hs.expand(this);
		};
	});

	$('.video-element .video-image').each(function() {
		this.onclick = function() {
			return hs.expand(this);
		};
	});


	$('.timeline-link').each(function() {
		this.onclick = function() {
			return hs.expand(this);
		};
	});

	$('.video-image').click(function(){
		globalFunction.SetVideo($(this));
	});

	$('.seasonal-popup').click(function(event){
		globalSettings.colorData = "";
		event.preventDefault();
		globalSettings.openStatus = true;
		globalSettings.currentColorName = $(this).parents('.seasonal-box').find('span').text();
		hs.expand(null, {src:$(this).attr('href')});
	});

	$('.leather-color').live('click', function(event){
		event.preventDefault();
		globalSettings.currentColorName = $('.seasonal-box').eq($(this).parents('li').index()).find('span').text();
		hs.close(this);
		hs.expand(null, {src:$(this).attr('href')});
		globalSettings.openStatus = true;
	});

	$('.play-video-popup').live('click' ,function(event){
		event.preventDefault();
		hs.close(this);
		$.prettyPhoto.open(globalSettings.currentVideo);
		/*return hs.htmlExpand(null, {
			contentId: 'video-container'
		});*/
	});
	
	$('.hot-shop-video').live('click' ,function(event){
		event.preventDefault();
		$.prettyPhoto.open($(this).attr('href'));
	});

	$('.slide-popup-zoom').click(function(){
		return hs.expand(null, {
			src: $('.slide-popup').attr('href')
		});
	});

	$('.zoom-timeline').click(function(){
		return hs.expand(null, {
			src: $('.timeline-link').attr('href')
		});
	});

	$('.left-article-box').height($('.article-box').height());

	/**/
	$('.arrow-page').live('click', function(event){
		event.preventDefault();
		globalSettings.currentLink = $(this).attr('href');
		$('.arrow-prew-page').fadeOut('300');
		$(this).fadeOut('300', function(){
			globalFunction.ContentAnimation();
			globalFunction.LoadDataContent('next');

			if($('.next_posts').length < 1){
				globalFunction.ActiveLinkChange('right');
			}

		});
	});

	$('.arrow-prew-page').live('click', function(event){
		event.preventDefault();
		globalSettings.currentLink = $(this).attr('href');
		$('.arrow-page').fadeOut('300');
		$(this).fadeOut('300', function(){
			globalFunction.ContentAnimation();
			globalFunction.LoadDataContent('previous');

			if($('.prev_posts').length < 1){
				globalFunction.ActiveLinkChange('left');
			}

		});
	});

	$('.seasonal-box img').click(function(){
		globalSettings.colorData = "";
		globalSettings.openStatus = true;
		globalSettings.currentColorName = $(this).parents('.seasonal-box').find('span').text();
		return hs.expand(null, {
			src: $(this).parent().find('a').attr('href')
		});
	});

	if($('.video-content').length > 0){
		$('.main-content').addClass('video-content-wrapper');
	}
	
	
	//$('.min-width').find('.video-content-wrapper').removeClass('video-content-wrapper');
	//new code
	$('.seasonal-trumb a').click(function(event){
		event.preventDefault();
		globalFunction.SetVideo($(this));
		globalFunction.ChangeImage($(this).find('img').attr('src'), $(this).attr('href'));
		$('.seasonal-trumb a').removeClass('active');
		$(this).addClass('active');
	});
	
	if($('.seasonal-trumb .active input[type="hidden"]').length > 0){
		if($('.seasonal-trumb .active input[type="hidden"]').attr('value').length > 0){
			globalSettings.currentVideo = $('.seasonal-trumb .active input[type="hidden"]').attr('value');
		}
	}
	
	if($(window).height() <= 768){
		$('.video-content').css('padding-bottom', '32.25%');
	}
	else{
		$('.video-content').css('padding-bottom', '42.25%');
	}
	
	if($('.current-item').length == 0){
		$('#content').addClass('min-width');
		$('.video-content-wrapper').removeClass('video-content-wrapper');
	}
	else{
		if($('.video-content').length == 0){
			$('#content').addClass('content-pages');
		}
	}
	
	if(($('.nav-item-2 a:first').hasClass('active')) && ($('.video-content').lenght > 0)){
		$('content').addClass('min-video-width');
	}
	
	$('.seasonal-trumb-holder').css({
		'margin-right': ((885 / 2) - ($('.seasonal-trumb-holder').width() / 2))
	});
	
	if($('.thumb-row .thumb').length < 4){
		$('.content-thumbs').width($('.thumb-row .thumb').length * $('.thumb-row .thumb').outerWidth());
	}
	
});

$(window).load(function(){
	
	if($(window).height() <= 768){
		$('.video-content').css('padding-bottom', '32.25%');
	}
	else{
		$('.video-content').css('padding-bottom', '42.25%');
	}
	
	$('.left-article-box').height($('.article-box').height() - 26);
	$('.arrow-page').fadeIn('2000');
	$('.arrow-prew-page').fadeIn('2000');
	
	if($('.popup-slider').length > 0){
		$('.highslide-container').addClass('colour-flow-popup');
	}

	//open page in new tab
	$(function() {
		$('a[rel*=external]').click( function() {
			window.open(this.href);
			return false;
		});
	});

});

