jQuery.easing.easeOutQuart = function (x, t, b, c, d) {
	return -c * ((t=t/d-1)*t*t*t - 1) + b;
};

$(document).ready(function () {

	// var watchIdx = Math.floor(Math.random()*6)+1; 
	// $('#watchPhoto img').attr('src', "/CasoStore/site/watches/html/images/watch-" + watchIdx + ".jpg");
	// var jewelIdx = Math.floor(Math.random()*4)+1; 
	// $('#jewelPhoto img').attr('src', "/CasoStore/site/watches/html/images/jewel-" + jewelIdx + ".jpg");
	
	var watchIdx = Math.floor(Math.random()*6)+1; 
	$('#watchPhoto img').attr('src', "images/watch-" + watchIdx + ".jpg");
	var jewelIdx = Math.floor(Math.random()*4)+1; 
	$('#jewelPhoto img').attr('src', "images/jewel-" + jewelIdx + ".jpg");	
	
	$(window).resize(function () {
		resizeHome();
	});
	
	if(window.orientation == 90 || window.orientation == -90) {
		$('body').addClass('landscape');
	}	

	if(!($.browser.msie && (parseInt($.browser.version, 10) < 7))) {

		$('#watches').bind({
			mouseenter: function () {
				$('#watches h2 a').addClass('hover');
			},
			mouseleave: function () {
				$('#watches h2 a').removeClass('hover');
			}		
		});

		$('#jewels').bind({
			mouseenter: function () {
				$('#jewels h2 a').addClass('hover');
			},
			mouseleave: function () {
				$('#jewels h2 a').removeClass('hover');
			}		
		});	

		if($('.productlist').length) {
			$('.productlist').vgrid({
				easeing: "easeOutQuint",
				time: 400,
				delay: 20,
				fadeIn: {
					time: 500,
					delay: 50
				}
			});
		}

	}

	$('#subnav').hide();

	$('.openSubnav').bind({
		click: function (e) {
			e.preventDefault();
			$('#subnav').fadeIn(500);			
		}
	});
	
	if(!($(window).width() < 481)) {
	
		$("#nav, #extra ul").lavaLamp({
			fx: 'easeout',
			speed: 300
		});
	
		$("#subnav ul").lavaLamp({
			fx: 'easeout',
			homeLeft: 20,
			homeTop: 30,
			speed: 300
		});	
	
		$("#column1 ul:not('#column1 ul#social')").lavaLamp({
			fx: 'easeout',
			speed: 100
		});		
	
	} else {
		
		$('.toggle').bind('click', function () {
			$(this).toggleClass('open').siblings('ul').slideToggle(200);
		});
		
	}
	
	$('.colorbox').bind('click', function (e) {
		e.preventDefault();
		if(!($(window).width() < 481)) {
			$('.colorbox').colorbox({ 
				transition: 'fade'
			});		
		} else {
			$('.colorbox').colorbox({ 
				transition: 'fade',
				innerWidth: 260,			
				onOpen: function() { 
					$('#colorbox').addClass('loading');				
					$('#header, #main').animate({
						opacity: .2
					}, 200);
				},
				onComplete: function () {
					$('#colorbox').removeClass('loading');								
				},
				onClosed: function() { 
					$('#header, #main').animate({
						opacity: 1
					}, 500);
				}						
			});
		}
	});	
	
	$('.product').css('opacity', '0');
	
	$('.productlist li').bind({
		mouseenter: function () {
			$(this).find('.product').animate({
				opacity: 1,
				right: 7
			}, 300);
		},
		mouseleave: function () {
			$(this).find('.product').animate({
				opacity: 0,
				right: 27
			}, 300);			
		}
	});
	
	$('.top a').bind({ 
		click: function (e) {
			e.preventDefault();
			$('html, body').animate({scrollTop:0}, 500);					
		}
	});

    $("#search").focus(function() { if ($(this).val() == 'Search') $(this).val(''); });
    $("#search").blur(function()  { if ($(this).val() == '') $(this).val('Search'); });	
	
	if($('#subNav').length) {
		$('#content').addClass('whenSubnav');
	}	
	
	$prev = $('#galleryNav #prev');
	$next = $('#galleryNav #next');	
	
	var thumbnails = $('#furtherphotosWrapper');
	
	if(thumbnails.length) {
		if($(window).width() > 959) {
			if(thumbnails.find('a').length <= 4) {
				$prev.addClass('disabled');
				$next.addClass('disabled');		
			}
			thumbnails.serialScroll({
				items:'a',
				prev:'#galleryNav #prev',
				next:'#galleryNav #next',
				offset:6, 
				start:0, 
				duration:800,
				force:true,
				stop:true,
				lock:false,
				cycle:false, 
				easing:'easeOutQuart', 
				exclude: 3,
				jump: false,
				onBefore:function(e, elem, $pane, $items, pos ){
					thumbnails.find('.active').removeClass('active');
					$(elem).addClass('active');
					$prev.add($next).show();
					if(pos == 0) {
						$prev.addClass('disabled');						
						$next.removeClass('disabled');					
					} else if($('#furtherphotosWrapper a:nth-last-child(4)').is('.active')) {
						$next.addClass('disabled');
						$prev.removeClass('disabled');	
					} else {
						$prev.removeClass('disabled');
						$next.removeClass('disabled');					
					}			
				} 
			});
		} else {
			thumbnails.serialScroll({
				items:'a',
				prev:'#galleryNav #prev',
				next:'#galleryNav #next',
				offset:6, 
				start:0, 
				duration:800,
				force:true,
				stop:true,
				lock:false,
				cycle:false, 
				easing:'easeOutQuart', 
				jump: false,
				onBefore:function(e, elem, $pane, $items, pos ){
					thumbnails.find('.active').removeClass('active');
					$(elem).addClass('active');
					$prev.add($next).show();
					if(pos == 0) {
						$prev.addClass('disabled');						
						$next.removeClass('disabled');					
					} else if(pos == $items.length-1) {
						$next.addClass('disabled');
						$prev.removeClass('disabled');	
					} else {
						$prev.removeClass('disabled');
					}			
				}
			});
		}
	}


    
$("#newsletter #newsletter_input").click(function () {
	showNewsletter();
});

$("#newsletter_submit").click(function (l) {
	if (! $("#newsletter_fieldset").hasClass("visible")) {		
		l.preventDefault();
		showNewsletter();
		$("#newsletter_input").select();
	}			
});

$("#newsletter .close").click(function (l) {
    l.preventDefault();
    hideNewsletter();
});	
	 

var optionsNewsletter = {         
    success: successNewsletter,  // post-submit callback
    beforeSubmit: newsletterValidation
}; 

$('#newsletter_input').click(function() {
	$('#newsletter_input').attr("value","");
});
  
$('#newsletter').ajaxForm(optionsNewsletter);

$('#search').click(function() {
	$('#search').select();
});	
	
});

