var contentWidth = 1100;
var slideDuration = 1000;
var slideContentOfftimeAnimation = 900;
var subSliderPauseBetweenSlides = 6000;
var currentSlide;
var tweetBoxTimeout;

function scaleImage(orig, size, elem) {
	var imgW = orig[0],
		imgH = orig[1],
		destW = size[0],
		destH = size[1],
		ratioX, ratioY, ratio, newWidth, newHeight, marginLeft, marginTop;
	
	// calculate scale ratios
	ratioX = destW / imgW;
	ratioY = destH / imgH;

	// fill
	ratio = ratioX > ratioY ? ratioX : ratioY;

	// calculate our new image dimensions
	newWidth = parseInt(imgW * ratio, 10);
	newHeight = parseInt(imgH * ratio, 10);

	marginLeft = Math.floor((destW - newWidth) / 2);
	marginTop = Math.floor((destH - newHeight) / 2);
	
	// Set new dimensions to both css and img's attributes
	elem.css({
		"width": newWidth,
		"height": newHeight,
		"left": marginLeft,
		"top": marginTop
	});
	
	return [newWidth,newHeight,marginLeft,marginTop]
}

function resizeSlides() {
	var bHeight = $('body').height()-165;
	if (bHeight<670) { //670 = 835 - 165 (840 min-height)
		if ($.browser.msie && $.browser.version.substr(0,1)<=7) {	
			$('html').css('overflow-y','scroll');
		} else {
			$('body').css('overflow-y','scroll');
		}
		bHeight = 670;
	} else {
		if ($.browser.msie && $.browser.version.substr(0,1)<=7) {	
			$('html').css('overflow-y','hidden');
		} else {
			$('body').css('overflow-y','hidden');
		}
	}
	var bWidth = $('body').width();
	if (bWidth > contentWidth) {
		destWidth = bWidth;
	} else {
		destWidth = contentWidth;
	}
	$('.main,#slide-container,.sub-slide-container').width(destWidth * ($('.slide').length));
	$('.main,#slide-container,.sub-slide-container').height(bHeight);
	$('.slide-main-column').height(bHeight);
	
	$('.slide,.sub-slide').width(destWidth);
	$('.slide,.sub-slide').height(bHeight);
	
	$('.slide-back').each(function() {
		var origW = 0; var origH = 0; var tt;
		if ($(this).attr("class") != undefined) {
			var classes = $(this).attr("class").split(" ");
			for (var i = 0; i < classes.length; i++){
				if (classes[i].substr(0,5) == "size-"){
					tt = classes[i].split("-");
					origW = tt[1];
					origH = tt[2];
				}
			}
			scaleImage([origW,origH], [destWidth,bHeight], $(this));
		}
	});
	
	//resize vertical cycler
	$('.sub-slide-container').cycle('resize');
	
	//reposition my slider
	if (currentSlide == undefined) {
		var left = 0;
	} else {
		var left = (-1 * currentSlide * destWidth)
	}
	$('#slide-container').css('left',left + 'px');
	$('#slide-container .slide .slide-content').each(function() {
		$(this).css('left',parseInt(($(this).parent().width() - $(this).width())/2 + (contentWidth/2)) + 'px');
	});
}

//sub-slide vertical off-time efects
function beforeSubSlide(curr,next,opts) {
	if (curr!=next) {
		$(next).parents('.slide').find('.sub-slide-text-container').cycle('next');
	}
}

