//======= Variable Settings
var slideWidth,
	howManySlides,
	howFar;
var currentSlide = 1; //Always start at 1?
var animating = false;
var globalEasing = 'easeInOutCubic';
var globalAnimSpeed = 3000;
var contactEasing = 'easeOutExpo';
var contactAnimSpeed = 1000;
var timer;
var globalTimerDuration = 10000;

$(document).ready(function() {
	//Finding values after document is loaded
	slideWidth = parseInt($('#slideshow').css('width'));
	howManySlides = $('.slide').length;
	
	$('.arrow.right').bind('click', function(event){
		slide('right');
		resetTimer(globalTimerDuration);
	});		
	$('.arrow.left').bind('click', function(event){
		slide('left');
		resetTimer(globalTimerDuration);
	});
	setTimer(globalTimerDuration);
	
	initPagePosIcons();
	
	var slideshowWidth;
	var centerPageNoWidth = parseInt($('.center-page-no').css('width'));
	var pageNoMarginRight = parseInt($('.page-no').css('margin-right'));
	
	$(window).resize(function(){
		slideshowWidth = parseInt($('#slideshow-wrapper').css('width'));
		var position = (slideshowWidth/2)-(centerPageNoWidth/2)+(pageNoMarginRight/2);
		$('.center-page-no').css('left',position);
	}).trigger('resize');
	
	howFar = parseInt($('.slide').css('width'))+parseInt($('.slide').css('margin-right'));
	
	
	
	$('#about>a').bind('mouseover', function(event){
		$(this).css('color','#000');
		$(this).parent().children('.drop-down-menu-wrapper').css('display','block');
	});
	$('#collections>a').bind('mouseover', function(event){
		$(this).css('color','#000');
		$(this).parent().children('.drop-down-menu-wrapper').css('display','block');
	});
	$('#store>a').bind('mouseover', function(event){
		$(this).css('color','#000');
		$(this).parent().children('.drop-down-menu-wrapper').css('display','block');
	});
	$('.drop-down-menu-wrapper').bind('mouseleave', function(event){
		$(this).css('display','none');
		$('#about>a').css('color','#999');
		$('#collections>a').css('color','#999');
		$('#store>a').css('color','#999');
	});
	
	
	
	$('.masonry-container-1').masonry({
	  itemSelector: '.masonry-child',
	  columnWidth:1,
	  isAnimated:true
	});
	
	$('.masonry-container-2').masonry({
	  itemSelector: '.masonry-child',
	  columnWidth:1,
	  isAnimated:true
	});
	
	//Contact Page
	
	var heightContact = parseInt($('#contact-isawan-container').css('height'));
	var heightInter = parseInt($('#contact-international-container').css('height'));
	var heightOnline = parseInt($('#contact-online-container').css('height'));
	
	$('#contact-international-container').css('height',0);
	$('#contact-online-container').css('height',0);
	
	$('#contact-from-btn').click(function(){
		$('#contact-isawan-container').stop().animate({
				height: heightContact
			}, contactAnimSpeed, contactEasing);
		$('#contact-international-container').stop().animate({
				height: 0
			}, contactAnimSpeed, contactEasing);
		$('#contact-online-container').stop().animate({
				height: 0
			}, contactAnimSpeed, contactEasing);
	});
	$('#contact-inter-btn').click(function(){
		$('#contact-isawan-container').stop().animate({
				height: 0
			}, contactAnimSpeed, contactEasing);
		$('#contact-online-container').stop().animate({
				height: 0
			}, contactAnimSpeed, contactEasing);
		$('#contact-international-container').stop().animate({
				height: heightInter
			}, contactAnimSpeed, contactEasing);
	});
	$('#contact-online-btn').click(function(){
		$('#contact-isawan-container').stop().animate({
				height: 0
			}, contactAnimSpeed, contactEasing);
		$('#contact-international-container').stop().animate({
				height: 0
			}, contactAnimSpeed, contactEasing);
		$('#contact-online-container').stop().animate({
				height: heightOnline
			}, contactAnimSpeed, contactEasing);
	});
});


//======== Functions ======
function resetToSlide(newSlide) {
	var newPos = -howFar*(newSlide-1);
	
	updatePagePosition(newSlide);
	currentSlide = newSlide;
	
	animating = true;
	$('#slider').stop().animate({
		opacity: 0
		}, globalAnimSpeed/2, globalEasing, function(){
			
			$('#slider').css('left',newPos);
			
			$('#slider').animate({
				opacity: 1
				}, globalAnimSpeed/2, globalEasing, function(){
					animating = false;
				});
		});
	
}

