$(document).ready(function(){
	
	if ($('#giftCardThemes a.thumb').length > 0) initGiftCardThemes();
	
	if($('#prodWrap').length > 0) {
	//	alert('sdf')
		theHover = $('<div>').attr("id", "hoverThumb").hide();
		theImg = $('<img>');
		$(theHover).html(theImg);
		$('#prodRight').append(theHover);
		
		$('#prodRight table img, #prodRight table div').hover(function() {
			
			theHover.css({backgroundColor: '#ffffff'}).children().show();
			
			//alert(this.tagName)
			if(this.tagName == 'DIV') {
				theColor = $(this).attr('theColor');
				theHover.children().hide().end().css({top: Math.round($(this).position().top-180)+"px", left: Math.round($(this).position().left-70)+"px", backgroundColor: "#"+theColor}).stop(true, true).fadeIn('fast');
			} else {
				
				$(theImg).attr('src', $(this).attr('big'));
				$(theHover).css({top: Math.round($(this).position().top-180)+"px", left: Math.round($(this).position().left-70)+"px"}).stop(true, true).fadeIn('fast');
			}
			
		}, function () {
			$(theHover).stop(true, true).fadeOut('fast');
		}).click(function() {
			$(this).siblings('input').attr('checked', true);
		});
	}
	
	var psToggle = true;
	if($('#personalShopper').length > 0) {
		
		$('#personalShopperInfo').hide();
		
		$('#personalShopper').bind('click', function() {
			if (psToggle) {
				$('#personalShopperInfo').slideDown('fast');
			} else {
				$('#personalShopperInfo').slideUp('fast');
			}
			psToggle = !psToggle;
		});
		
	}
	
	setTimeout(hideURLBar, 0);
	
		
});

function hideURLBar() {
	window.scrollTo(0,1);
}

function initGiftCardThemes() {
	
	$('#giftCardThemes a.thumb').fancybox({
		'overlayOpacity': 0.85,
		'overlayColor': '#FFF'
	});
	
}

/******************** AD ROTATOR PLUGIN *********************/