//before slide change callback
function beforeSlide(curr,next) {
	//make menu active
	$('.menu-list>a').removeClass('active');
	$('.menu-list>a').eq($(next).index()).addClass('active');
	
	//google analytics events
	var tpage = $('.menu-list>a').eq($(next).index()).text().replace(/ /g,"_").replace(/^\s+|\s+$/g,"");
	if (tpage.toUpperCase()=="HOME") tpage = "/";
	_gaq.push(['_trackPageview', tpage]);
	
	//slide movement off-time efects +  menu adjustment
	var diff = Math.abs($(curr).index() - $(next).index()) - 1;
	if ($(curr).index() < $(next).index()) {
		$(next).find('.slide-content').css('left',(parseInt($(next).width()+(contentWidth/2))) + 'px');
		setTimeout(function() {
			$(next).find('.slide-content').animate({'left':parseInt(($(next).width()-contentWidth)/2+(contentWidth/2)) + 'px'},slideContentOfftimeAnimation + (70* diff));
		},10);
	} else if ($(curr).index() > $(next).index()) {
		$(next).find('.slide-content').css('left',-1 * (parseInt($(next).width()+(contentWidth/2))) + 'px');
		setTimeout(function() {
			$(next).find('.slide-content').animate({'left':parseInt(($(next).width()-contentWidth)/2+(contentWidth/2)) + 'px'},slideContentOfftimeAnimation + (70* diff));
		},10);
	}
	
	//if coming slide has sub-slides, initialize sub slider
	if ($(next).children('.sub-slide-container').length>0) {
		//initialize slides if not already initialized
		if ($(next).children('.sub-slide-container').data("cycle-initialized") == undefined) {
			$(next).children('.sub-slide-container').cycle({
				fx: 'scrollVert', 
				timeout: subSliderPauseBetweenSlides,
				slideExpr: 'div.sub-slide',
				before: beforeSubSlide
			});
			//store variable to avoid re-initializing
			$(next).children('.sub-slide-container').data("cycle-initialized","true");
			//initialize the content text slider too
			$(next).find('.sub-slide-text-container').cycle({
				fx: 'scrollVert', 
				timeout: 0,
				slideExpr: 'div.sub-slide-text'
			});
		} 
	}
	
	//if current slide had sub-slides, stop the auto-cycler
	if (($(curr).index()!=$(next).index()) && $(curr).children('.sub-slide-container').length>0) {
		$(curr).children('.sub-slide-container').cycle('pause');
	}
}

//after slide change callback
function afterSlide(curr,next) {
	//if next slide has sub-slides resume auto-cycler
	if ($(next).children('.sub-slide-container').length>0) {
		if ($(next).children('.sub-slide-container').data("cycle-initialized") == "true") {
			$(next).children('.sub-slide-container').cycle('resume');
		}
	}
		
}

//my custom slider function. changes slide to slide index given as paramater
function changeSlide(index) {
	curr = $('#slide-container .slide').eq(currentSlide);
	next = $('#slide-container .slide').eq(index);
	wd = $(next).width();
	beforeSlide(curr,next);
	setTimeout(function() {
		$('#slide-container').stop().animate({
			left: (-1 * index * wd) + 'px'
		}, slideDuration, 'easeOutQuad', function() {
			currentSlide = index;
			afterSlide(curr,next)
		});
	},10);
}

$(function() {
	//slide backgrounds
	$(window).resize(function() {
		resizeSlides();
	});
	resizeSlides();
	
	//menu events
	$('.menu-list>a').each( function(index, Element) {
		$(this).click(function() {
			changeSlide(index);
			return false;
		});
	});
	
	//next arrow animation
	$('.next-arrow').not(".next-arrow-always-open")
		.mouseenter(function() {
			$(this).animate({
				width: '179px'
			}, 150, 'easeOutQuad');
		})
		.mouseleave(function() {
			$(this).animate({
				width: '60px'
			}, 150, 'easeOutQuad');
		});
	//next arrow event
	$('.next-arrow').not(':last').click(function() {
		changeSlide((currentSlide+1));
		return false;
	});
	//similar events
	$('.slide-main-box .a-find-more').click(function() {
		changeSlide((currentSlide+1));
		return false;
	});
	
	//hide social separator on hover
	$('.a-twitter,.a-facebook')
		.mouseenter(function() {$('.social-sep').css('visibility','hidden')})
		.mouseleave(function() {$('.social-sep').css('visibility','visible')})
	//twitter box
	$('.a-twitter')
		.mouseenter(function() {
			$('#tweet-box').show();
			$('.a-twitter').addClass("active");
		})
		.mouseleave(function() {tweetBoxTimeout = setTimeout(function() {
			$('#tweet-box').hide();
			$('.a-twitter').removeClass("active");
		},50)})
	$('#tweet-box')
		.mouseenter(function() {clearTimeout(tweetBoxTimeout)})
		.mouseleave(function() {tweetBoxTimeout = setTimeout(function() {
			$('#tweet-box').hide();
			$('.a-twitter').removeClass("active");
		},50)})
	
	//when everything loaded, remove the loading animation
	$(window).load(function() {
		if ($.browser.msie && $.browser.version.substr(0,1)<7) {
			$('#loading').fadeOut(1500,function() {
				$('.header-content').css('border','1px solid #BBBCBC');
				$('.header-content').css('border','0');
			});
		} else {
			$('#loading').fadeOut(1500);
		}
		//initialize my slide
		currentSlide = 0;
		changeSlide(0);
	});
	
	//initialize work portfolio scroller
	$('.portfolio-container').jScrollPane({horizontalDragMinWidth:92,horizontalDragMaxWidth:92});
	
	$(document).scroll(function() {$(document).scrollLeft(0);});
});