function showNewsletter() {
	$("#newsletter_fieldset").addClass("visible");
    $("#newsletter .privacy").addClass("visible");
    $("#newsletter .close").addClass("visible")
}

function hideNewsletter() {
	$("#newsletter .privacy").removeClass("visible");
    $("#newsletter .close").removeClass("visible");
    $("#newsletter_fieldset").removeClass("visible");
    
   	$("#newsletter_input").removeClass("error");
   	$("label.error").hide();
    
    $("#myform").validate({errorPlacement: null})
}

function newsletterValidation() {
	$("#newsletter").validate();	
	return $("#newsletter").valid( );
}

function resizeHome () {
	if($('#wrapper').height() <= 760) {
		$('#homepage #menu').addClass('centered');
	} else if($('#wrapper').height() >= 760) {
		$('#homepage #menu').removeClass('centered');			
	}	
}

function successNewsletter(responseText, statusText)  {
	if(statusText='success') {
		$('#newsletter_input').hide();
		$('#newsletter_submit').hide();
		
		hideNewsletter();
        
		$('#newsletter_fieldset').append(responseText);
		if (responseText.match("success")!= null) {
			pageTracker._trackPageview("/target/newsletter"); 
		}
		$('#newsletter_retry').click(function() {
			$('#newsletter_response').remove();
			$('#newsletter_input').fadeIn();
			$('#newsletter_submit').fadeIn();
			$('#newsletter_input').select();			
		});
	}	 
} 

window.onorientationchange = function() {
    var orientation = window.orientation;
    switch(orientation) {
        case 0:
		$('body').removeClass('landscape');
        break;

        case 90:
		$('body').addClass('landscape');
        break;

        case 180:
		$('body').removeClass('landscape');
        break;

        case -90:
		$('body').addClass('landscape');
        break;
    }
}

