﻿/// <reference path="mootools.js" />
/// <reference path="swfobject.js" />

var anchorSet = null;
var defaultAnchorSetOnPage = null;
var anchorArray = new Array();
var tabPrefix = 'tab-';

var SiteFramework = new Class({
    initialize : function() {
        window.addEvent('domready', this.domReady.bind(this));
	    this.fixRollovers();
    },
    
    attachRandomFlash : function(element,baseurl,width,height,maxRand,clickTag) {
        var promo = $(element);
        if (promo) {
            var promo_num = Math.floor(Math.random()*maxRand)+1;
            this.attachFlash(element, baseurl + promo_num + '.swf', width, height, clickTag);
        }
    },
    
    attachRandomLoader : function(element,loaderurl,baseurl,width,height,maxRand,clickTag) {
        var promo = $(element);
        if (promo) {
            var promo_num = Math.floor(Math.random()*maxRand)+1;
            this.attachLoader(element,loaderurl, baseurl + promo_num + '.swf', width, height, clickTag);
        }
    },
    attachLoader : function(element,loaderurl,fullurl,width,height,clickTag,noTransparency) {
        var fo = new SWFObject(loaderurl, element, width, height, 8);
        if (!noTransparency)
		    fo.addParam('wmode', 'transparent');
	    fo.addParam('quality', 'high');
	    fo.addParam('scale', 'noscale');
	    fo.addVariable('swf',fullurl) ;
	    fo.addVariable('clickTAG', clickTag);
	    fo.write(element);
    },
    attachFlash : function(element,fullurl,width,height,clickTag,noTransparency) {
        var fo = new SWFObject(fullurl, element, width, height, 8);
        if (!noTransparency)
		    fo.addParam('wmode', 'transparent');
	    fo.addParam('quality', 'high');
	    fo.addParam('scale', 'noscale');
	    fo.addVariable('clickTAG', clickTag);
	    fo.write(element);
    },
    attachFlash2 : function(element,fullurl,width,height,clickTag0,clickTag1,noTransparency) {
        var fo = new SWFObject(fullurl, element, width, height, 8);
        if (!noTransparency)
		    fo.addParam('wmode', 'transparent');
	    fo.addParam('quality', 'high');
	    fo.addParam('scale', 'noscale');
	    fo.addVariable('CLICKtag01', clickTag0);
	    fo.addVariable('CLICKtag02', clickTag1);
	    fo.write(element);
    },
    attachAccordian : function() {
        var questions = $$('.question');
		if (questions.length > 0)
		{
			this.Accordian = new Object();
			this.Accordian.Slider = new Accordion(questions,$$('.answer'),{display: -1, alwaysHide: true});
			this.Accordian.Slider.addEvent('onActive',function(el) { (el).addClass('question-open'); });
			this.Accordian.Slider.addEvent('onBackground',function(el) { $(el).removeClass('question-open'); });
            //questions[0].addClass('question-open');
		}
    },    
    attachToolTips : function() {
   // alert($$('.tooltip').length > 0);
		if($$('.tooltip').length > 0)
		{
		//alert('attach tips');
			var myTips = new Tips('.tooltip',{
				className: 'tipcontainer'
				});
		}
    },
	initialShowTabbedRecipe: function() {
		//alert('initialShowTabbedRecipe');
		doShowHide();			
	},
	attachShowHideTrigger : function() {
		$$('.listitem-recipe-tabs li a').each(function(a) {
			//add a click event to the product attribute checkboxes
			a.addEvent('click', doShowHide);
		}.bind(this));
	},

	
	checkQuerystring : function() {
		//alert(anchorSet + " - checkQuerystring");
		if(defaultAnchorSetOnPage)
		{
			setInterval( function() {
				if( anchorSet != getAnchor() && arrayContains(anchorArray, getAnchor()))
				{
					doShowHide();				
				}
			}, 150);
		}
	},

    domReady : function() {
		this.attachAccordian();
		this.attachToolTips();
		this.initialShowTabbedRecipe();
		this.attachShowHideTrigger();
		this.checkQuerystring();
    },
    
    
    fixRollovers : function() {
        try {
          document.execCommand("BackgroundImageCache", false, true);
        } catch(err) {}
    }
});

var Site = new SiteFramework();


