// Script Dependancies
// popup_image_script.js

var product_banner_menu_show_count = 0;
var viewing_product_banner_menu = '';

var banner_show_timer = 200;
var banner_hide_timer = 100;
var banner_onunload_timer = 5000;
var hide_all_popup_layers_onunload_timer = 5000;

function _get_brand_url(brand_name, dir_nesting) {
	return dir_nesting + brand_name.toLowerCase().replace(/[^a-zA-Z0-9]+/g, '_') + '/';
}

function _get_subcategory_url(brand_name, category_name, dir_nesting) {
	return (category_name == '') ? _get_brand_url(brand_name, dir_nesting) : _get_brand_url(brand_name, dir_nesting) + category_name.toLowerCase().replace(/[^a-zA-Z0-9]+/g, '_') + '.php';
}

function BrandCategoriesExpandContractSideLinksOnClick(img_elem, brand_name, category_indices, category_names, dir_nesting) {
	if (img_elem.attributes.getNamedItem('value').value == '1') {
		_do_brand_categoried_contract(img_elem, brand_name, dir_nesting);
	} else {
		_do_brand_categoried_expand(img_elem, brand_name, category_indices, category_names, dir_nesting);
	}
}

function BrandCategoriesExpandContractSideLinksOnMouseOver(img_elem, dir_nesting) {
	if (img_elem.attributes.getNamedItem('value').value == '1') {
		img_elem.attributes.getNamedItem('src').value = dir_nesting + 'images/misc/sbar_contract_hover.gif';
	} else {
		img_elem.attributes.getNamedItem('src').value = dir_nesting + 'images/misc/sbar_expand_hover.gif';
	}
}

function BrandCategoriesExpandContractSideLinksOnMouseOut(img_elem, dir_nesting) {
	if (img_elem.attributes.getNamedItem('value').value == '1') {
		img_elem.attributes.getNamedItem('src').value = dir_nesting + 'images/misc/sbar_contract.gif';
	} else {
		img_elem.attributes.getNamedItem('src').value = dir_nesting + 'images/misc/sbar_expand.gif';
	}
}

function _do_brand_categoried_expand(img_elem, brand_name, category_indices, category_names, dir_nesting) {
	img_elem.attributes.getNamedItem('src').value = dir_nesting + 'images/misc/sbar_contract_hover.gif';
	img_elem.attributes.getNamedItem('value').value = '1';
	
	var ul_elem = null;
	try {
		ul_elem = document.createElement('<ul id="' + brand_name + ' subcategories' + '" class="ul_w_arrows11">');
	} catch (e) {
		ul_elem = document.createElement('ul');
		ul_elem.setAttribute('id', brand_name + ' subcategories');
		ul_elem.setAttribute('class', 'ul_w_arrows11');
	}
	if (ul_elem == null) {
		alert('DEBUG : ul_elem failed to to initialise' + e);
		return;
	}
	
	var li_elem = null;
	num = Math.min(category_names.length, category_indices.length);
	for (i = 0; i < num; i++) {
		try {
			li_elem = document.createElement('<li class="li_arrows_flush">');			
		} catch (e) {
			li_elem = document.createElement('li');
			li_elem.setAttribute('class', 'li_arrows_flush');
		}
		if (li_elem == null) {
			alert('DEBUG : li_elem failed to to initialise ' + e);
			return;
		}
		
		var a_elem = null;
		//var brand_link = 'brand.php?b=' + brand_name.replace(/ /g, '_') + '&cat=' + category_indices[i];
		var brand_link = _get_subcategory_url(brand_name, category_names[i], dir_nesting);
		try {
			a_elem = document.createElement('<a class="sbar_link" href="' + brand_link + '">');
		} catch (e) {
			a_elem = document.createElement('a');
			a_elem.setAttribute('class', 'sbar_link');
			a_elem.setAttribute('href', brand_link);
		}
		if (a_elem == null) {
			alert('DEBUG : a_elem failed to to initialise ' + e);
			return;
		}
		
		a_elem.innerHTML = category_names[i];
		li_elem.appendChild(a_elem);
		ul_elem.appendChild(li_elem);
	}
	
	
	img_elem.parentNode.parentNode.parentNode.parentNode.parentNode.appendChild(ul_elem);
	//alert(ul_elem);
}

function _do_brand_categoried_contract(img_elem, brand_name, dir_nesting) {
	img_elem.attributes.getNamedItem('src').value = dir_nesting + 'images/misc/sbar_expand_hover.gif';
	img_elem.attributes.getNamedItem('value').value = '';
	
	ul_elem = document.getElementById(brand_name + ' subcategories');
	if (ul_elem != null) ul_elem.parentNode.removeChild(ul_elem);
	else alert('DEBUG : ul tag was not initialised --> ' + brand_name + ' subcategories');
}


// ---------------- PRODUCT BANNER POPUP MENU -------------------------
function ProductBannerLinkOnClick(brand_name) {
	setTimeout("ProductBannerMenuHide('" + brand_name + "')",banner_onunload_timer);
}

function ProductBannerOnMouseOver(elem, brand_name, offset) {
	var r = getAbsolutePosition(elem);
	var left = r.x + elem.offsetWidth - offset;
	var top = r.y + elem.offsetHeight;
	
	if (navigator.appVersion.search('Safari') >= 0) {
		if (brand_name == 'specials')
			left += 336;
		else if (brand_name == 'Apollo Outdoor Equipment')
			left += 346;
		else
			left += 396;
			
		top += 100;
	} else if (navigator.userAgent.search('Navigator') >= 0) {
		left += 270;
		top += 100;
	}
	
	
	
	product_banner_menu_show_count++;
	viewing_product_banner_menu = brand_name;
	setTimeout('_do_product_banner_menu_show(' + left + ',' + top + ',"' + brand_name + '")', banner_show_timer);
}