(function($){
    
    function Column(_opts) {
        
    	var self = this;
        self.wrap = $('<div></div>');
        self.img1 = $('<img />').appendTo(self.wrap).css({ position: 'absolute', top: 0, left: 0, visibility: 'hidden' });
        self.img2 = $('<img />').appendTo(self.wrap).css({ position: 'absolute', top: 0, left: 0, visibility: 'hidden' });
        self.link = $('<a style="display:block; width:100%; height:100%; position:absolute; top:0; left:0; z-index:5; background: url(/assets/images/global/spacer.gif)"></a>').appendTo(self.wrap);
        
        self.pImg;
        self.cImg;
        
        self.defaults = { imgToggle: false };
        self.opts = $.extend(self.defaults, _opts);
        self.cSlide = -1;
        
        self.init = function() {
            
            self.wrap.css({ width: self.opts.width, height: self.opts.height, overflow: 'hidden', position: 'absolute' });
            
            self.img1.load(imgLoad);
            self.img2.load(imgLoad);
            
            function imgLoad() {
                self.onLoad();
            }
            
            self.link.hover(function() {
            	self.link.stop(true, true);
            	self.link.animate({ opacity: 0 }, { duration: 200 });
            }, function() {
            	self.link.stop(true, true);
            	self.link.animate({ opacity: .2 }, { duration: 400 });
            });
            
        }
        
        self.onLoad = function() {
            
        	self.resizeImage();
            self.cImg.css({ visibility: 'visible' }).fadeIn(self.opts.duration);
            
        }
        
        self.resizeImage = function() {
        	
        	var imgW, newW = self.cImg.width();
        	var imgH, newH = self.cImg.height();
        	var ratio = imgW/imgH;
        	
        	if (imgW < self.opts.width) {
        		newW = self.opts.width;
        		newH = newW / ratio;
        	}
        	if (newH < self.opts.height) {
        		newH = self.opts.height;
        		newW = newH * ratio;
        	}
        	
        	self.cImg.css({ left: Math.round((self.opts.width-newW) / 2), right: Math.round((self.opts.height-newH) / 2) });
        }
        
        self.nextSlide = function() {
            var newSlide = (self.cSlide+1 < self.opts.slides.length) ? self.cSlide+1 : 0;
            self.gotoSlide(newSlide);
        }
        
        self.prevSlide = function() {
            var newSlide = (self.cSlide > 0) ? self.cSlide-1 : self.opts.slides.length-1;
            self.gotoSlide(newSlide);
        }
        
        self.toggleImage = function() {
        	
        	if (self.opts.imgToggle) {
        		self.pImg = self.img1;
        		self.cImg = self.img2;
        	} else {
        		self.cImg = self.img1;
        		self.pImg = self.img2;
        	}
        	self.opts.imgToggle = !self.opts.imgToggle;
        	
        }
        
        self.gotoSlide = function(_num) {
        	
        	self.toggleImage();
        	
            if (_num != self.cSlide) {
            	if (typeof(self.opts.slides[_num].href) == "string" && self.opts.slides[_num].href.length > 0) {
            		self.link.attr('href', self.opts.slides[_num].href);
            	} else {
            		self.link.removeAttr('href');
            	}
            	self.pImg.css({ 'z-index': 1 });
                self.cImg.attr('src', self.opts.slides[_num].src).css({ 'z-index': 2 }).hide();
                self.cSlide = _num;
            }
            
        }
        
        self.init();
        
    }
    
    $.fn.adRotator = function(options) {
        
        var defaults = {
            width: -1,
            height: -1,
            spacing: 10,
            delay: 3000,
            delayOffset:200,
            fadeDuration: 2000,
            bgColor: '#F2F2F2',
            debug: false
        }
        var opts = $.extend(defaults, options);
        
        return this.each(function() {
            
            var self = this;
            var el = $(this);
            var cW = (opts.width > -1) ? opts.width : el.width();
            var cH = (opts.height > -1) ? opts.height : el.height();
            var intId;
            var fullDelay;
            var data;
            var loader;
            
            var cols = new Array();
            el.addClass('adRotator').css({ position: 'relative', width: cW, height: cH, overflow: 'hidden' });
            
            var colSize;
            
            // Add loader
            if (typeof(opts.loader) == "string") {
            	loader = $('<div style="position:absolute;z-index:3;width:100%;height:100%;background:url(' + opts.loader + ') 50% 50% no-repeat;"></div>').appendTo(el);
            }
            
            $.getJSON(opts.json, function(_data) {
            	if (typeof(opts.loader) == "string") loader.remove();
            	data = _data;
                colSize = Math.floor((cW - (opts.spacing * (data.length-1))) / data.length);
                for (var i = 0; i < data.length; i++) {
                    var cCol = new Column({ slides: data[i], width: colSize, height: cH, duration: opts.fadeDuration });
                    cCol.wrap.css({ top: 0, left: (i * (colSize + opts.spacing)) }).appendTo(el);
                    if (opts.debug) cCol.wrap.append('<span style="position:absolute;z-index:4;top:2px;left:2px;background:#FFF;color:#000;">' + colSize + 'px</span>');
                    cols[i] = cCol;
                }
                fullDelay = opts.delay + (opts.delayOffset*(data.length-1)) + opts.fadeDuration;
                timerHandler();
                intId = setInterval(timerHandler, fullDelay);
            });
            
            function timerHandler() {
            	
            	cols[0].nextSlide();
                
                for (var i = 1; i < cols.length; i++) {
                	data[i].timeoutId = doTimeout(i, i*opts.delayOffset);
                }
                
            }
            
            function doTimeout(_num, _delay) {
            	
            	var num = _num;
            	var delay = _delay;
            	
            	//alert(num);
            	
            	return setTimeout(cols[num].nextSlide, delay);
            	
            }
            
            function nextSlideCol(_num) {
            	
            	cols[_num].nextSlide();
            	
            }
            
        });
        
        return this;
        
    };
})(jQuery);
