/**
 * Custom JS Functions
 */
 
// ** global variables ** //

/**
 * Product Browse dattributeid
 */
var pbDid = 94151;

/**
 * Attribute Search dattributeid
  */
var asDid = 103667;

/**
 * Create a shortcut to jQuery, calling no conflict due to our custom $ function
 * {@link http://docs.jquery.com/Using_jQuery_with_Other_Libraries}
 */
var $j = jQuery.noConflict();

/**
 * Function used to display product selected by
 * user from our product browse list
 * @param {int} did
 * @param {string} siteurl
 * @param {object} product_data
 * @return {string} prod_div
 */
function show_product(did, siteurl, product_data) {
	if (product_data == "") {
		alert("No data available for the selected product. Please try your request again.");
		product_browse('');
		return;
	}
	
	//set our product_data (parsing our JSON string)
	var product_data = json_decode(unescape(product_data));
	
	//begin our product listing
	var prod_div = ' <div class="box rollover">';
	

	//product image	
	if (product_data.productImage != '' && product_data.productImage != 'null') {
		prod_div += '  <img class="thumbnail float-left" alt="product image" src="' + siteurl + 'content/products/images/thumb/' + product_data.productImage + '" />';
	}

	prod_div += '  <div class="box-text float-left">';
	
	//brandLogo
	if (product_data.brandLogo != 'null') {
		prod_div += '<img src="' + siteurl + 'content/products/images/logos/' + product_data.brandLogo + '" class="brand-logo" />';
	}

	//product name	
	if (product_data.realProductName != 'null') {
		prod_div += '  <h3>' + product_data.realProductName + '</h3>'; 
	}

	//subtitle
	if (product_data.subtitle != 'null') {
		prod_div += '  <h4>' + product_data.subtitle + '</h4>'; 
	}

	//description
	if (product_data.productDescription != 'null' && product_data.productDescription != 'template') {
		prod_div += '  <p>' + product_data.productDescription + '</p>';
	}
	
	//lamps
	if (product_data.lamps != 'null') {
		prod_div += '  <p>' + product_data.lamps + '</p>';
	}
	
	prod_div += '  <div class="clear"></div>';
	
	//end of content div
	prod_div += '  </div>';
	prod_div += '  <div class="clear"></div>';
	prod_div += ' </div>';

	return prod_div;	
}

//-- QUICK SEARCH FUNCTIONS --//

function show_search_results(start, end) {
	var str_html = '';
	
	for (var i = start; i < end; i++) {
		str_html += '<div class="box">';
		str_html += '<img class="thumbnail float-left" src="content/products/' + results[i].productImage + '" alt="' + results[i].productName + '" />';
		str_html += '<div class="box-text float-left">';
		str_html += '<h3>' + results[i].productName + '</h3>';
		str_html += '<h4>' + results[i].subtitle + '</h3>';
		str_html += '<p>' + results[i].productDescription + '</p>';
		str_html += '<p><a href="' + results[i].filename + '">View Product Page</a></p>';
		str_html += '</div>';
		str_html += '<div class="clear"></div>';
		str_html += '</div>';
	}
	
	$('search-contents').innerHTML = str_html;	
}

/**
 * Function used to write out our product browse category list
 * @param {int} did
 * @param {string} divid
 * @param {string} siteurl
 * @param {string} selected_did Optional
 */
