$(document).ready(function(){
	var jq_zoom_options = {
		zoomWidth: 350,
		zoomHeight: 409,
		xOffset: 76,
		yOffset: -35,
		position: "right",
		title: false,
		preloadImages: true,
		showPreload: false,
		cursor: 'crosshair'
	};

	if( $("#thumbs_container_2 .thumb img").length ){
		$('#main_photo a').jqzoom(jq_zoom_options);
		$("#thumbs_container_2 .thumb img").mouseover( function(i) {
				thumb = $(this).attr("src");
				$("#main_photo_img").attr({src: thumb});
				$('#main_photo a').attr('href', $(this).attr('_zoom') );
		});
	}
	
	if( $('#front_page_images').length ){
		setTimeout( function(){
			front_rotate(1);
		}, 3000);
	}
	
	if( $('.press-item').length ){
		$('.press-item a').click( function(e){
			e.preventDefault();
		});
		
		$('.press-item img').click( press_click );
		$('.press-item img').mouseover( press_over );
		$('#press-image').click( function(){
			$('#press-bg').fadeOut();
			$('#press-full').fadeOut('fast');
		});
		
		$(window).resize(function(){
			if( $('#press-bg').is(':visible') )
			show_bg();
		});
		
		$('.press-item a').each( function(){
			$.preloadImages( $(this).attr('href') );
		});
	}

	$("#brand-drop").click( function(e){
		e.preventDefault(); 
		e.stopPropagation();
		
		br_list = $('#brand-list');
		br_h = br_list.height();
		if( br_list.height() > 0 && br_list.css('display') == 'block' ){
			br_list.animate({ 
				height: '0px' 
			}, 
			{ 
				duration: 275, 
				complete: function(){
					br_list.css({
						'display' 	: 'none',
						'height'	: 'auto'
					});
				}
							
			});
			return;
		}
		
		br_list.css({
						'height' 	: '0px',
						'display' 	: 'block'
					});
		br_list.animate({ height: br_h+'px' }, 275);
	}); 
	
	$("#measure_question").mouseover( function() {
		$("#measure_pop").show();
	});
	
	$("#measure_question").mouseout( function() {
		$("#measure_pop").hide();
	});
	
	$('.lookbook-select a').click( function(e){
		$('#left, #right').show('fast');
		$('#fin').hide('fast');

		e.returnValue = false;
		if(e.preventDefault) e.preventDefault();
		
		new_html = $(this).next().html();
		$('#look-book').html(new_html);
		
		$("#look-book").animate({ marginLeft: "0px" }, 500);
	});
	
	$('#stage').click( function(e){
		$('#left, #right').hide('fast');
		stage_w = $('#stage').css('width').replace(/[^\d]/ig, '');
		
		
		var x = e.pageX - $(this).offset().left;
		var y = e.pageY - $(this).offset().top;
		
		lookbook_m = parseFloat($('#look-book').css('marginLeft').replace(/[^-\.\d]/ig, ''));
		if( x < (stage_w / 2) ){
			// nearer the left side, go backwards..
			$('#fin').hide('fast');
			if( lookbook_m >= 0 )
				return;
			else
				$("#look-book").animate({ marginLeft: "+="+stage_w+"px" }, 1000);
		} else {
			// nearer right side, go forwards..
			lb_max_m = ( $('#look-book .sub').length * stage_w - stage_w);
			if( Math.abs(lookbook_m) >= lb_max_m )
				$('#fin').show('fast');
			else
				$("#look-book").animate({ marginLeft: "-="+stage_w+"px" }, 1000);

		}
	});
	
	if( $.cookie('popdown') == '1' ) return;
	$('#popdown').show();
	$('#popdown-parent').animate(
		{
			'top': '-230px'
		},
		{
			duration: 1000,
			queue: false,
			complete: function(){ 
				$.cookie('popdown', '1', { expires: 7 });
			},
			easing: 'easeOutExpo'
		}
	);
	
	$('#popdown-parent a').click(function(){
		$('#popdown-parent').animate(
				{
					'top': '230px'
				},
				{
					duration: 1000,
					queue: false,
					complete: function(){ 
						$('#popdown-parent').remove();
						$('#popdown').hide('fast');
					},
					easing: 'easeOutExpo'
				}
			);	
	});
});

function front_rotate(num){
	new_src = $('#front_page_images img.cycle')[num];
	new_src = $(new_src).attr('src');
	
	num++;
	if( num == $('#front_page_images img.cycle').length )
		num = 0;

	$('<img>')
		.attr('src', new_src)
		.css({
			'opacity': 0,
			'position': 'absolute'
		})
		.appendTo( $('#front_page_images a') )
		.animate({ 'opacity': 1 }, 1000, function(){
			setTimeout( function(){
				front_rotate(num);
				to_remove = $('#front_page_images a img')[0];
				$(to_remove).remove();
			}, 3000);
		});
}

function press_over(e){
	return; // :(
	// this is a ridiculous function. and i love it.
	el = $(e.target);
	new_src = el.attr('src').replace(/0\.jpg/, '1.jpg');
	$('<img>')
		.attr('src', new_src)
		.css({
			'position': 'absolute',
			'display': 'none'
		})
		.addClass('over')
		.prependTo( el.parent() )
		.fadeIn('fast')
		.mouseout( function(){
			$(this).fadeOut('fast', function(){ $(this).remove() });
		});
}

