jQuery(function() {
	
	jQuery("select").selectbox();
	
	if (jQuery.browser.msie) {
		//Cufon.replace('#main-nav li a, a.shopping-bag, #topbar li, .sort, #footer li, .paging .page, p, h2, h2 a, h3, h3 a, h4, h4 a, h5, h5 a', { 'hover' : true });
	};
	
	DD_roundies.addRule('.corner', '5px', true);
	DD_roundies.addRule('.greyboard', '3px', true);

	jQuery('#main-nav .shopping-bag').live('click', function() {
		
		var isEmpty = true;		
		var linkName =$('numItems').innerHTML;
		for (var index = 1; index <=9; index++)
		{
			if (linkName.search(index) > -1)
			{
				isEmpty = false;
			}
		}
		if (isEmpty == true)
		{
			location.href = contextPath + '/shopping_cart/show_cart_empty.jsp';
			return false;
		}
		
		//acdc changes**DON'T DELETE!! this is defined in milly.js
		if(jQuery('#shopping-cart')[0].style.display == 'none' && jQuery('#shopping-cart')[0].innerHTML == ''){
			//will do the toggleMiniCart after responsed content in populateMiniCart
			populateMiniCart(contextPath);
		}else{
			toggleMiniCart();
		}
		
		return false;
	});
	
	tooltip();
	
	jQuery('#look-popup').center();
	
	jQuery('#collections-landing .text .entry').hover(function() {
		jQuery(this).addClass('entry-hover');
	}, function() {
		jQuery(this).removeClass('entry-hover');
	});
	
	jQuery('#main-nav .shopping-bag').live('click', function() {
		jQuery(this).toggleClass('shopping-bag-active');
		var left = jQuery(this).position().left + jQuery('#shopping-cart').width() - 150;
		var top = parseInt(jQuery(this).position().top) + 20;
		
		jQuery('#shopping-cart').css({
			'left' : left,
			'top' : 110
		}).toggle();
		return false;
	});
	
	jQuery('#main-nav .nav li a').live('click', function() {
		var subnav = jQuery(this).parents('li:eq(0)').find('.subnav');
		jQuery('#main-nav .nav li .subnav').each(function(){
			jQuery(this).hide();
		});
		if (subnav.length) {
			subnav.toggle();
		};
	});
	 	
	jQuery('.popup .close').live('click', function() {
		jQuery(this).parents('.popup:eq(0)').hide();
		return false;
	});
	
	jQuery('.category-grid-chooser a').live('click', function() {
		jQuery('.category-grid').hide();
		jQuery('.category-grid-chooser a').removeClass('active');
		jQuery(jQuery(this).attr('href')).show();
		jQuery(this).addClass('active');
		return false;
	});
	
	jQuery('.custom-scroll').jScrollPane({
		scrollbarWidth : 3
	});
	
	jQuery('#slider-scroll').jScrollPane({
		scrollbarWidth : 5
	});
	
	jQuery('.studio-page .images .buttons a').live('click', function() {
		var current = jQuery(this).parents('.images:eq(0)').find('li.active');
		var mover = jQuery(this).parents('.images:eq(0)').find('ul');
		return managePhotoGallery(this, current, mover);
	});
	
	
	jQuery('#collections-landing .images .buttons a').live('click', function() {
		var current = jQuery('.holder').find('li.active');
		var mover = jQuery('.holder').find('ul');
		return managePhotoGallery(this, current, mover);
	});
	
	jQuery('#press-event .images .buttons a').live('click', function() {
		var current = jQuery('.holder').find('li.active');
		var mover = jQuery('.holder').find('ul');
		return managePhotoGallery(this, current, mover);
	});
	

});

function managePhotoGallery(thisObj, current, mover)
{
	if (jQuery(thisObj).hasClass('next')) {
		if (current.is(':last-child')) {
			return false;
		};
		current.removeClass('active');
		var next = current.next();
		var next_position = next.position().left;
		next.addClass('active');
		mover.animate({
			'left' : -1*(next_position)
		});
	} else {
		if (current.is(':first-child')) {
			return false;
		};
		current.removeClass('active');
		var next = current.prev();
		var next_position = next.position().left;
		next.addClass('active');
		mover.animate({
			'left' : -1*(next_position)
		});
	};
	return false
}