function write_product_browse_dd(did, divid, siteurl, selected_did) {
	if (!selected_did) {var selected_did = '';}		
	var nextdiv = next_div('prod-browse', divid);
			 
	var categories = '';
	
	var choices = list_children(did, 'pb');
		
	for (var i = 0, c_count = choices.length; i < c_count; i++) {
		var var_object = new Object();
			var_object.category = choices[i];
			var_object.siteurl = siteurl;
			
		categories += '<div id="prod-browse-button-' + choices[i] + '" class="accordion-button" onclick="show_product_browse_list(\'' + escape(json_encode(var_object)) + '\');"><span class="light">' + PBindex[choices[i]][1] + '</span></div>\n' +
		              '<div id="prod-browse-content-' + choices[i] + '" class="accordion-content"><div class="clear"></div></div>\n';
	}

	categories += '<div id="' + nextdiv + '"></div>';
	$(divid).innerHTML = categories;
	
	if (selected_did != '') {
		//need to check if this is a subcategory or not
		//if it is, we need to open the parent category and then call search product browse with selected did
		var var_object = new Object();
		if (selected_did.indexOf('_') != -1) {
			var_object.sub_category = selected_did.substring(selected_did.indexOf('_')+1); 
			selected_did = selected_did.substring(0, selected_did.indexOf('_'));
		} 
		
		var_object.category = selected_did;
		var_object.siteurl = siteurl;
		show_product_browse_list(json_encode(var_object));
		//search_product_browse(selected_did, siteurl);
	}
}


/**
 * Function used to process our product browse
 * list item rollover functionality
 * @param {object} select_ref
 * @param {string} divid
 * @param {string} type
 * @param {string} siteurl
 * @param {string} attribute_search_history Optional
 */
function search_product_browse(category,siteurl,parent) {
	//don't process if they selected the top select list item ('all')
	if (category == pbDid) {		
		$('prod-browse-content-' + category).innerHTML = '';
	} else {
		//reset our div
		$('prod-browse-content-' + category).innerHTML = '';

		//if this is a non-product link, open a new window with our url
		if (PBindex[category][0] == "Non-Product Link") {
			//reset our category list?
			//open our window
			window.open(PBindex[category][2]);					
			return;
		} else {
			var parameters = new Object();
				parameters._method = 'get_index_product_list';
				parameters.did = category;
			
				AjaxRequest.post({
						'url': siteurl + 'includes/ajax.php',
						'parameters': parameters,
						'onLoading': toggle_element('loading'),
						'onSuccess': function(t) {write_product_browse_list(category, siteurl, t.responseText, parent);toggle_element('loading');},
						'onError': function(t) {alert('Unable to retrieve product(s) for this product type');toggle_element('loading');}
				});
		}
	}
}

/**
 * Function used to write out our product browse list
 * @param {int} did
 * @param {string} divid
 * @param {string} siteurl
 * @param {object} product_data
 * @param {int} parent
 */
function write_product_browse_list(did, siteurl, product_data, parent) {
	var filename_array = window.location.pathname.split('/');	
	var filename = filename_array[filename_array.length-2];

	var prod_list = "<ul id=\"" + did + "\">\n";
    
	if (product_data == "") {
		alert("No data available for the selected category. Please try your request again.");
		product_browse('');
		return;
	}

	//parse our json string
	product_data = json_decode(product_data);
	var products = product_data.products;
		
	for (var i = 0, product_count = products.length; i < product_count; i++) {
		if (filename == products[i].filename) {//on this product page, don't link and no mouseover event
			prod_list += '<li><a class=\"current\">' + products[i].productName + '</a></li>\n';
		} else {//not on product page, include mouseover and link
			prod_list += '<li><a class="product" href="' + siteurl + 'products/' + products[i].filename + '/?pbid=' + 
							(typeof(parent) != 'undefined' ? parent + '_' + did : did) + '">' + products[i].productName + '</a>' +
						 ' <ul>' +
						 '  <li style="position:relative;">' + show_product(products[i].did, siteurl, json_encode(products[i])) + '</li>' +
						 ' </ul>' +
						 '</li>';
		}
	}
	
	prod_list += "</ul>\n<div class=\"clear\"></div>";

	$('prod-browse-content-' + did).innerHTML = prod_list;
	
	//now we need to run our script to build our sub menus
	buildsubmenus(did);
$j('#prod-browse-content-' + did).pngFix();
	//if our menu is open, we're going to close it, else open it
	if ($j('#prod-browse-content-' + did).is(':visible')) {
		//alert('up');
		$j('#prod-browse-content-' + did).slideUp('normal');
	} else {
		$j('#prod-browse-content-' + did).slideDown('normal');
	}
	
}