function ProductBannerOnMouseOut(brand_name) {
	product_banner_menu_show_count--;
	setTimeout('_do_product_banner_menu_hide("' + brand_name + '")', banner_hide_timer);
}

function ProductBannerMenuOnMouseOver() {
	product_banner_menu_show_count++;
}

function ProductBannerMenuOnMouseOut(brand_name) {
	ProductBannerOnMouseOut(brand_name);
}

function _do_product_banner_menu_show(left, top, brand_name) {
	if (product_banner_menu_show_count > 0 && viewing_product_banner_menu == brand_name) {
		layer = document.getElementById(brand_name + ' product_banner_layer');
		layer.style.left = (left - layer.firstChild.offsetWidth) + 'px';
		layer.style.top = top + 'px';
		layer.style.visibility = '';
	}
}

function _do_product_banner_menu_hide(brand_name) {
	if (product_banner_menu_show_count <= 0 || viewing_product_banner_menu != brand_name) {
		layer = document.getElementById(brand_name + ' product_banner_layer');
		
		layer.style.visibility = 'hidden';
		layer.style.left = '-5000px';
		
		product_banner_menu_show_count = (product_banner_menu_show_count < 0) ? 0 : product_banner_menu_show_count;
	}
}

function ProductBannerMenuHide(brand_name) {
	product_banner_menu_show_count = 1;
	_do_product_banner_menu_hide(brand_name);
}

// ---------------- CATEGORY LISTING TUMBNAILS -------------------------
function HideAllPopupLayersOnUnload() {
	var popup_layers = document.getElementsByTagName('DIV');
	for(var i = 0; i < popup_layers.length; i++) {
		var is_popup_layer = (popup_layers[i].attributes != undefined) ? popup_layers[i].attributes.getNamedItem('popup_id') : null;
		is_popup_layer = (is_popup_layer != null && is_popup_layer.value == 'popup') ? true : false;
		if (is_popup_layer) {
			setTimeout("_do_hide_all_popup_layers_on_unload('" + popup_layers[i] + "')", hide_all_popup_layers_onunload_timer);	
		}
	}
}

function _do_hide_all_popup_layers_on_unload(popup_id) {
	var popup_layer = document.getElementById(popup_id);
	if (popup_layer != null) {
		popup_layer.style.visibility = 'hidden';
	}
}

// ---------------- CATEGORY LISTING TUMBNAILS -------------------------
function CategoryListingThumbnailImageOnMouseOver(elem_id, image_url) {
	var elem = document.getElementById(elem_id);
	elem.attributes.getNamedItem('src').value = image_url;
}

// ---------------- Add to Favourites -------------------------
function CreateBookmark(url, title) {
	if (window.sidebar) { // Mozilla Firefox & Netscape Navigator Bookmark
		window.sidebar.addPanel(title, url,"");
	} else if (window.external && (!document.createTextNode || typeof(window.external.AddFavorite) == 'unknown')) { // IE Favorite
		// IE4/Win generates an error when you execute "typeof(window.external.AddFavorite)"
        // In IE7 the page must be from a web server, not directly from a local file system, otherwise, you will get a permission denied error.
		window.external.AddFavorite( url, title);
	} else {
		var ua = navigator.userAgent.toLowerCase();
		if (ua.indexOf('konqueror') != -1) {
			alert('You need to press CTRL + B to bookmark our site.');
		} else if (window.home || ua.indexOf('webkit') != -1) {		// Safari, iCab
			var buttonStr = (ua.indexOf('mac') != -1) ? 'Command/Cmd' : 'CTRL';
			alert('You need to press ' + buttonStr + ' + D to bookmark our site.');
		} else if (!window.print || ua.indexOf('mac') != -1) {
			alert('You need to press Command/Cmd + D to bookmark our site.');
		} else alert('In order to bookmark this site you need to do so manually through your browser.');
	}
}

// ---------------- Shopping Cart -------------------------
function SearchTextOnFocus(elem) {
	elem.setAttribute('class', 'searchbar_active_txt');
	if (elem.value == 'Search') elem.value = '';	
}

function SearchTextOnBlur(elem) {
	if (elem.value == '') {
		elem.setAttribute('class', 'searchbar_txt');
		elem.value = 'Search';
	}
}

function SearchTextOnKeyDown(e, elem, dir_nesting) {
	var key = (e.keyCode == undefined) ? e.which : e.keyCode;
	if (key == 13) {		// return key
		window.location = dir_nesting + 'pages/search.php?s=' + escape(elem.value);
	}
}

function SearchButtonOnClick(elem, dir_nesting) {
	var search_field = document.getElementById('sbar_search');
	if (search_field != null) {
		window.location = dir_nesting + 'pages/search.php?s=' + escape(search_field.value);
	}
}

// ---------------- CCF Australia -------------------------
function CCF_AustraliaBannerTick(index, dir_nesting) {
	dir_nesting = (dir_nesting == undefined || dir_nesting == '') ? 'http://home.exetel.com.au/ciyss/ccf_australia/' : dir_nesting;
	
	$images = new Array('msg1', 'msg2', 'msg3', 'child1', 'child2', 'child3', 'child4', 'child5', 'child6', 'child7', 'msg4');
	var main = document.getElementById('ccf_australia_main');
	if (main != null) {
		main.firstChild.src = dir_nesting + $images[index] + '.jpg';
		
		if (index >= 0 && index <= 9) {
			setTimeout('CCF_AustraliaBannerTick(' + (index + 1) + ')', 15000);
		} else {
			setTimeout('CCF_AustraliaBannerTick(0)', 15000);
		}
	}
}