function slide(direction) {
	if (!animating) {
		if (direction==='right') {
			if (currentSlide < howManySlides) {
				var newSlide = currentSlide+1;
				slideTo(newSlide);
			} else {
				//slideTo(1);
				resetToSlide(1);
			}
		} else if (direction==='left') {
			if (currentSlide > 1) {
				var newSlide = currentSlide-1;
				slideTo(newSlide);
			} else {
				//slideTo(howManySlides);
				resetToSlide(howManySlides);
			}
		}
	}
}
function slideTo(newSlide) {
	if (!animating) {
		var newPos = -howFar*(newSlide-1);
		
		updatePagePosition(newSlide);
		
		animating = true;
		$('#slider').stop().animate({
			left: newPos
			}, globalAnimSpeed, globalEasing, function(){
				animating = false;
				currentSlide = newSlide;
			});
	}
}

function setTimer(timerDuration){
	timer = window.setInterval("slide('right')",timerDuration);
}
function resetTimer(timerDuration){
	clearInterval(timer);
	setTimer(timerDuration);
}

function updatePagePosition(slideNo) {
	$('.page-no').removeClass('active');
	$('.no'+slideNo).addClass('active');
}
function initPagePosIcons(){
	$('.no1').click(function(){
		slideTo(1);
		resetTimer(globalTimerDuration);
	});
	$('.no2').click(function(){
		slideTo(2);
		resetTimer(globalTimerDuration);
	});
	$('.no3').click(function(){
		slideTo(3);
		resetTimer(globalTimerDuration);
	});
	$('.no4').click(function(){
		slideTo(4);
		resetTimer(globalTimerDuration);
	});
	$('.no5').click(function(){
		slideTo(5);
		resetTimer(globalTimerDuration);
	});
	$('.no6').click(function(){
		slideTo(6);
		resetTimer(globalTimerDuration);
	});
	$('.no7').click(function(){
		slideTo(7);
		resetTimer(globalTimerDuration);
	});
	$('.no8').click(function(){
		slideTo(8);
		resetTimer(globalTimerDuration);
	});
	$('.no9').click(function(){
		slideTo(9);
		resetTimer(globalTimerDuration);
	});
	$('.no10').click(function(){
		slideTo(10);
		resetTimer(globalTimerDuration);
	});
	$('.no11').click(function(){
		slideTo(11);
		resetTimer(globalTimerDuration);
	});
	$('.no12').click(function(){
		slideTo(12);
		resetTimer(globalTimerDuration);
	});
	$('.no13').click(function(){
		slideTo(13);
		resetTimer(globalTimerDuration);
	});
	$('.no14').click(function(){
		slideTo(14);
		resetTimer(globalTimerDuration);
	});
	$('.no15').click(function(){
		slideTo(15);
		resetTimer(globalTimerDuration);
	});
	$('.no16').click(function(){
		slideTo(16);
		resetTimer(globalTimerDuration);
	});
	$('.no17').click(function(){
		slideTo(17);
		resetTimer(globalTimerDuration);
	});
	$('.no18').click(function(){
		slideTo(18);
		resetTimer(globalTimerDuration);
	});
	$('.no19').click(function(){
		slideTo(19);
		resetTimer(globalTimerDuration);
	});
	$('.no20').click(function(){
		slideTo(20);
		resetTimer(globalTimerDuration);
	});
	$('.no21').click(function(){
		slideTo(21);
		resetTimer(globalTimerDuration);
	});
	$('.no22').click(function(){
		slideTo(22);
		resetTimer(globalTimerDuration);
	});
	$('.no23').click(function(){
		slideTo(23);
		resetTimer(globalTimerDuration);
	});
	$('.no24').click(function(){
		slideTo(24);
		resetTimer(globalTimerDuration);
	});
	$('.no25').click(function(){
		slideTo(25);
		resetTimer(globalTimerDuration);
	});
	$('.no26').click(function(){
		slideTo(26);
		resetTimer(globalTimerDuration);
	});
	$('.no27').click(function(){
		slideTo(27);
		resetTimer(globalTimerDuration);
	});
	$('.no28').click(function(){
		slideTo(28);
		resetTimer(globalTimerDuration);
	});
	$('.no29').click(function(){
		slideTo(29);
		resetTimer(globalTimerDuration);
	});
	$('.no30').click(function(){
		slideTo(30);
		resetTimer(globalTimerDuration);
	});
}