/**
 * Function used to initialize our product browse functionality
 * @return string|int selected_did Optional
 */
function product_browse(selected_did) {
	//closes all accordion content divs
	$j("div.accordion-content").hide();
	
	if (!selected_did) {var selected_did = '';}	
	var siteurl = '' 

	$('content-browse').style.display = '';
	$('content').style.display = 'block';

	var ulstring = ' <ul>' +
				   '  <li class="selected"><a href="javascript: void(0);" onclick="product_browse();' + ((typeof(pageTracker) != 'undefined') ? 'pageTracker._trackPageview(\'/product_browse/\');' : '') + '"><span>Product Browse</span></a></li>' +
				   ' </ul>' +
				   ' <div class="clear"></div>';

	$('pbrowse-tabs').innerHTML = ulstring;	
	$('content-browse').innerHTML = '';
	$('bottomDiv').innerHTML = '';
	$('attSearch1').innerHTML = ''; 
	$('prod-browse').innerHTML = '';
	$('attSearch1').style.display = 'none';

	write_product_browse_dd(pbDid, 'prod-browse', siteurl, (selected_did != '' ? selected_did : ''));
	
	$('prod-browse').style.display = '';
}

/**
 * Simple accordian style menu, for use with dual-lite product browse
 * @param string param_object
 * Original code taken from {@link http://www.stemkoski.com/stupid-simple-jquery-accordion-menu/} 
 */
function show_product_browse_list(param_object) {
	//set our param object for passing variables into our search product browse function
	var object = json_decode(unescape(param_object));

	//if these are products under a sub-category we need to show the parent category div
	if (typeof(object.parent) != 'undefined') {
		$j('li.accordion-content-sub').slideUp('normal');
	} else {
		$j('div.accordion-content').slideUp('normal');
	}

	//need to determine if the next level is sub-categories or products
	//products are queried from the DB, sub-categories come from local js
	var is_subcat = false;
	var categories = "<ul>\n";
	var children = list_children(object.category, 'pb');
	for (var i = 0, c_count = children.length; i < c_count; i++) {
		if (typeof(PBindex[children[i]]) != 'undefined') {
			//there are sub-categories, need to show them
			is_subcat = true;
			var var_object = new Object();
			var_object.category = children[i];
			var_object.siteurl = object.siteurl;
			var_object.parent = object.category;
			categories += '<li id="prod-browse-button-' + children[i] + '"><a href="javascript:;" onclick="show_product_browse_list(\'' + escape(json_encode(var_object)) + '\');">' + PBindex[children[i]][1] + '</a></li>';
			categories += '<li id="prod-browse-content-' + children[i] + '" style="display:none;" class="accordion-content-sub"></li>';
		}
	}

	if (is_subcat == true) {
		$('prod-browse-content-' + object.category).innerHTML = categories;
		//if our menu is open, we're going to close it, else open it
		if ($j('#prod-browse-content-' + object.category).is(':visible')) {
			$j('#prod-browse-content-' + object.category).slideUp('normal');
		} else {
			$j('#prod-browse-content-' + object.category).slideDown('normal');
		}
		
		//if sub_category was passed in as part of the pbid, we need to go ahead and open the subcategory 
		if (typeof(object.sub_category) != 'undefined') {
			search_product_browse(object.sub_category, object.siteurl, object.category);
		}
		
	} else {
		//execute our search product browse function
		search_product_browse(object.category, object.siteurl, object.parent);
	}
}
/**
 * Function used to toggle our product browse/attribute search menu
 * @param {string} mode
 * @param {boolean} save Optional
 */
 
function hide_side_pane() { //Default function to automatically hide the side-pane
	$j('#side-pane').hide();
	$j('#content').css("width", "100%");
	$j('#hide').hide();
	$j('#show').show();
}
 