function set_homepage() {
	var pos = (parseInt(jQuery(window).width()) - parseInt(jQuery('.center').css('width')))/2;
	var left_position = pos + 510;
	var models_left_position = pos - 120;
	var models_top_position = jQuery('#wrapper').height() - 908;
	if (models_top_position < 0) {
		models_top_position = 0;
	};
	if (!FlashDetect.installed)
	{
		jQuery('body.homepage #collection-header').css('left', left_position).show();
		jQuery('body.homepage #wrapper').css('background-position', left_position);
		jQuery('body.homepage .models:eq(0)').css({
			'left': models_left_position,
			'top' : models_top_position,
			'height': jQuery('#wrapper').height()
		}).show();
		jQuery('#logo').empty();
		jQuery('#logo').prepend('<a href="#">Milly</a>');
	}
	else
	{
		jQuery('body.homepage').css('background', 'none');
		jQuery('body').css('background', 'none repeat scroll 0 0 black');	
	}
}
function set_lookbook() {
	jQuery('#lookbook .gallery img').css({
		'height' : jQuery('#wrapper').height(),
		'width' : jQuery('#wrapper').width()/3
	});
	
	var subnav_position = jQuery('#lookbook .gallery img.active').position().left - jQuery('#lookbook .subnav').width() - 10;
	var subnav_top = jQuery('#logo').height();
	jQuery('#lookbook .subnav').css({'left': subnav_position, 'top' : subnav_top});
	
	var buttons_prev_left = jQuery('#lookbook .gallery img.active').position().left - 52;
	var buttons_next_left = buttons_prev_left + jQuery('#lookbook .gallery img.active').width() + 52;
	jQuery('#lookbook .buttons a.prev').css('left', buttons_prev_left);
	jQuery('#lookbook .buttons a.next').css('left', buttons_next_left);
	
	var look_no_left = jQuery('#lookbook .gallery img.active').position().left + jQuery('#lookbook .gallery img.active').width();
	jQuery('#lookbook .look-no').css('left', look_no_left)
}

//acdc changes**DON'T DELETE!!
function toggleMiniCart(){
	var el = jQuery('#main-nav .shopping-bag')[0];
	jQuery(el).toggleClass('shopping-bag-active');
	var left = jQuery(el).position().left + jQuery('#shopping-cart').width() - 150;
	var top = parseInt(jQuery(el).position().top) + 20;
	
	jQuery('#shopping-cart').css({
		'left' : '60%',
		'top' : '110px'
	}).toggle();
}

jQuery.fn.center = function(loaded) {
    var obj = this;
    body_width = parseInt(jQuery(window).width());
    body_height = parseInt(jQuery(window).height());
    block_width = parseInt(obj.width());
    block_height = parseInt(obj.height());
    
    left_position = parseInt((body_width/2) - (block_width/2)  + jQuery(window).scrollLeft());
    if (body_width<block_width) { left_position = 0 + jQuery(window).scrollLeft(); };
    top_position = parseInt((body_height/2) - (block_height/2) + jQuery(window).scrollTop());
    if (body_height<block_height) { top_position = 0 + jQuery(window).scrollTop(); };
    
    if(!loaded) {
        
        obj.css({'position': 'absolute'});
        obj.css({'left': left_position, 'top': top_position});
        jQuery(window).bind('resize', function() { 
            obj.center(!loaded);
        });
        jQuery(window).bind('scroll', function() { 
            obj.center(!loaded);
        });
        
    } else {
        obj.stop();
        obj.css({'position': 'absolute'});
        obj.animate({'left': left_position, 'top': top_position}, 200, 'linear');
    }
}


tooltip = function(){	
	/* CONFIG */		
		xOffset = 55;
		yOffset = 15;		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result		
	/* END CONFIG */		
	jQuery("a.tooltip").hover(function(e){											  
		this.t = this.title;
		this.title = "";									  
		jQuery("body").append("<div id='tooltip'><div class='la'>&nbsp;</div><div class='bg'><span>"+ this.t +"</span></div><div class='ra'>&nbsp;</div><div class='cl'>&nbsp;</div></div>");
		jQuery("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");		
    },
	function(){
		this.title = this.t;		
		jQuery("#tooltip").remove();
    });	
	jQuery("a.tooltip").mousemove(function(e){
		jQuery("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
};


//Stupid Browsers that do not implement trim
if(typeof String.prototype.trim !== 'function') 
{
	String.prototype.trim = function() 
	{
		return this.replace(/^\s+|\s+$/g, '');    
	} 
} 