//show a selected div
function showSelected(className) {
	$$('.'+className).each(function(thisContainer) {
		thisContainer.removeClass('hide');
		thisContainer.addClass('show');
	});
}


//set an initial anchor
//defaultAnchorSetOnPage should be set on the page loading the tab switcher (as this code is re-used throughout the site)
function getAnchor() {
	var anchor = $get("#");
	if(!anchor)
		anchor = defaultAnchorSetOnPage;
	return anchor;
}


//turn on a graphical tab
function tabOn(className) {
	var tabElement = $(tabPrefix + className);
	if(tabElement)
		tabElement.addClass('selected');
}


//hide a recipe tab and show another one
function doShowHide() 
{
	var loadAnchorArray = anchorArray.length == 0;
	var i = 0;
	//hide all the recipes initially
	$$('.tabbed-recipe').each(function(thisContainer) {
		thisContainer.removeClass('show');
		thisContainer.addClass('hide');
	});		


	//loop all the tabs and turn off the 'selected' class
	$$('ul.listitem-recipe-tabs li').each(function(thisContainer) {
		if(loadAnchorArray)
		{
			anchorArray[i] = thisContainer.id.replace(tabPrefix, '');
			i++;
		}	
		
		thisContainer.removeClass('selected');
		
	});

	//find out which one has been clicked
	//wait to ensure the html anchor has been updated
	//otherwise it gets the one that was there already
	//hide previous, show new

	setTimeout ( 
		function() {
			anchorSet = getAnchor();
		}
		, 20 );

	setTimeout ( 
		function() {
			if(!arrayContains(anchorArray, anchorSet))
				anchorSet = defaultAnchorSetOnPage;
			showSelected(anchorSet);
			tabOn(anchorSet);
			
		}
		, 21 );
}


/*
http://webfreak.no/wp/2007/09/05/get-for-mootools-a-way-to-read-get-variables-with-javascript-in-mootools/

Function: $get
	This function provides access to the "get" variable scope + the element anchor

Version: 1.3

Arguments:
	key - string; optional; the parameter key to search for in the url's query string (can also be "#" for the element anchor)
	url - url; optional; the url to check for "key" in, location.href is default

Example:
	>$get("foo","http://example.com/?foo=bar"); //returns "bar"
	>$get("foo"); //returns the value of the "foo" variable if it's present in the current url(location.href)
	>$get("#","http://example.com/#moo"); //returns "moo"
	>$get("#"); //returns the element anchor if any, but from the current url (location.href)
	>$get(,"http://example.com/?foo=bar&bar=foo"); //returns {foo:'bar',bar:'foo'}
	>$get(,"http://example.com/?foo=bar&bar=foo#moo"); //returns {foo:'bar',bar:'foo',hash:'moo'}
	>$get(); //returns same as above, but from the current url (location.href)
	>$get("?"); //returns the query string (without ? and element anchor) from the current url (location.href)

Returns:
	Returns the value of the variable form the provided key, or an object with the current GET variables plus the element anchor (if any)
	Returns "" if the variable is not present in the given query string

Credits:
		Regex from [url=http://www.netlobo.com/url_query_string_javascript.html]http://www.netlobo.com/url_query_string_javascript.html[/url]
		Function by Jens Anders Bakke, webfreak.no
*/
function $get(key,url){
	if(arguments.length < 2) url =location.href;
	if(arguments.length > 0 && key != ""){
		if(key == "#"){
			var regex = new RegExp("[#]([^$]*)");
		} else if(key == "?"){
			var regex = new RegExp("[?]([^#$]*)");
		} else {
			var regex = new RegExp("[?&]"+key+"=([^&#]*)");
		}
		var results = regex.exec(url);
		//alert(results);
		return (results == null )? "" : results[1];
	} else {
		url = url.split("?");
		var results = {};
			if(url.length > 1){
				url = url[1].split("#");
				if(url.length > 1) results["hash"] = url[1];
				url[0].split("&").each(function(item,index){
					item = item.split("=");
					results[item[0]] = item[1];
				});
			}
		return results;
	}
}

function arrayContains(a, obj) {
  var i = a.length;
  while (i--) {
    if (a[i] === obj) {
      return true;
    }
  }
  return false;
}