function toggle_menu(mode, save) {
	if (!save) {var save = false;}

	switch (mode) {
		case 'hide':
			if ($('side-pane')) {$('side-pane').style.display = 'none';}
			if ($('hide')) {$('hide').style.display = "none";}
			if ($('show')) {$('show').style.display = "";}	
			if ($('content')) {$('content').style.width = "100%";}
			
			//Animated Version
			/*
			$j('#side-pane').hide();
			$j('#content').animate({width: '100%'});
			$j('#hide').hide();
			$j('#show').show();*/
			break;
		case 'show':
			if ($('side-pane')) {$('side-pane').style.display = 'block';}
			if ($('hide')) {$('hide').style.display = "";}
			if ($('show')) {$('show').style.display = "none";}
			if ($('content')) {$('content').style.width = "782px";}
			
			//Animated Version
			/*
			$j('#content').animate({width: '782px'}, 400, showBrowse);
			//$('#side-pane').show('slow');
			$j('#show').hide();
			$j('#hide').show();*/
			break;			
	}
	function showBrowse() { //Fade product browse in after content animates
		$j('#side-pane').show("slow");	
	}
	//update our cookie if applicable
	if (save) {
		save_product_browse_cookie(mode);
	}
}
/**
 * Function used to build our submenus for our
 * product browse navigation menu
 * @param {string} menu_id
 * Original Code taken from {@link http://www.dynamicdrive.com/style/csslibrary/item/suckertree-menu-vertical/}
 */
function buildsubmenus(menu_id){
	var ultags = $(menu_id).getElementsByTagName('ul');
	
	for (var t = 0; t < ultags.length; t++) {
		if (ultags[t].parentNode.parentNode.id == menu_id) //if this is a first level submenu
			ultags[t].style.left=ultags[t].parentNode.offsetWidth + 'px'; //dynamically position first level submenus to be width of main menu item
		else //else if this is a sub level submenu (ul)
			ultags[t].style.left = ultags[t-1].getElementsByTagName('a')[0].offsetWidth + 'px'; //position menu to the right of menu item that activated it
			ultags[t].parentNode.onmouseover = function() {
				this.getElementsByTagName("ul")[0].style.display = 'block';
			}
		
		ultags[t].parentNode.onmouseout = function() {
			this.getElementsByTagName('ul')[0].style.display = 'none';
		}
	}
	
	for (var t = ultags.length-1; t >-1; t--) { //loop through all sub menus again, and use "display:none" to hide menus (to prevent possible page scrollbars
		ultags[t].style.visibility = 'visible';
		ultags[t].style.display = 'none';
	}
}


/**
 * Function to initialize our attribute search functionality
 * @param {string} attribute_search_history Optional
 */
function attribute_search(attribute_search_history) {
	if (!attribute_search_history) {var attribute_search_history = '';}
	var siteurl = ''; 
	
	$('content-browse').style.display = 'none';
	$('content').style.display = 'block';
				   
	var ulstring = ' <ul>' +
				   '  <li class="selected"><a href="javascript: void(0);" onclick="attribute_search();' + ((typeof(pageTracker) != 'undefined') ? 'pageTracker._trackPageview(\'/attribute_search/\');' : '') + '"><span>Attribute Search</span></a></li>' +
				   '  <li><a href="javascript: void(0);" onclick="product_browse();' + ((typeof(pageTracker) != 'undefined') ? 'pageTracker._trackPageview(\'/product_browse/\');' : '') + '"><span>Product Browse</span></a></li>' +
				   ' </ul>';

	$('pbrowse-tabs').innerHTML = ulstring;

	$('content-browse').innerHTML = '';
	$('attSearch1').innerHTML = ''; //clear existing att search
	
	$('content-browse').style.display = 'none';
	$('attSearch1').style.display = 'block';
	$("prod-browse").innerHTML = ''; //clear any existing product list
	$("prod-browse").style.display = 'none';

	if (attribute_search_history != '') {
		write_attribute_search_dd(asDid, 'attSearch1', siteurl, attribute_search_history, attribute_search_history);
	} else {
		write_attribute_search_dd(asDid, 'attSearch1', siteurl);
	}
}

//-- FILE SEARCH FUNCTIONS --//

