/*
	This file will not function correctly with the prototype and scriptaculous javascript frameworks.
	It's important that those 2 files be included before global.js is included.
	Prototype:  http://www.prototypejs.org	
	Scriptaculous: http://script.aculo.us/
	
 */

/*******************
 Global Variables
********************/
var leftNavPopOut = new Template("<a class=\"menu-link\" href=\"#{link}\">#{name} (#{product_count})</a>");
var leftNavTimeout;

/*
 * Add a zoom icon to an image with a class of addZoom.
 * This function needs updated using prototypejs
 */
function addZoomIcon(positionX, positionY){

	var elements = $$('.addZoom');	
	for(var i = 0; i < elements.length; i++){		
		elements[i].insert({after: new Element('img', {
			src: sitePath + 'custom/images/glass.png',
			style:'position:absolute; display:inline; bottom: ' + positionX + 'px; left: ' +positionY + 'px; border: 0;'
			})
		});
	}	
}

//This function logs javascript errors that could occur.  Please use this with a try catch block.
function processJavaScriptError(message, url, line){
	new Ajax.Request('/ajax/logError.do', {
		method: 'get',
		parameters: {
			url: url,
			line: line,
			message: message
		}
	});
}


/*
	Set up last ditch effort to log errors.  JavaScript errors should be captured using try
	catch blocks.  The onerror event is only supported by IE and fireFox browsers.
*/
//window.onerror = processJavaScriptError;

// Execute some code once the dom has loaded.
document.observe('dom:loaded', function() {
	/*
	 * If the left nav exists create the category menus.
	 */
	if($('left-nav')){
		$('left-nav').insert(new Element('div', { 'id':'category_menu', 'class':'backdrop'}));
		$('category_menu').setStyle({display: 'none'});
		$('category_menu').absolutize();
		
		$$('#left-nav a').each(function(elem) {
			elem.observe('mouseover', function(event) {
				event.stop();
				window.clearTimeout(leftNavTimeout);			
				showLeftNav.delay(0.2, elem.identify());
			});
			elem.observe('mouseout', function(event) {
				event.stop();
				leftNavTimeout = Element.hide.delay(0.2, 'category_menu');	
			});
		});
		
		$('category_menu').observe('mouseover', function(event) {
			event.stop();
			window.clearTimeout(leftNavTimeout);
		});
		
		$('category_menu').observe('mouseout', function(event) {
			event.stop();
			leftNavTimeout = Element.hide.delay(0.2, 'category_menu');	
		});
		
		$$('.tabContainer').each(function(elem){
			new panelControl(elem);
		});
		
		addZoomIcon(0,0);
	}
	
	/*
	 * If the bookmark link exist add it's functionality.
	 */
	if($('bookmark_link')){
		$('bookmark_link').observe('click', function(event){
			event.stop();
			bookmarkPage();
		});
		
		$('bookmark_link').show();
	}
	
	
	
	//Maddness!  Find all captcha class elements, add an image after it,
	//and bind an event to each image that will refresh the captcha elements.
	$$('.captcha_image').each(function(elem){
	
		//Create refresh element.
		var refElem = new Element('div', {'id':'captcha_refresh'});
		refElem.observe('click', function(event){
			event.stop();		
			$$('.captcha_image').each(function(elem){					
				elem.src = elem.src.split('?', 1) + '?timestamp=' + Math.floor(Math.random() * 101);
			});
		});
		
		elem.insert({
			after:
			refElem			
		});
	});
	
	//Add javascript tracking to CI buttons.
    $$('a.bnb').each(function(elem) {
        elem.observe('click', function(event){
        	event.stop();
            recordOutboundLink(elem, 'Buy Now Button', elem.href.toQueryParams().sSKU);
        });
    });
});


function showLeftNav(elemID) {
	var tmpLinksStr = '';

	if(z_catalog[elemID].categories != null){
		z_catalog[elemID].categories.split(',').each(function(cat){
			tmpLinksStr += leftNavPopOut.evaluate(z_catalog[cat]);
		});
		
		$('category_menu').show();
		$('category_menu').style.left= $(elemID).cumulativeOffset()[0] + 200 + 'px';		
		$('category_menu').style.top= $(elemID).cumulativeOffset()[1] + 'px';		
		$('category_menu').update(tmpLinksStr);				
		
	} else {
		$('category_menu').hide();
	}

}


function bookmarkPage()
{	
	if(window.sidebar)
		window.sidebar.addPanel('Cables To Go', document.location, '');
	else if(window.external)
		window.external.AddFavorite(document.location, 'Cables To Go');	
}


//Used for GA trackong on the CI buy now button.
function recordOutboundLink(link, category, action) {
    try {
        var pageTracker=_gat._getTracker("UA-3064065-4");
        pageTracker._trackEvent(category, action);
        //setTimeout('document.location = "' + link.href + '"', 100);
        window.open(link, "", "status=0, toolbar=0, location=0, scrollbars=1, resizable=1, height=600, width=800");
    } catch(err) {}
}