function press_click(e){
	e.preventDefault();
	el = $(e.target);
	
	if( ! $('#press-bg').is(':visible') )
		show_bg();
		
	//el.css('display', 'none');
		
	thumbs = $('#press-thumbs');
	if( thumbs_html = $( el.parents().filter('.press-set') ).html() )
		thumbs.html( thumbs_html );
	$('.press-item img').mouseover( press_over );
	
	thumbs.children().filter('.press-item').show();
	thumbs.children().filter('.press-item').click( press_click );
	
	
	$('#press-image').children().filter('img').attr('src', el.parent().attr('href'));
	arrPageSizes = ___getPageSize();
		
	img_w =  el.parent().attr('width');
	img_h =  el.parent().attr('height');
	press_x = ( arrPageSizes[0] / 2 ) - ( img_w / 2 );
	$('#press-full').animate({
		'left': press_x+'px'
	}, 500);
	
	$('#press-image img').width(1);
	$('#press-image img').animate({
		width: img_w+"px"
	}, 
	{
		duration: 500,
		easing: 'swing'
	});
	$('#press-full').show();
}

function show_bg(){
	var arrPageSizes = ___getPageSize();
	
	$('#press-bg').css({
		backgroundColor:	'#000',
		opacity:			.7,
		width:				arrPageSizes[0],
		height:				arrPageSizes[1]
	}).fadeIn('slow');
}

$.preloadImages = function(){
  for(var i = 0; i<arguments.length; i++){
    $("<img>").appendTo('body').attr("src", arguments[i]).remove();
  }
}



/**
 / THIRD FUNCTION
 * getPageSize() by quirksmode.com
 *
 * @return Array Return an array with page width, height and window width, height
 */
function ___getPageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
};


jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options = $.extend({}, options); // clone object since it's unexpected behavior if the expired property were changed
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // NOTE Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

jQuery.easing['jswing'] = jQuery.easing['swing'];

jQuery.extend( jQuery.easing,
{
	def: 'easeOutQuad',
	swing: function (x, t, b, c, d) {
		//alert(jQuery.easing.default);
		return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
	},
	easeInQuad: function (x, t, b, c, d) {
		return c*(t/=d)*t + b;
	},
	easeOutQuad: function (x, t, b, c, d) {
		return -c *(t/=d)*(t-2) + b;
	},
	easeInOutQuad: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t + b;
		return -c/2 * ((--t)*(t-2) - 1) + b;
	},
	easeInCubic: function (x, t, b, c, d) {
		return c*(t/=d)*t*t + b;
	},
	easeOutCubic: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t + 1) + b;
	},
	easeInOutCubic: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t + b;
		return c/2*((t-=2)*t*t + 2) + b;
	},
	easeInQuart: function (x, t, b, c, d) {
		return c*(t/=d)*t*t*t + b;
	},
	easeOutQuart: function (x, t, b, c, d) {
		return -c * ((t=t/d-1)*t*t*t - 1) + b;
	},
	easeInOutQuart: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
		return -c/2 * ((t-=2)*t*t*t - 2) + b;
	},
	easeInQuint: function (x, t, b, c, d) {
		return c*(t/=d)*t*t*t*t + b;
	},
	easeOutQuint: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t*t*t + 1) + b;
	},
	easeInOutQuint: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
		return c/2*((t-=2)*t*t*t*t + 2) + b;
	},
	easeInSine: function (x, t, b, c, d) {
		return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
	},
	easeOutSine: function (x, t, b, c, d) {
		return c * Math.sin(t/d * (Math.PI/2)) + b;
	},
	easeInOutSine: function (x, t, b, c, d) {
		return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
	},
	easeInExpo: function (x, t, b, c, d) {
		return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
	},
	easeOutExpo: function (x, t, b, c, d) {
		return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
	},
	easeInOutExpo: function (x, t, b, c, d) {
		if (t==0) return b;
		if (t==d) return b+c;
		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
	},
	easeInCirc: function (x, t, b, c, d) {
		return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
	},
	easeOutCirc: function (x, t, b, c, d) {
		return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
	},
	easeInOutCirc: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
		return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
	},
	easeInElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
	},
	easeOutElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
	},
	easeInOutElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d/2)==2) return b+c;  if (!p) p=d*(.3*1.5);
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
		return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;
	},
	easeInBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		return c*(t/=d)*t*((s+1)*t - s) + b;
	},
	easeOutBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
	},
	easeInOutBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158; 
		if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
		return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
	},
	easeInBounce: function (x, t, b, c, d) {
		return c - jQuery.easing.easeOutBounce (x, d-t, 0, c, d) + b;
	},
	easeOutBounce: function (x, t, b, c, d) {
		if ((t/=d) < (1/2.75)) {
			return c*(7.5625*t*t) + b;
		} else if (t < (2/2.75)) {
			return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
		} else if (t < (2.5/2.75)) {
			return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
		} else {
			return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
		}
	},
	easeInOutBounce: function (x, t, b, c, d) {
		if (t < d/2) return jQuery.easing.easeInBounce (x, t*2, 0, c, d) * .5 + b;
		return jQuery.easing.easeOutBounce (x, t*2-d, 0, c, d) * .5 + c*.5 + b;
	}
});