/**
 * Function used to show our given div, also hides
 * other divs and sets active nav link (used by our
 * download pages e.g. instruction sheets, photomotry, etc.)
 * @param {string} div
 * @param {boolean} show_children Optional
 * @param {string} parent Optional
 */
function show_div(div, show_children, parent) {
	if (!show_children) {var show_children = false;}
	if (!parent) {var parent = '';}
		
	var div_array = $('products-container').getElementsByTagName('div');

	//hide all our other divs
	for (i = 0, div_length = div_array.length; i < div_length; i++) {
		if (div_array[i].id != '') {
			div_array[i].style.display = "none";
		}
	}

	//clear out our file-search-results (where applicable)
	if ($('file-search-results')) {
		$('keywords').value = '';
		$('file-search-results').innerHTML = '';
		$('file-search-results').style.display = "none";
	}
	
	if (parent != '') {
		//show our div
		pdiv_array = $(parent).getElementsByTagName('div');
		for (i = 0, pdiv_length = pdiv_array.length; i < pdiv_length; i++) {
			if (pdiv_array[i].id == div) {
				$(div).style.display = "block";
				$(parent).style.display = "block";
			}
		}
	} else if ($(div)) {
		//show our div
		$(div).style.display = "block";
		
		if (show_children) {
			pdiv_array = $(div).getElementsByTagName('div');
			for (var i = 0, pdiv_length = pdiv_array.length; i < pdiv_length; i++) {
				pdiv_array[i].style.display = "block";
			}
		}
	}

	return;
}

/**
 * Function used to search our files, based on the entered keyword(s)
 * @param {string} filetype
 */
function search_files(filetype) {
	var nav_array = $('products-nav').getElementsByTagName('a'), div_array = $('products-container').getElementsByTagName('div'), keywords = $('keywords').value;

	//reset our other nav links
	for (var i = 0, nav_length = nav_array.length; i < nav_length; i++) {
		nav_array[i].className = '';
	}

	//hide all our other divs
	for (i = 0, div_length = div_array.length; i < div_length; i++) {
		if (div_array[i].id != '') {
			div_array[i].style.display = "none";
		}
	}
	
	if (keywords != '' && filetype != '') {
		var failure = function(t) {alert('Unable to process your search.');toggle_element('loading');}
		var success = function(t) {$('file-search-results').innerHTML = (t.responseText == '') ? 'No results found for ' + keywords + '.' : t.responseText;toggle_element('loading');}

		var parameters = new Object();
		parameters._method = 'search_files';
		parameters.filetype = filetype;
		parameters.keywords = keywords;
		
		AjaxRequest.post({
				'url': 'includes/ajax.php',
				'parameters': parameters,
				'onLoading': toggle_element('loading'),
				'onSuccess': success,
				'onError': failure
		});
	}
}

//-- SHOPPING CART FUNCTIONS --//

/**
 * Function used to add/update item in shopping cart. Checks to ensure
 * item isn't already in the cart, if so update routine runs
 * @param {integer} item_id
 */
function add_to_cart(item_id) {
	var total_items = $('total_items').value;
	var new_item_id = (parseInt(total_items) + 1);
	var item_name = ($('product_name_' + item_id) ? $('product_name_' + item_id).innerHTML : '');
	var item_number = ($('product_number_' + item_id) ? $('product_number_' + item_id).innerHTML : '');
	var item_description = $('product_description_' + item_id).innerHTML;
	var item_quantity = $('product_quantity_' + item_id).value;
	var item_price = ($('product_price_' + item_id).value / 100);
	var item_size = ($('product_size_' + item_id) ? $('product_size_' + item_id).value : '');

	if (item_quantity == '') {
		alert('Please enter a quantity for item: ' + item_name);
		return;
	}

	if ($('item_id_' + item_id + (item_size != '' ? '_' + item_size : ''))) {
		//item exists in cart already, update
		update_cart(item_id, $('item_id_' + item_id + (item_size != '' ? '_' + item_size : '')).value, item_quantity);
	} else {
		//item doesn't exist, add
		var row = build_shopping_cart_row(new_item_id, item_id, item_quantity, item_price, item_name, item_number, item_description, item_size);
		var target_element = $(total_items == 0 ? 'cart_header' : 'cart_item_' + total_items); //determine where to add
		var next = target_element.nextSibling;
		(next) ? target_element.parentNode.insertBefore(row, next) : target_element.parentNode.appendChild(row);
	
		//update total items in cart
		$('total_items').value = (parseInt(total_items)+parseInt(1));
		
		//update cart subtotal
		update_cart_subtotal();
	}

	//clear out our quantity
	$('product_quantity_' + item_id).value = '';

	//reset our size if applicable
	if (item_size != '') {
			$('product_size_' + item_id).selectedIndex = 0;
	}

	//scroll to our shopping cart details
	$j.scrollTo('#cart-details', 700);
}

