/****************** COMMON SCRIPTS ********************/

var domEnabled = (document.getElementById);

// popup function
function popUp(URL,winName,w,h,scroll,resize) {
	createPopUp(URL,winName,w,h,scroll,resize);
}
function createPopUp(URL,winName,w,h,scroll,resize) {
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable='+resize;
	win = window.open(URL,winName,settings);
	return win;
}

// product image popuo
function loadProductImage(url) {
	popUp(url, 'sony_product_photo', 470, 470, 'no', 'no');
}

function loadPic(url,winName,w,h) {
	var pic = createPopUp('',winName,w,h,'no','no');
	var oTag = "<img src=\"" + url + "\" width=\"" + w + "\" height=\"" + h + "\" alt=\"\" />";+
	pic.document.write('<html><head><title>Sony</title></head><body style="margin:0;padding:0">' + oTag + '</body></html>');
}

/****************** AD PRO POPUP ********************/
function getAd(src,res) {
	var w,h;
	if(res == 'high') {
		w = 315;
		h = 240;
	} else {
		w = 160;
		h = 120;
	}
	h+=16;
	var ad = createPopUp('', 'ad', w, h, 'no', 'no');
	var oTag = '<OBJECT CLASSID="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" WIDTH="'+w+'" HEIGHT="'+h+'"'+
		'CODEBASE="http://www.apple.com/qtactivex/qtplugin.cab">' +
		'<PARAM name="SRC" VALUE="'+src+'">' +
		'<PARAM name="AUTOPLAY" VALUE="true">' +
		'<PARAM name="SCALE" VALUE="false">' +
		'<PARAM name="CONTROLLER" VALUE="true">' +
		'<EMBED SRC="'+src+'" WIDTH="'+w+'" HEIGHT="'+h+'" AUTOPLAY="true" CONTROLLER="true" SCALE="false" PLUGINSPAGE="http://www.apple.com/quicktime/download/"></EMBED>' +
		'</OBJECT>';
	ad.document.write('<html><head><title>Sony</title></head><body style="margin:0;padding:0">' + oTag + '</body></html>');
}

/****************** SEARCH FORM VALIDATOR ********************/

function formSearch_Validate(formId, msg) {
	if(!domEnabled) return;
	var form = document.getElementById(formId);
	if(form != null) {
		var controls = form.getElementsByTagName('input');
		if(controls != null) {
			for(var i=0; i<controls.length; i++) {
				if(controls[i].getAttribute("type") == "text" && controls[i].value.length == 0) {
					if(msg.length > 0) alert(msg);
					return false;
				}
			}
		}
	}
	return true;
}

/****************** ADVISOR SCRIPTS ********************/

var oldCheckedLabel;
var EOK = (document.implementation);
function InitRadioButtons() {
	if (!EOK) return;
	var labels = document.getElementsByTagName('label');
	for(var i=0; i<labels.length; i++) {
		if(labels[i].htmlFor) {
			control = document.getElementById(labels[i].htmlFor);
			if(control && control.className.indexOf('RadioButton') > -1) {
				if(control.checked) {
					labels[i].className = "RadioLabelOn";
					oldCheckedLabel = labels[i];
				} else {
					labels[i].className = "RadioLabel";
				}
				labels[i].onclick = new Function("ToggleRadio(this)");
				control.className = "hidden";
			}
		}
	}
}

// click radio button
function ToggleRadio(obj) {
	if (!EOK) return;
	var radioBtn = document.getElementById(obj.htmlFor);
	if(radioBtn) {
		radioBtn.checked = true;
		obj.className = "RadioLabelOn";
		if(oldCheckedLabel && oldCheckedLabel != obj) oldCheckedLabel.className = "RadioLabel";
		oldCheckedLabel = obj;
	}
}

// advisor form validation
function validateAdvisor(formObj) {
	if(!domEnabled) return true;
	var errMsg = document.getElementById('advisorError');
	var inputs = document.getElementById('advisorOptions').getElementsByTagName('input');
	for(var i=0; i<inputs.length; i++) {
		if(inputs[i].getAttribute('type') == 'radio' && inputs[i].checked) {
			errMsg.className = 'hidden';
			var newAns = document.getElementById('new_ans');
			newAns.value = inputs[i].value;
			return true;
		}
	}
	errMsg.className = '';
	return false;
}

