(function($, window, document) {

function stop(e) {
	if (!e) e = window.event;
	(e.stopPropagation) ? e.stopPropagation() : e.cancelBubble = true;
	(e.preventDefault) ? e.preventDefault() : e.returnValue = false;
	return false;
}

function prevDef(e) {
	if (!e) e = window.event;
	(e.preventDefault) ? e.preventDefault() : e.returnValue = false;
	return false;
}

$(document).ready(function () {
    //Remove noJs bodyclass
    if (typeof Sitecore == 'undefined') {
        $("body").removeClass("noJs").addClass("activeJs");
    }
    //add print button if needed
    if ($('.print').length != 0) { $('.print').printButton(); }
    //init tabs
    if (($('#dl-tabs2').length != 0) && (typeof Sitecore == 'undefined')) { inittabs2($("#dl-tabs2"), $(".tabbedcontent")); }
    // init tabs that still need to work with #
    if (($('#dl-tabs').length != 0) && (typeof Sitecore == 'undefined')) { inittabs($("#dl-tabs"), $(".tabbedcontent")); }


    //init guideblock
    if ($('#ctrGuides').length != 0) { initGuides(); }

    initGA();
});
	

//#tabs

function inittabs(tabs,tabcontent) {
	
	//hide tab contents except active or first one
	
	var activetab = $("li", $(tabs)).index($('.on'));
	
	if (activetab > -1)
	{
		$("li.tabcontent:not(:eq(" + activetab + "))", $(tabcontent)).hide();
		$("li.tabcontent:eq(" + activetab + ")", $(tabcontent)).show();
	}
	else { //if no active tabs set, open with first one
		$("li.tabcontent:not(:eq(0))",$(tabcontent)).hide();
		$("li:first",$(tabs)).addClass("on");
	}
	
	//associate tabs with tabbed content
	$("li a",$(tabs)).each(function(intIndex){
		$(this).attr('href', 'javascript:void(0)').click(function(){
			//hide all tab content
			$("li.tabcontent",$(tabcontent)).hide();
			//show content of clicked tab
			$("li.tabcontent:eq("+intIndex+")",$(tabcontent)).show();
			$(this).parent().addClass("on").siblings().removeClass("on");
		});
	})

}
function inittabs2(tabs, tabcontent) {

    //hide tab contents except active or first one
    
    var pages = tabcontent.children("li"), activetab = tabs.find("li.on");
    if (activetab.length === 0) { 
      activetab = tabs.find("li").first().addClass("on");
    }
    pages.not(pages.eq(activetab.index()).show()).hide();
    
}

function initGA() { //Google Analytics

    //external links
    var mailto = /^mailto:/i, javascript = /^javascript:/i, download = /\.(pdf|doc|docx|zip|rar|7z)$/i;
    $.expr[':'].external = function (obj) {
        return !obj.href.match(mailto) && !obj.href.match(javascript) && obj.hostname !== document.location.hostname && obj.hostname.indexOf(".gh.") === -1;
    };
    $.expr[':'].mailto = function (obj) {
        return !!obj.href.match(mailto);
    };
    $.expr[':'].download = function (obj) {
        return !!obj.href.match(download);
    };

    var createEventTracker = function(name) {
        return function() {
            if (window._gaq) {
                window._gaq.push(['_trackEvent', 'Hyperlinks', 'Clicked ' + $(this).attr('href'), name]);
            }
            else if (window.console && window.console.log) {
                console.log("no tracking code found");
            }
        };
    };

    //_trackEvent(category, action, opt_label, opt_value)
    $('a:external').click(createEventTracker("External link"));
    $('a:mailto').click(createEventTracker("Mailto link"));
    $('a:download').click(createEventTracker("Download link"));

}

function initGuides() {

	//var slideTime = 200;
    var waitTime = 3000;
    var slideTimer = null;
    //if(slideTime>=waitTime){slideTime=waitTime}
	
	function nextSlide(index) {
	
		var target = $(".guide:eq("+index+")","#ctrGuides");		
		
		$(target).switchClass('guidenormal','guideactive', 30);
		$(target).siblings().switchClass('guideactive','guidenormal', 50);
					  
		//toggle the persona images!
		var theindex = $('#ctrGuides li.guide').index($(target));
		$("li:eq(" + theindex + ")","#ctrGuideImages").siblings().fadeOut(500).hide().end().fadeIn(500);			
		
	}
	
	function startSlider() {
		clearInterval(slideTimer);
		//disable images except the active one (initialy randomized by XSL rendering)
		var theindex = $('#ctrGuides li.guide').index($('#ctrGuides li.guideactive'));
		$("li:not(:eq(" + theindex + "))","#ctrGuideImages").hide();
		slideTimer = setInterval(function() { 
			nextSlide(theindex);
			theindex = (theindex<2? theindex = theindex+1 : theindex=0 ); 
		}, waitTime)
	}	
	//set behaviour when mouse is moved onto guide block
	$(".guide","#ctrGuides").mouseenter(function(e){	
		clearInterval(slideTimer);
		var theindex = $('#ctrGuides li.guide').index(this);
		nextSlide(theindex);
	})
	//set behaviour when mouse is moved away from guide block
	$(".guide","#ctrGuides").mouseleave(function(e){	
		clearInterval(slideTimer);
		startSlider();
	})	
	
	startSlider();
};

}(this.jQuery.noConflict(), this, this.document));
