var s = {
	"openPopin":function(link, width, height, selector){
		var that = $(this);
		// Specif if zoomable
		if (that.hasClass('product-image-zoom')) {
			selector = '.product-zoom-content';
			link = '';
		}
		else {
			// Overlay
			//$('body').css('overflow', 'hidden');
		}
		var content = '';
		var link = typeof link == 'undefined' || typeof link == 'object' ? that.attr('href') : link;
		var width = typeof width == 'undefined' ? 510 : width;
		var height = typeof height == 'undefined' ? 300 : height;
		var marginLeft  = Math.round(width / 2);
		var marginTop  = Math.round(height / 2);
		var top = "50%";
		if (link != '' && typeof selector == 'undefined') {
			$.ajax({
				"url": link + '?t=t&content_only=1',
				"cache": false,
				"async": false,
				"success": function(html){
					content = html;
				}
			});
		}
		else {
			content = $(selector).html();
		}
		// Specif if zoomable
		if (that.hasClass('product-image-zoom')) {
			width = 995;
			marginLeft  = Math.round(width / 2);
			height = 830;
			marginTop = 0;
			top = "150px";
		}
		if (that.hasClass('popin-sendtoafriend')) {
			width = 545;
			marginLeft  = Math.round(width / 2);
			height = 425;
			marginTop = 0;
			top = "150px";
		}
		// GotoTop
		$('html, body').animate({scrollTop: 0}, 'slow');
		jQuery('<div>&nbsp;</div>').attr('class','popin-overlay').css({"position":"absolute","top":0,"left":0,"zIndex":500,"width":"100%","height":"100%"}).appendTo('body');
		// Popin
		jQuery('<div>&nbsp;</div>').attr('class','popin-content').css({"position":"absolute","top":top,"left":"50%","zIndex":600,"width":width+'px',"height":height+'px',"margin":"-" + marginTop + "px 0 0 -" + marginLeft + "px"}).html(content).appendTo('body');	

		if(typeof(fleXenv)!="undefined") fleXenv.fleXcrollMain("popin-cms-content");
		return false;
	},
	"openCMSPopin":function(){
		//s.openPopin($(this).attr('href'), 900, 500);
		s.openPopin($(this).attr('href'), 400, 330);
		return false;
	},
	"closePopin":function(){
		//$('body').css('overflow', 'auto');
		$('.popin-overlay,.popin-content').remove();
	},
	/*"showBackgroundFlash":function(){
		var html =	'<div class="page-background" style="width:100%;z-index:1;">'+
					'<object style="width:100%;height:100%">'+
					'<param name="movie" value="' + baseDir + 'themes/automnehiver2011/swf/bg_' + iso + '.swf"/>'+
					'<param name="menu" value="false"/>'+
					//'<param name="scale" value="noscale"/>'+
					'<param name="wmode" value="transparent"/>'+
					'<embed src="' + baseDir + 'themes/automnehiver2011/swf/bg_' + iso + '.swf" type="application/x-shockwave-flash" wmode="transparent" allowfullscreen="true" menu="false" style="width:100%;height:100%" flashvars="" />'+ //scale="noscale"
					'</object></div>';
		$('body').prepend(html);
		var height = $(window).height() - $('.page-background').offset().top;
		height = 750; // ...
		$('.page-background').height(height);
		$(window).bind('resize', function() {
			var hB = $('body').height();
			var hW = $(window).height();
			//$('.page-background').height(hB < hW ? hW : hB);
		});
	},*/
	"initShare":function(){
		$('.product-share-inner').bind('mouseenter', function(){
			$('.product-share-outer').removeClass('product-share-hide');
		});
		$('.product-share-outer-content').bind('mouseleave', function(){
			$('.product-share-outer').addClass('product-share-hide');
		});
	},
	"initPopin":function(){
		$('.popin').bind('click', s.openPopin);
		$('.popin-cms').bind('click', s.openCMSPopin);
	},
	"initMenu":function(){
		$('.header-nav ul li').hover(function() {
			$(this).parent('ul').siblings('.preview').css('background-image', ($('img', $(this)).attr('src') ? 'url(\''+$('img', $(this)).attr('src')+'\')' : 'none'));
			$(this).parent('ul').parent('.header-nav').addClass('header-nav-active');
			$(this).children('.header-sub-nav').show();
		}, function() {
			$(this).parent('ul').parent('.header-nav').removeClass('header-nav-active');
			$(this).children('.header-sub-nav').hide();
		});
                
                $('.header-sub-nav-content ul li').hover(function() {
                    if ($(this).parent().parent().find('.preview').css('background-image') != 'none')
                    {
                        $(this).parent().parent().find('.preview').show();
                        
                    }
		}, function() {
			$(this).parent().parent().find('.preview').hide();
		});
                $('.header-nav ul .preview').hide();
	},
	"initPlaceholder":function(){
		//s.placeholder = $
		var el = $('input[name=search_query]');
		var initVal = el.val();
		el.bind('focus', function(){
			if (el.val() == initVal) {
				el.val('');
			}
		}).bind('blur', function(){
			if (el.val() == '') {
				el.val(initVal);
			}
		});
	},
	"initBlog":function(){
		var elA = $('.footer-blog');
		var elB = $('.footer-blog-posts-autohide');
		elA.mouseover(function(){
			elB.show();
		});
		elB.bind('mouseleave', function(){
			elB.hide();
		});
	},
	"initRollOver":function(){
            /*jQuery('.over').each(function(){
                jQuery(this).children('a').click(function(){
                    jQuery('.over').children('a').removeClass('current');
                    jQuery('.hover').each(function(){
                        jQuery(this).attr('src', jQuery(this).attr('src').replace('_r', ''));
                    });
                    jQuery(this).addClass('current');
                    jQuery(this).children('img').attr('src', jQuery(this).children('img').attr('src').replace('.png', '_r.png'));
                    $('.details').hide();
                    $('#cms_'+jQuery(this).attr('id')).show();
                });
            });*/
	    jQuery('.hover').hover(function(){
		var that = jQuery(this);
                if(!jQuery(this).parent().hasClass('current')) that.attr('src', that.attr('src').replace('.png', '_r.png'));
	    }, function(){
		var that = jQuery(this);
                if(!jQuery(this).parent().hasClass('current')) that.attr('src', that.attr('src').replace('_r', ''));
		
	    });
	},
	/*"initBackground":function(){
	    if ($('body').attr('id') == 'index') {
		s.showBackgroundFlash();
	    }
	},*/
	"initBubble":function(){
		if (jQuery('#category').length || jQuery('#search').length) {
			jQuery('.roll-link').hide();
			jQuery('.info').hide();
			jQuery('.block').bind('mouseenter', function(e){
				var bubble = jQuery(this).children('.info');
				var height = bubble.height();
				var width = bubble.width();
				$(this).mousemove(function(e){
					var o = $(this).offset();
					var x = (e.pageX - o.left) - 50;
			        var y = (e.pageY - o.top) - (height + 5);
			        bubble.css({"top":y,"left":x});
				});
				bubble.show();
				jQuery(this).children('.roll-link').show();
				jQuery(this).children('.list').hide();
			});
			jQuery('.block').bind('mouseleave', function(){
				jQuery(this).children('.list').show();
				jQuery(this).children('.roll-link').hide();
				jQuery(this).children('.info').hide();
			});
		}
	},
	"init":function(){
		s.initMenu();
		s.initPopin();
		s.initPlaceholder();
		s.initBlog();
		s.initShare();
		s.initRollOver();
		/*s.initBackground();*/
		s.initBubble();
	}
};
// Module Color
var c = {
	"change":function(el){
		var id = $(el).children('a').attr('rel');
		var img = baseDir + 'modules/colors/images/h_' + id + '.jpg';
		//var imgHp = baseDir + 'modules/colors/images/hp_' + id + '.jpg';
		//$('#index').css('backgroundImage', 'url(' + imgHp + ')');
		$('.homepage-module-color').css('backgroundImage', 'url(' + img + ')');
		$('.color-inner').removeClass('color-inner-active');
		$(el).addClass('color-inner-active');
	},
	"autoChange":function(iEl){
		iEl++;
		var cI = $('.color-inner');
		if (iEl > (parseInt(cI.length) - 1)) iEl = 0;
		var el = cI.eq(iEl);
		c.change(el);
		setTimeout('c.autoChange(' + iEl + ')', 5000);
	},
	"init":function(){
		c.autoChange(-1);
	}
};
jQuery(document).ready(s.init);
jQuery(document).ready(c.init);