function hideAdvisorError() {
	if(!domEnabled) return true;
	var errMsg = document.getElementById('advisorError')
	errMsg.className = 'hidden';	
}

/****************** GENERIC SITEMAP SCRIPT ********************/

function setupSitemap(mapId) {
	if(!domEnabled) return;
	var siteMap = document.getElementById(mapId);
	var siteNodes = siteMap.getElementsByTagName('A');
	for(var i=0; i<siteNodes.length; i++) {
		var link = siteNodes[i].getAttribute("href");
		if(link && link.substr( link.length-1, 1 ) == "#" ) {
			siteNodes[i].removeAttribute("href");
			siteNodes[i].className = "nolink";
		}
	}
}

/****************** GENERIC COMPARISON CATEGORIES SCRIPT ********************/

function setupComparisonCategories(listId) {
	if(!domEnabled) return;
	tree_build( document.getElementById(listId) );
}

function tree_build(oNode) {
	if(oNode.nodeName != "UL") return;
	var i=0;
	while(i < oNode.childNodes.length) {
		var currNode = oNode.childNodes[i];
		if(currNode.nodeName == "LI") {
			var sublists = currNode.getElementsByTagName('UL');
			if(sublists.length > 0) {
				var spanObj = currNode.getElementsByTagName('SPAN')[0];
				if(spanObj) {
					currNode.sublist = sublists[0];
					tree_setupLinkNode(currNode);
				}
				tree_build(sublists[0]);
				sublists[0].style.display = "none";
				currNode.className = "collapsed";
			}
		}
		i++;
	}
}

function tree_setupLinkNode(oNode) {
	var spanObj = oNode.getElementsByTagName('SPAN')[0];
	spanObj.onmouseover = function() { spanObj.className="hover" };
	spanObj.onmouseout = function() { spanObj.className = ""; };
	spanObj.onclick = function() { tree_toggleNode(oNode); };
}

function tree_toggleNode(oNode) {
	var spanObj = oNode.childNodes[0];
	if(oNode.sublist.style.display!="block") {
		oNode.sublist.style.display = "block";
		oNode.className = "expanded";
	} else {
		oNode.sublist.style.display = "none";
		oNode.className = "collapsed";
	}
	
	
}

/****************** FORM COMPARISON SCRIPT ******************/
function comparison_compareProducts(form, minProds, maxProds, tooFewProductsMsg, tooMuchProductsMsg) {
	if(!domEnabled) return;
	
	var inputFields = form.getElementsByTagName('INPUT');
	
	var prodCount = 0;
	var selectedProds = new Array(maxProds);
	
	for(var i=0; i<inputFields.length; i++) {
		if(inputFields[i].getAttribute('TYPE') == "checkbox" && inputFields[i].checked) {
			selectedProds[prodCount] = inputFields[i].value;
			prodCount++;
		}
	}
	
	if(prodCount > maxProds) alert(tooMuchProductsMsg);
	if(prodCount < minProds) alert(tooFewProductsMsg);
	if(prodCount > maxProds || prodCount < minProds) return false;
	
	for(var i=0; i<maxProds; i++) {
		var prodInput = document.getElementById('prdId'+i);
		
		if(prodCount<=i) {
			prodInput.value = "";
			prodInput.disabled = true;
		} else {
			prodInput.value = selectedProds[i];
			prodInput.disabled = false;
		}
	}
	
	return true;
}


/****************** GENERIC MENU SCRIPT ********************/

/* config */
var debugEnabled = false;

/* debug function */
function startDebug() {
	debugEnabled = true;
	w = 200
	h = 600
	// LeftPosition = ((screen.width) ? (screen.width-w)/2 : 0) + 285;
	// TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	LeftPosition = 0
	TopPosition = 0
	settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars=no,resizable=no'
	winDebug = window.open('','winDebug',settings)
	winDebug.document.writeln('<title>_debug_</title><body style="margin: 3px;font-family:Tahoma; font-size: 7pt;">')
}
function debug(msg) { if(debugEnabled) winDebug.document.writeln(msg+'<br>'); }
if(debugEnabled) startDebug();
/* end debug */

// Inits a menu bar
function setupMenubar(barId) {
	if(!domEnabled) return;
	debug("setup::" + barId);
	var menuBar = document.getElementById(barId);
	menuBar.details = new Object();
	menuBar.details.width = parseInt(menuBar.offsetWidth);
	menuBar.details.itemsWidth = 0;
	menuBar.details.itemCount = 0;
	mb_setupMenu(menuBar, menuBar);
	menuBar.style.visibility = 'visible';
}

