;(function($){
	
	window.dsg = { fn: {} };
	
	
	dsg.fn.initHeader = function(){
		
		var jHeaderTop = $('#header-top'),
			jServices = jHeaderTop.find('.box-service .box-content'),
			jServicesAnchors = jServices.prev('.box-header'),
			jNav = $('#navigation'),
			jAlphaSearch = jNav.find('.alpha-prd-search'),
			jAlphaListCt,
			jTabNavs = jNav.find('> li').not(jAlphaSearch),
			jTabNavCts = jNav.find('.nav-content'),
			sHeaderHeight = $('#header').outerHeight(),
			tserviceTimer,
			tTabTimer;
		
		jTabNavs.each(function(){
			var jTabNav = $(this),
				sTabNavPos = jTabNav.position(),
				sTabNavWidth = jTabNav.outerWidth(),
				jRelCt = jTabNav.find('.nav-content'),
				sRelCtWidth = jRelCt.width(),
				sFeaturePos = jRelCt.find('.features').position(),
				sCtHeight = jRelCt.height();
			
			if ( $.browser.msie && $.browser.version < 7 ){
				var jSection = jRelCt.find('.section'),
					sSectionLength = jSection.length,
					sSectionWidth = jSection.outerWidth();
				sRelCtWidth = sSectionLength*sSectionWidth
				jRelCt.css({width:sRelCtWidth+'px'});
			}
			
			if (sFeaturePos){
				var sPosDiff = sFeaturePos.left - (sTabNavPos.left + sTabNavWidth);
				if ( sPosDiff < 60 || sPosDiff < -60 ) jRelCt.css({ left: (50 - sPosDiff) + 'px'});
				var sNewPos = jRelCt.position();
				if ( (sNewPos.left + sRelCtWidth) > 980 ) jRelCt.css({left:'auto', right: '10px'});
			}
			
			jTabNav.bind({
					expand : function(){
						if ($('#header .box-alpha-search').length) $('.box-alpha-search').hide(); jAlphaSearch.removeClass('alpha-active');
						jTabNav.addClass('active');
						jRelCt.show().stop().animate({opacity:1}, 200);
					}, 
					collapse : function(){
						jTabNavs.removeClass('active');
						jTabNavCts.stop().hide().css({opacity:0});
					}, 
					mouseenter : function(){ 
						window.clearTimeout(tTabTimer);
						tTabTimer = window.setTimeout(function(){ 
							jTabNavCts.not(jTabNav).trigger('collapse');
							jTabNav.trigger('expand');  
						},300);
					},
					mouseleave : function(){ 
						window.clearTimeout(tTabTimer);
						tTabTimer = window.setTimeout(function(){ jTabNav.trigger('collapse');  },300);
					}
				})
				.find('a')
					.focus(function(){ jTabNav.trigger('expand'); });
				
			jTabNav.find('a:last').blur(function(){ jTabNav.trigger('collapse'); });
			if ( !($.browser.msie && $.browser.version < 7) ) jRelCt.find('.section').css({height: sCtHeight - 25 + 'px'});
		});
		
		jTabNavCts.hide();
		
		jServices
			.each(function(){
				var jService = $(this),
					jControler = jService.prev('.box-header');
					
				jService.bind({
					collapse : function(){
						jService.addClass('collapsed').hide();
						jControler.removeClass('active');
						jControler.blur();
						$('html').unbind('click');
					},
					expand : function(){
						jService.removeClass('collapsed').show();
						jControler.addClass('active');
						$('html').bind('click', function(){ jService.trigger('collapse'); });
					},
					mouseleave : function(){
						tserviceTimer = window.setTimeout(function(){ jService.trigger('collapse') }, 1500);
					},
					mouseenter : function(){
						window.clearTimeout(tserviceTimer);
					}
				});
				
				jControler.click(function(e){
					e.preventDefault();
					e.stopPropagation();
					jServices.not(jService).trigger('collapse');
					if ( jService.hasClass('collapsed') ) jService.trigger('expand'); else jService.trigger('collapse');
				});
			})
			.click(function(e){ e.stopPropagation(); })
			.hide().addClass('collapsed');
		
		jAlphaSearch.find('a')
			.bind('mouseenter focus', function(e){
				var sUrl = $(this).attr('href');
				window.clearTimeout(tTabTimer);
				e.preventDefault();
				$(document).click(function(){
					jAlphaListCt.fadeOut(150); 
					jAlphaSearch.removeClass('alpha-active');
				});
				if (!$('#header .box-alpha-search').length) {
					$.get(sUrl, function(data){
						jAlphaListCt = $(data).find('#area-1 .box-alpha-search').hide().appendTo(jAlphaSearch);
						jAlphaListCt.fadeIn(150);
						ajaxBind = function(){
							jAlphaList = jAlphaListCt.find('.box-header a');
							jAlphaList.click(function(e){
								e.preventDefault();
								var sUrl = $(this).attr('href');
								$.get(sUrl, function(data){
									jAlphaListCt.html($(data).find('#area-1 .box-alpha-search').html());
									ajaxBind();
								});
							});
							jAlphaSearch = jNav.find('.alpha-prd-search');
						};
						jAlphaSearch.addClass('alpha-active');
						ajaxBind();
						jAlphaSearch
							.bind('mouseleave', function(){ 
								window.clearTimeout(tTabTimer);
								tTabTimer = window.setTimeout(function(){
									jAlphaListCt.fadeOut(150); 
									jAlphaSearch.removeClass('alpha-active');
								}, 150);
							});
						jAlphaListCt
							.click(function(e){
								e.stopPropagation();
							})
							.find('a:last')
								.blur(function(){ jAlphaListCt.hide(); });
					});
				} else { 
					jAlphaListCt.fadeIn(300); 
					jAlphaSearch.addClass('alpha-active');
				}
			})
			.click(function(e){ e.preventDefault(); });
	}
	
	dsg.fn.initCartSummary = function(){
		var jCartSummary = $('#cart-summary').addClass('collapsed'),
			jCartHeader = jCartSummary.find('.box-header'),
			sCartHeaderHeight = jCartHeader.outerHeight(),
			jCartContent = jCartSummary.find('.box-content').hide(),
			jCartCtl = $('<a href="#cart-summary" class="collapsed" role="button">Show more</a>').appendTo(jCartHeader.find('.order-infos .left-message')),
			jCartClose = $('<a href="#cart-summary" role="button">Close</a>').appendTo(jCartSummary.find('.check-out'));
		
		jCartContent.bind({
			expand : function(){
				jCartSummary.removeClass('collapsed').addClass('expanded');
				jCartContent.slideDown(300);
				jCartCtl.text('Show less').removeClass("collapsed").addClass("expanded");
			},
			collapse : function(){
				jCartContent.slideUp(300, function(){
					jCartSummary.removeClass('expanded').addClass('collapsed');
				});
				jCartCtl.text('Show more').removeClass("expanded").addClass("collapsed");
			}
		});
		
		jCartCtl.click(function(e){
			e.preventDefault();
			if (jCartContent.not(':animated')) {
				if ( jCartContent.is(':hidden') ) jCartContent.trigger('expand'); else jCartContent.trigger('collapse');
			}
		});
		
		jCartClose.click(function(e){
			e.preventDefault();
			jCartContent.trigger('collapse');
		});
		
	}
	
	dsg.fn.initGallery = function(box, params){
		
		var opts = $.extend({
			zoom: false
		}, params);
		
		var jGallery = $(box),
			jPrdImgCt = jGallery.parents('.prd-page-top').find('.box-prd-medias'),
			jGalleryItems = jGallery.find('li'),
			sGalleryLength = jGalleryItems.length,
			jCurrentItem = jPrdImgCt.find('.item'),
			tServiceTimer;

		if ( sGalleryLength > 4 ) jGallery.carousel({
			dispItems: 4, 
			showEmptyItems: false, 
			animSpeed: 600, 
			slideEasing : 'easeInOutQuint',
			nextBtn: '<input role="button" tabindex="0" type="button" value="next" />', 
			prevBtn: '<input role="button" tabindex="0" type="button" value="previous" />'
		});
		
		
		if (opts.zoom) { 
			var jServiceAd = $('#prd-testimony'),
				jServiceAdImg = jServiceAd.find('img'),
				sServiceHeight = jServiceAdImg.height(),
				jWrap;
				tServiceTimer;
			jServiceAdImg.bind('mouseenter mousemove', function(e){ e.stopImmediatePropagation(); });
			jCurrentItem.find('a')
				.CloudZoom({ 
					timerLength : 500,
					tint : '#000000',
					tintOpacity : 0.5,
					showCallBack: function(){ 
						if (!jServiceAdImg.is(':animated')){
							jServiceAdImg.animate({bottom: - sServiceHeight + 'px'}, 150, function(){
								jServiceAd.css({right:'-99999px'});
							});
						}
					}, 
					hideCallBack: function(){
						jServiceAd.animate({right:0}, 100, function(){
							jServiceAdImg.animate({bottom:'0'}, 150);
						});
					}
				});
		}
		
		jGalleryItems.each(function($i){
			var jItem = $(this).find('a'),
				sRelItem = jItem.attr('href'),
				jRelItem;
			
			if ($i > 0) jRelItem = $('<img src="'+sRelItem+'" class="item item-'+$i+'" alt="" width="510" height="454" />');
				else jRelItem = jPrdImgCt.find('.item:first').addClass('item-0');
			
			jItem.click(function(e){
				e.preventDefault();
				jCurrentItem.css({zIndex:'2'});
				jPrdImgCt.find('.item').not(jCurrentItem,jRelItem).hide().css({zIndex:'1'});
				if ( !(jPrdImgCt.find('img.item-'+$i).length) ) jRelItem.hide().css({zIndex:'2'}).prependTo(jPrdImgCt);
				jRelItem.hide().css({zIndex:'3'}).fadeIn(300);
				jCurrentItem = jPrdImgCt.find('.item-'+$i);
				jGalleryItems.not(':eq('+$i+')').removeClass('active');
				jGalleryItems.eq($i).addClass('active');
			});
		});
		
	}
	
	dsg.fn.initSuperbox = function(){
		$(function() {
			$.superbox.settings = { overlayOpacity: .8, closeTxt: "Close", boxWidth: "964" };
			$.superbox();
		});
	}
	
	dsg.fn.initQuickLook = function(prd,quickLookUrl,quickLookTxt){
		var sQuickLookTxt;
		if (!quickLookTxt) sQuickLookTxt = 'Quick look'; else sQuickLookTxt = quickLookTxt;
		$(prd).each(function(){
			var jPrd = $(this),
				sPrdRef = jPrd.attr('data-ajax'),
				jQLCtl = $('<a class="quick-look btn-green" href="' + quickLookUrl + '&ref=' + sPrdRef + '"><span>' + sQuickLookTxt + '</span></a>').hide().appendTo(jPrd),
				sUrl = jQLCtl.attr('href'),
				jQLCtls = $('a.quick-look'),
				jQLCt;
			
			jPrd
				.bind('mouseenter', function(){
					$('a.quick-look').not(jQLCtl).hide().css({opacity:'0'});
					jQLCtl.show().stop().animate({opacity:'1'},300);
				})
				.bind('mouseleave', function(){
					$('a.quick-look').hide().css({opacity:'0'});
					jQLCtl.hide().css({opacity:'0'});
				});
				
			jQLCtl.click(function(e){
				e.preventDefault();
				$.superbox.wait(function(){
					$.get(sUrl, function(data){
						jQLCt = $(data);
						$.superbox.open(jQLCt,{boxWidth:"952",boxHeight:'-1'});
						dsg.fn.initGallery('#area-1.box-quick-look .prd-gallery');
						dsg.fn.initCart(cartOptions);
						$('#area-1.box-quick-look').find('.media-video a').click(function(e){
							e.preventDefault();
							var sHref = $(this).attr('href'),
								jIframe = $('<iframe frameborder="0" src="'+ sHref +'" />').css({width:'500px',height:'420px'});
								$.superbox.open(jIframe,{boxWidth:'500',boxHeight:'420'});
						});
						ReevooMark.init_badges();
					});
				});
			})
		});
	}
	
	dsg.fn.initAssociated = function(prd){
		$(prd).each(function(){
			var jPrd = $(this),
				jLink = jPrd.find('.prd-name a'),
				bFirstView = true,
				sPrdUrl = jLink.attr('data-ajax');
			
			if (sPrdUrl){
				jLink
					.click(function(e){ e.stopPropagation(); })
					.simpletooltip({
						zIndex : 99,
						floatingTooltip : true, click: true,
						customTooltip: function(target){
							return tooltip = $.ajax({
								async: false,
								cache: false,
								url: sPrdUrl
							}).responseText;
						},
						showEffect: "fadeIn", hideEffect: "fadeOut",
						showCallback : function(target, tooltip){
							var sPos = $(target).offset().left + $(target).outerWidth();
							$(tooltip)
								.addClass('tooltip')
								.removeClass('floating-tooltip-right')
								.css({left: sPos +'px'})
								.fadeIn(150);
							if( bFirstView == true ) {
								var sTopPos = $(target).offset().top + ($(target).height()/2) - ($(tooltip).outerHeight()/2);
								$(tooltip).css({top : sTopPos + 'px'});
							}
							$('.tooltip').not(tooltip).remove();
							$(tooltip).click(function(e){ e.stopPropagation(); });
							$(document).click(function(){ $(tooltip).trigger('hide'); });
						},
						hideCallback : function(){ $('html').unbind('click'); bFirstView = false }, 
						hideDelay: 1
					});
			}
			
		});
	}
	
	dsg.fn.simpleTooltips = function(){
		$('a.tooltip-controler').add('a.ms-tooltip')
			.click(function(e){ 
				e.preventDefault(); 
				e.stopPropagation(); 
			})
			.simpletooltip({
				floatingTooltip : true,
				showEffect: "fadeIn", hideEffect: "fadeOut",
				showCallback : function(target, tooltip){
					$('.tooltip').not($(tooltip)).trigger('hide');
					$(tooltip).click(function(e){ e.stopPropagation(); });
					$(document).click(function(){ $(tooltip).trigger('hide'); });
				},
				hideCallback : function(){ $(document).unbind('click'); }, 
				hideDelay: 1
			});
	};
	
	$(function(){
		var jPrint = $('a[rel=print]');
		jPrint.click(function(e){
			e.preventDefault();
			window.print();
		});
	});
	
})(jQuery);
