// main senseo javascript

$(document).ready(function() {
	
	// js-enabled
	$("body").addClass("js-enabled");	
	
	// external links
	$("a[@rel=external]").click(function() {
		window.open(this.href);
		return false;
	});
	
	/* Begin: special edition */
	// triggers for overlay with tabs
	$('a.trigger').click(function(){
		DynamicOverlay(this, 'moreinfotabs');
		return false;
	});
	
	// link for overlay
	$('a.moreinfo').click(function(){
		DynamicOverlay(this, 'moreinfo');
		return false;
	});
		
	// gallery in overlay
	var galleries = $('.gallery');
	if(galleries.length) {
		galleries.each(function(){
			new Gallery(this);
		});
	}	
	/* End: special edition */

	// help popup
	new HelpPopups();
	
	/* QUADRANTE */
	hijaxlinks(document.getElementById("view-video"))
	
	// create FAQ
	$(".faq").append("<span class='btm'></span>");
	$(".faq").tabbedFaqList();
	
	//$("#appliance-picker").appliancePicker();
	
	// fix the included navigation for static version
	var static = true;
	if (static) {
		var url = document.location.href;
	
		// navigation constants:
		var home = "";
		var products = "/Products/";
		var story = "/Story";
		var weekly = "/Coffee%20Break/";
		var support = "Support";
		var registration = "Registration";
		var search = "Search";
		
		var boutique = "Boutique";
		var suivez = "Suivez";
		var commande= "Commande";
		var articles= "Articles";
		var tarifs= "Tarifs";
		var collector= "Collector";
		
		var index = 0;
		if (url.toLowerCase().indexOf(products.toLowerCase()) > 0) index = 1;
		else if (url.toLowerCase().indexOf(story.toLowerCase()) > 0) index = 3;
		else if (url.toLowerCase().indexOf(weekly.toLowerCase()) > 0) index = 4;
		else if (url.toLowerCase().indexOf(support.toLowerCase()) > 0) index = 5;
		
		else if (url.toLowerCase().indexOf(boutique .toLowerCase()) > 0) index = 2;
		else if (url.toLowerCase().indexOf(suivez .toLowerCase()) > 0) index = 2;
		else if (url.toLowerCase().indexOf(commande.toLowerCase()) > 0) index = 2;
		else if (url.toLowerCase().indexOf(articles.toLowerCase()) > 0) index = 2;
		else if (url.toLowerCase().indexOf(tarifs.toLowerCase()) > 0) index = 2;
		else if (url.toLowerCase().indexOf(collector.toLowerCase()) > 0) index = 2;

		else if (url.toLowerCase().indexOf(registration.toLowerCase()) > 0) index = -1;
		else if (url.toLowerCase().indexOf(search.toLowerCase()) > 0) index = -1;
	
		$("#nav-section ul li").removeClass("current");
		$("#nav-section ul li:eq(" + index + ")").addClass("current");
	}

});

HelpPopups = function(root) {
	var self = this;
	if(!root) root = $(document);
	this.targetPopup = null;
	this.open = function(sourceLink) {
		var popupHeight = $("#" + sourceLink.attr("href").split("#")[1]).height();
		this.targetPopup = $("#" + sourceLink.attr("href").split("#")[1])
		this.targetPopup.css({ top: sourceLink.offset().top - $("#column-main").offset().top - popupHeight/2 - 5 + "px", left: sourceLink.offset().left - $("#column-main").offset().left + 40 + "px" });
	}
	this.close = function() {
		var self = this;
		if(!this.targetPopup) return;
		this.targetPopup.css({ left: "-999em" }); 
		this.targetPopup = null;	
	}

	// get vaiables you need
	$('.help-popup').hide();
	$('.help-popup .content').prepend('<a href="#" title="Close popup" class="close">Close</a>');
	$('.help-popup .content .close').click(function(e) {
		$('.help-popup').hide();
		e.preventDefault();
	});
	$("a.help").click(function(e) {
		var showing = $(this.hash).css('display');
		$('.help-popup').hide();
		if(showing == 'none')
		$(this.hash).show();
		self.open($(this));
		e.preventDefault();
	});
	
}

/* QUADRANTE */
function hijaxlinks(scope){
    $('a.videolink',scope).click(function(event){
        newContentInlinePopUp({href:this.getAttribute("href"),title:this.getAttribute("title"),type:"videolink"})
        event.preventDefault();
    })
    $('a.imagelink',scope).click(function(event){
        newContentInlinePopUp({href:this.getAttribute("href"),type:"imagelink"})
        event.preventDefault();
    })	
}

function syncPositionPopupToImageSize(objImg){
    $("#blockMsg").css({marginLeft:(-0.5*objImg.width+"px"),height:(objImg.height+"px")})
}

/* Begin: special edition */
/* Slideshow */
var Gallery = function(root) {
	$(".thumbs a", root).click(function(e) {
		var strImg = $("img", this).attr("src");
		var arrImg = strImg.split(".");
		
		$(".image img", root).attr("src", arrImg[0] + "." + arrImg[1]);
		
		e.preventDefault();
	});
}

/* Dynamic overlay */
var DynamicOverlay = function(link, id) {
	
	var $target = $('#' + id);
	var $overlay = $('#overlay');
	var $win = $(window);
	var $content = $('#content');

	var contentTop = $content.offset().top;
	var height = $(document).height();
	var top = $win.scrollTop() - contentTop + ($win.height() - 450)/2;
	var left = ($content.width() - 700)/2 
	
	var availTop = $content[0].offsetHeight - 450;
	if(top > availTop) {
		top = availTop
	} 

	$target.css({ left: left+'px', top:top+'px'});
	$overlay.css({ height: height + 'px' });
	$overlay.show();

	var $links = $target.find('.page_navigation a, a.button.video');
	function activateTab(hash) {
		var $item = $links.filter('[href$='+hash+']').parent();
		$item.siblings('li').removeClass('current');
		$item.addClass('current');

		var $tab = $(hash);
		$tab.siblings('.content').hide();
		$tab.show();
	}

	if(!$target[0].ranOnce) {
		$target[0].ranOnce = true;
		
		$links.click(function(){
			activateTab(this.hash);
			return false;
		});
		
		$target.find('a.close').click(function(){
			$overlay.hide();
			$target.fadeOut();
			return false;
		});
	}

	activateTab(link.hash);
	$target.fadeIn();
};
/* End: special edition */