var mb_temp_id=1;
var menuBarItemXgraze;
// Sets up a menu (and its menu items)
function mb_setupMenu(menu, bar) {
	// only <ul> (menus)
	if(menu.nodeName == "UL") {
		// add functions and props.
		menu.hide = mb_hideMenu;
		menu.show = mb_showMenu;
		menu.visible = true;
		if(menu != bar) {
			// !bar
			menu.id = bar.id + "-" + (++mb_temp_id);
			menu.hide();
		}

		// count menu items
		var itemCount = 0;
		for(var i=0; i<menu.childNodes.length; i++) {
			if(menu.childNodes[i].nodeName == "LI") {
				var menuItem = menu.childNodes[i];
				if(menu == bar) {
					bar.details.itemsWidth += menuItem.getElementsByTagName('SPAN')[0].offsetWidth;
					bar.details.itemsCount++;
				}
				itemCount++;
			}
		}
		
		if(menu == bar) {
			bar.details.itemCount = itemCount;
			if(bar.details.width == 0) { 
				bar.style.border = "1px solid red";
				bar.details.width = bar.offsetWidth;
				bar.style.border = "";
				//bar.details.width = bar.details.itemsWidth + (bar.details.itemCount*menuBarItemXgraze);
			}
			menuBarItemXgraze = Math.floor( ((bar.details.width - bar.details.itemsWidth) / bar.details.itemCount) );
		}
		
		/*var barSize = bar.offsetWidth;*/
		
		var menuItem;
		var x = 0;
		var lastX = 0;
		for(var i=0; i<menu.childNodes.length; i++) {
			// loop through menu items (<il>)
			if(menu.childNodes[i].nodeName == "LI") {
				menuItem = menu.childNodes[i];
				menuItemLink = menu.childNodes[i].getElementsByTagName('A')[0];
				
				var spanElem = menuItem.getElementsByTagName('SPAN')[0];

				if(menu == bar) {
					if(x==0) {
						menuItem.className = "firstItem";
					} else if(x==(itemCount-1)) {
						menuItem.className = "lastItem";
					}
					// bar, set X width & position
					menuItem.style.left = lastX;
					//debug("menuItem.offsetWidth:" + menuItem.getElementsByTagName('SPAN')[0].offsetWidth);
					// menuItem.style.width = spanElem.offsetWidth + menuBarItemXgraze;
					menuItem.style.width = spanElem.offsetWidth + 14;
					lastX += menuItem.offsetWidth;
				} else {
					// !bar, set Y position
					menuItem.style.top = menuItem.offsetHeight * x;
				}
				spanElem.style.display = 'block';

				// item props.
				menuItem.bar = bar;
				menuItemLink.bar = bar;
				menuItem.menu = menu;
				menuItemLink.menu = menu;
				menuItem.subMenu = null;
				menuItemLink.subMenu = null;
				// item events
				menuItemLink.onmouseover = mb_activateMenu;
				menuItemLink.onmouseout = mb_deActivateMenu;
				// check for a submenu
				if(menu.childNodes[i].getElementsByTagName('UL').length > 0) {
					// submenu
					var submenu = menu.childNodes[i].getElementsByTagName('UL')[0];
					if(submenu.getElementsByTagName('LI').length > 0) {
						submenu.direction = 1;
						var submenuRight;
						if(menu != bar) {
							submenuRight = menu.menuItem.offsetWidth + menu.menuItem.offsetLeft + menuItem.offsetWidth;
						} else {
							submenuRight = (submenu.getElementsByTagName('LI')[0].offsetWidth + menuItem.offsetLeft);
						}
						if(submenuRight > bar.details.width) submenu.direction = -1;

						if(menu == bar) {
							// bar, setup submenu position;
							submenu.style.top = menuItem.offsetHeight;
							// submenu beyond bar's width, correct submenu X pos.
							if(submenu.direction == -1) submenu.style.left = (bar.details.width - submenuRight);
						} else {
							var _parent = menuItem;
							// !bar, setup submenu x/y position
							menuItemLink.className = 'withMenu';
							//debug( submenu.direction + ";" + menu.direction);
							if(submenu.direction == -1 || menu.direction == -1) {
								submenu.style.left = -menuItem.offsetWidth;
							} else {
								submenu.style.left = menuItem.offsetWidth;
							}
							submenu.style.top = submenu.offsetTop - menuItem.offsetHeight;
						}

						// link submenu to parent menuitem and parentMenu (if appliable)
						menuItem.subMenu = submenu;
						menuItemLink.subMenu = submenu;
						submenu.menuItem = menuItem;
						if(menu != bar) submenu.parentMenu = menu;
						// setup submenu
						mb_setupMenu(submenu, bar);
					}
				}
				x++;
			}
		}

		// find out menu position		
		var menuX = 0;
		var menuY = 0;
		_parent = menu;
		while(_parent) {
			menuX += _parent.offsetLeft;
			menuY += _parent.offsetTop;
			_parent = _parent.offsetParent;
		}
		 //debug("menu '" + menu.id + "' bounds: X:"+menuX+";Y:"+menuY);
		menu.x = menuX;
		menu.y = menuY;
		// find out menu size
		menu.width = menu.getElementsByTagName('LI')[0].offsetWidth;
		menu.height = menuItem.offsetTop + menuItem.offsetHeight;
	}
}