/**
 * Function to build our elements for a new shopping cart row
 * @param {integer} new_item_id
 * @param {integer} item_id
 * @param {integer} item_quantity
 * @param {string} item_price
 * @param {string} item_name Optional
 * @param {string} item_number Optional
 * @param {string} item_description
 * @param {string} item_size
 * @return {object} row
 */
function build_shopping_cart_row(new_item_id, item_id, item_quantity, item_price, item_name, item_number, item_description, item_size) {
	//Robin: "Holy Shit Batman! All this just to build our table row client side!!!"
	var row = '';
	var cell = '';
	var attribute = '';
	var link_node = '';
	var input = '';
	var image = '';
	row = document.createElement('tr');
	row.setAttribute('id', 'cart_item_' + new_item_id);
	row.setAttribute('class', 'cart-content');

	cell = document.createElement('td');
	cell.setAttribute('height', '30');

	link_node = document.createElement('a');
	link_node.setAttribute('href', 'javascript:void(0);');				
	link_node.setAttribute('onclick', "$j.scrollTo('#merch_" + item_id + "', 700);");
	link_node.appendChild(document.createTextNode((item_name != '') ? item_name : '') );
	cell.appendChild(link_node);
	row.appendChild(cell);

	cell = document.createElement('td');
	cell.setAttribute('align', 'left');
	cell.setAttribute('height', '30');
	cell.appendChild(document.createTextNode(format_currency(item_price)));
	row.appendChild(cell);

	cell = document.createElement('td');
	cell.setAttribute('height', '30');

	input = document.createElement('input');
	input.setAttribute('type', 'hidden');
	input.setAttribute('id', 'item_id_' + item_id + (item_size != '' ? '_' + item_size : ''));
	input.setAttribute('name', 'item_id_' + item_id + (item_size != '' ? '_' + item_size : ''));
	input.setAttribute('value', new_item_id);
	cell.appendChild(input);

	input = document.createElement('input');
	input.setAttribute('type', 'hidden');
	input.setAttribute('id', 'item_name_' + new_item_id);
	input.setAttribute('name', 'item_name_' + new_item_id);
	input.setAttribute('value', (item_number != '' ? item_number : (item_name != '' ? item_name : item_description)));
	cell.appendChild(input);

	input = document.createElement('input');
	input.setAttribute('type', 'hidden');
	input.setAttribute('id', 'item_description_' + new_item_id);
	input.setAttribute('name', 'item_description_' + new_item_id);
	input.setAttribute('value', ((item_name != '') ? item_name : item_description) + ((item_size != '') ? ' (' + item_size + ')' : ''));
	cell.appendChild(input);

	input = document.createElement('input');
	input.setAttribute('type', 'hidden');
	input.setAttribute('id', 'item_quantity_' + new_item_id);
	input.setAttribute('name', 'item_quantity_' + new_item_id);
	input.setAttribute('value', item_quantity);
	cell.appendChild(input);

	input = document.createElement('input');
	input.setAttribute('type', 'hidden');
	input.setAttribute('id', 'item_price_' + new_item_id);
	input.setAttribute('name', 'item_price_' + new_item_id);
	input.setAttribute('value', item_price);
	cell.appendChild(input);

	input = document.createElement('input');
	input.setAttribute('type', 'text');
	input.setAttribute('id', 'cart_item_quantity_' + new_item_id);
	input.setAttribute('name', 'cart_item_quantity_' + new_item_id);
	input.setAttribute('value', item_quantity);
	input.setAttribute('size', '1');
	input.setAttribute('onchange', 'if(validate_quantity(this, true)){update_cart(' + item_id + ', ' + new_item_id + ', this.value, true);}');
	cell.appendChild(input);
	row.appendChild(cell);
	
	cell = document.createElement('td');
	cell.setAttribute('align', 'left');
	cell.setAttribute('height', '30');
	cell.setAttribute('id', 'cart_item_price_' + new_item_id);		
	cell.appendChild(document.createTextNode(format_currency(parseFloat(item_price) * parseInt(item_quantity))));
		
	row.appendChild(cell);

	cell = document.createElement('td');
	cell.setAttribute('height', '30');
	cell.setAttribute('align', 'center');

	image = document.createElement('img');
	image.setAttribute('src', 'images/merchandise-delete-icon.jpg');
	image.setAttribute('alt', 'Delete Item');
	
	link_node = document.createElement('a');
	link_node.setAttribute('href', 'javascript:remove_from_cart(' + new_item_id + ');');
	link_node.setAttribute('class', 'remove');
	link_node.appendChild(image);
	cell.appendChild(link_node);
	row.appendChild(cell);
	
	return row;
}

