/*
-----------------------------------------------
Site JavaScript
Site: elemental.uk.com
Version: 06 August 2009
----------------------------------------------- */

var magImageIndexer = 0;

/* Sliding Nav (req: mootools.js v1.11)
----------------------------------------------- */
window.addEvent('domready', function() {
	var prod = new Fx.Slide('products');
	prod.options.duration = 0;
	prod.hide();
	$$('.MGroupList').setStyle('display', 'block');
	$$('div.MGroup').each(function(div) {
		var link = div.getElement('a');
		var block = link.getNext();		
		var fx = new Fx.Slide(block);
		fx.options.duration = 0;
		fx.hide();
		link.addEvent('click', function() {
			fx.toggle();
		});
	});
	
	$('toggle').addEvent('click', function(e){
		$('products').setStyles('display: block');
		e = new Event(e);
		prod.toggle();
		e.stop();
		
	});
});
document.write('<style type="text/css" media="screen">.MGroupList{display: none}</style>');

/* IE6 PNG fix (req: iepngfix.htc)
----------------------------------------------- */
window.addEvent('domready', function() {
	if (document.all && document.styleSheets && document.styleSheets[0] && document.styleSheets[0].addRule) {
		document.styleSheets[0].addRule('img', 'behavior: url(/js/iepngfix.htc)');
		document.styleSheets[0].addRule('div', 'behavior: url(/js/iepngfix.htc)');
	}
});

/* SWF Embedding (req: swfobject.js v2.2)
----------------------------------------------- */
var flash_gallery = '';
var params = { wmode: "transparent" };
function loadHomeFlash(root) {
	swfobject.embedSWF("home.swf?123", "HomeFlash", "740", "560", "8.0.0", false, false, params);
}

function loadGalleryFlash(root, catname, prodid) {
	var flashvars = { cat: catname, id: prodid, rootPath: root };
	swfobject.embedSWF(root + "gallery.swf?123", "gallery", "980", "220", "8.0.0", false, flashvars, params);
    flash_gallery = document.getElementById("gallery");
}

function loadMosaicFlash(root) {
	swfobject.embedSWF("mosaic.swf", "maincontent", "740", "560", "8.0.0", false, false, params);
}

function loadMosaicGallery(root, catname, prodid) {
	var flashvars = { cat: catname, id: prodid };
	swfobject.embedSWF(root + "mosaicgallery.swf", "gallery", "740", "160", "8.0.0", false, flashvars, params);
    flash_gallery = document.getElementById("gallery");
}


var ajaxLoadProductName = '';
function doSearch(root) {
    if ($('searchbox').value == '') {
        $('searchbox').focus();
    } else {
        window.location = root + 'search.aspx?s=' + $('searchbox').value;
    }
    return false;
}

function showHideLoadingImage() {
//Used to show or hide the loading image
    $('tellafriend').style.visibility = 'hidden';
    if ($('loading').style.display !== null) {
        if ($('loading').style.display == 'none') {
            $('loading').style.display = 'inline';
        } else {
            $('loading').style.display = 'none';
        }
    }
    
    i = document.title.lastIndexOf(':');
    if (i > -1) {
        document.title = document.title.substring(0, i) + ': ' + ajaxLoadProductName;
    }
}

function ajax_GetWebControl(root, ctl, updateCtl, querystring) {
//AJAX call to load in a web control.
//Hides the loading image on completion
//  ctl: AJAX handler to identify wich control to load
//  updateCtl: Name of the control holding the content
//  querystring: Additional parameters to send to the load control. If not required use ''
    ajaxwebcontrol = new Ajax(root + 'AJAX/ajaxWebControlHTML.aspx?control='+ctl+'&'+querystring, {
        method: 'get',
        update: $(updateCtl),
        onComplete: showHideLoadingImage
    }).request();
}

function loadAjaxProduct(root, pid, pname) {
//Calls the ajax control loader
//Display the loading image if it's not already showing
//  pid: The product id to show
    magImageIndexer = 0;
    ajaxLoadProductName = pname;
    updateCtl = 'productpane';
    if ($(updateCtl) !== null) { $(updateCtl).empty(); }
    $('loading').style.display = 'inline';
    $('hidTellFriendProdID').value = pid;
    ajax_GetWebControl(root, 'product', updateCtl, 'id=' + pid);
}

function checkEnter(c) {
//Checks for enter key being pressed and fires .net postback on given control
    var characterCode;
    e = event;
    characterCode = e.keyCode;
    if(characterCode == 13) {
        __doPostBack(c,'');
    }
}

function crossBrowserLinkClick(element) {
	if (element.click) {
		element.click();
	} else {
	    if (element.onclick !== null) {
	        var onclick = element.onclick.toString();
	        if (onclick.indexOf('{') > -1)
	            onclick = onclick.substr(onclick.indexOf('{')+1, onclick.indexOf('}') - (onclick.indexOf('{')+1) - 1);
	            
	        if (onclick.indexOf('\n') > -1)
	            onclick = onclick.replace(/\n/, '');
	        
	        onclick = onclick.replace(/\s*/, '');
	        eval(onclick);
	    } else {
		    var href = element.href;
		    var func;
    		
		    if (href.indexOf('javascript:') > -1)
			    func = href.substr(11);
		    else
			    func = href;
    			
		    //strip the space off (encoded as %20)		
		    if (func.substr(0, 3) == '%20')
			    func = func.substr(3);	

		    //run the function
		    eval(func);
		}
	}
}

function thumbSwap(imgSrc, number) {
    //imgSrc = root + 'Include/GetImage.aspx?id=' + id + '&size=3&number=' + number;
    magImageIndexer = number - 1;
	$('preview').src = imgSrc;
}