function mb_activateMenu() {

	if(this.bar.visibleMenu) {
		var _submenu = this.bar.visibleMenu;
		while(true) {
			_submenu.visible = false;
			if(!_submenu.parentMenu) break;
			_submenu = _submenu.parentMenu;
		}
	}
	
	var _submenu = this.menu;
	if(this.subMenu) {
		_submenu = this.subMenu;
		_submenu.selectedItem = this.subMenu.menuItem;
	}
	while(true) {
		_submenu.visible = true;
		_submenu.show();
		if(!_submenu.parentMenu) break;
		_submenu = _submenu.parentMenu;
	}

	if(this.bar.visibleMenu ) {
		var _submenu = this.bar.visibleMenu;
		while(true) {
			if(!_submenu.visible) {
				_submenu.hide();
				_submenu.selectedItem = null;
			}
			if(!_submenu.parentMenu) break;
			_submenu = _submenu.parentMenu;
		}
	}
	
	if(this.subMenu) {
		this.bar.visibleMenu = this.subMenu;
		this.subMenu.onmouseout = mb_checkMousePos;
	}
}

function mb_deActivateMenu(evt) {

	evt = (evt) ? evt : event;
	// mouse X relative to page
	if(evt.pageX) {
		mouseY = evt.pageY;
	} else {
		mouseY = evt.y;
	}

	var y = 1;
	_parent = this;
	while(_parent) {
		y += _parent.offsetTop;
		_parent = _parent.offsetParent;
	}
	if(mouseY <= y && this.bar.visibleMenu) this.bar.visibleMenu.hide();
	
}

function mb_checkMousePos(evt) {
	// event obj
	evt = (evt) ? evt : event;
	// menu LeftX, TopY relative to page
	var menuX = this.x;
	var menuY = this.y;
	// mouse X relative to page
	if(evt.pageX) {
		mouseX = evt.pageX;
		mouseY = evt.pageY;
	} else {
		mouseX = evt.x;
		mouseY = evt.y;
	}
	mouseX--;
	// menu RightX
	var menuMaxX = menuX + this.width;
	var menuMaxY = menuY + this.height;
		
	// hide if cursor outside menu box
	if(mouseX >= menuMaxX || mouseX <= menuX || mouseY >= menuMaxY) {
		var _submenu = this;
		if(this.menuItem.bar.visibileMenu) _submenu = this.menuItem.bar.visibileMenu;

		var submenus = this.getElementsByTagName('UL');
		for(var i=0; i<submenus.length; i++) {
			if(submenus[i].visible) {
				submenus[i].visible = false;
				submenus[i].hide();
			}
		}
		while(true) {
			// debug("!menuHide!");
			_submenu.onmouseout = null;
			_submenu.visible = false;
			_submenu.hide();
			if(!_submenu.parentMenu) break;
			_submenu = _submenu.parentMenu;
		}
		this.menuItem.bar.visibileMenu = null;
	}
}



// show menu
function mb_showMenu() {
	/* */
	this.style.visibility = 'visible';
	/* */
}

// nide menu
function mb_hideMenu() {
	/* */
	this.style.visibility = 'hidden';
	/* */
}