/**
 * jQuery.ScrollTo - Easy element scrolling using jQuery.
 * Copyright (c) 2007-2009 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
 * Dual licensed under MIT and GPL.
 * Date: 3/9/2009
 * @author Ariel Flesler
 * @version 1.4.1
 *
 * http://flesler.blogspot.com/2007/10/jqueryscrollto.html
 */
;(function($){var m=$.scrollTo=function(b,h,f){$(window).scrollTo(b,h,f)};m.defaults={axis:'xy',duration:parseFloat($.fn.jquery)>=1.3?0:1};m.window=function(b){return $(window).scrollable()};$.fn.scrollable=function(){return this.map(function(){var b=this,h=!b.nodeName||$.inArray(b.nodeName.toLowerCase(),['iframe','#document','html','body'])!=-1;if(!h)return b;var f=(b.contentWindow||b).document||b.ownerDocument||b;return $.browser.safari||f.compatMode=='BackCompat'?f.body:f.documentElement})};$.fn.scrollTo=function(l,j,a){if(typeof j=='object'){a=j;j=0}if(typeof a=='function')a={onAfter:a};if(l=='max')l=9e9;a=$.extend({},m.defaults,a);j=j||a.speed||a.duration;a.queue=a.queue&&a.axis.length>1;if(a.queue)j/=2;a.offset=n(a.offset);a.over=n(a.over);return this.scrollable().each(function(){var k=this,o=$(k),d=l,p,g={},q=o.is('html,body');switch(typeof d){case'number':case'string':if(/^([+-]=)?\d+(\.\d+)?(px)?$/.test(d)){d=n(d);break}d=$(d,this);case'object':if(d.is||d.style)p=(d=$(d)).offset()}$.each(a.axis.split(''),function(b,h){var f=h=='x'?'Left':'Top',i=f.toLowerCase(),c='scroll'+f,r=k[c],s=h=='x'?'Width':'Height';if(p){g[c]=p[i]+(q?0:r-o.offset()[i]);if(a.margin){g[c]-=parseInt(d.css('margin'+f))||0;g[c]-=parseInt(d.css('border'+f+'Width'))||0}g[c]+=a.offset[i]||0;if(a.over[i])g[c]+=d[s.toLowerCase()]()*a.over[i]}else g[c]=d[i];if(/^\d+$/.test(g[c]))g[c]=g[c]<=0?0:Math.min(g[c],u(s));if(!b&&a.queue){if(r!=g[c])t(a.onAfterFirst);delete g[c]}});t(a.onAfter);function t(b){o.animate(g,j,a.easing,b&&function(){b.call(this,l,a)})};function u(b){var h='scroll'+b;if(!q)return k[h];var f='client'+b,i=k.ownerDocument.documentElement,c=k.ownerDocument.body;return Math.max(i[h],c[h])-Math.min(i[f],c[f])}}).end()};function n(b){return typeof b=='object'?b:{top:b,left:b}}})(jQuery);

/**
 * JavaScript Dropdown Menu with Multi Levels
 * Original code taken from {@link http://www.leigeber.com/2008/11/drop-down-menu/}
 */
var menu=function(){
	var t=15,z=50,s=6,a;
	function dd(n){this.n=n; this.h=[]; this.c=[]}
	dd.prototype.init=function(p,c){
		a=c; var w=document.getElementById(p), s=w.getElementsByTagName('ul'), l=s.length, i=0;
		for(i;i<l;i++){
			var h=s[i].parentNode; this.h[i]=h; this.c[i]=s[i];
			h.onmouseover=new Function(this.n+'.st('+i+',true)');
			h.onmouseout=new Function(this.n+'.st('+i+')');
		}
	}
	dd.prototype.st=function(x,f){
		var c=this.c[x], h=this.h[x], p=h.getElementsByTagName('a')[0];
		clearInterval(c.t); c.style.overflow='hidden';
		if(f){
			p.className+=' '+a;
			if(!c.mh){c.style.display='block'; c.style.height=''; c.mh=c.offsetHeight; c.style.height=0}
			if(c.mh==c.offsetHeight){c.style.overflow='visible'}
			else{c.style.zIndex=z; z++; c.t=setInterval(function(){sl(c,1)},t)}
		}else{p.className=p.className.replace(a,''); c.t=setInterval(function(){sl(c,-1)},t)}
	}
	function sl(c,f){
		var h=c.offsetHeight;
		if((h<=0&&f!=1)||(h>=c.mh&&f==1)){
			if(f==1){c.style.filter=''; c.style.opacity=1; c.style.overflow='visible'}
			clearInterval(c.t); return
		}
		var d=(f==1)?Math.ceil((c.mh-h)/s):Math.ceil(h/s), o=h/c.mh;
		c.style.opacity=o; c.style.filter='alpha(opacity='+(o*100)+')';
		c.style.height=h+(d*f)+'px'
	}
	return{dd:dd}
}();

function arc_html() {
	var arcNav 		= (($j('ul.arc-nav') != null) ? $j('ul.arc-nav').html() : null);
	var arcContent 	= (($j('div.arc-content') != null) ? $j('div.arc-content').html() : null);	
	var myTable 	= ((arcContent != null) ? $j('div.section').html('<table cellspacing="0" cellpadding="0" width="100%"><tr><td valign="top" style="width:200px;"><ul class="arc-nav" style="width:auto; float:none;">' + arcNav + '</ul></td><td width="3">&nbsp;</td><td valign="top" class="box"><div class="arc-content" style="width:auto;float:none;">' + arcContent + '</div></td></tr></table>') : null);	
}

/**
 * Function used to display flash ZC popup window 
 * Called from prouct page photometry file links. 
 * @param {string} siteurl
 * @param {string} file (list of all IES files for the product)
 * @param {string} image (product's thumbnail image)
 * @param {string} current file (specific IES file from the clicked link)
 */
function open_flash_zc(siteurl, file, image, current) {
	var A = "InputOutputMode=XML"; 
	var B = "InputURL=" + siteurl + "flashzc/xml_input.php" + escape("?filename=" + file + "&image=" + image + "&current=" + current); 
	var C = "OutputURL=SaveXML.php"; 
	var D = "PhotometricURL=PhotometricDB.php"; 
	var E = "InstanceKey=LightingAnalysts"; 
	var popurl = siteurl + "flashzc/FlashZC.php?" + A + "&"  + C + "&"  + D + "&"  + E + "&" + B; 
	winpops=window.open(popurl,"FlashZC","width=650,height=500,status=no,resizable=yes,menubar=no,toobar=no,location=no,directories=no"); 
	winpops.focus(); 
}