/* Nikken Add to Cart Options */

function sendRequest(prodID, element){
	var net6 = false;
	var browser_agent = navigator.userAgent;
	if (browser_agent.contains('Netscape6')) net6 = true;
	$('product_options_container').setStyle('display', 'block'); 
	$('options_content_container').set('html', '<img src="/files/img/tomboy-loading.gif" alt="Please Wait"/>');
	if(net6) $('options_content_container').set('html', 'Please upgrade');
	var req = new Request.HTML({  
		method: 		'get',  
		url: 			$(element).get('href'),  
		data: 		{ 'frombrowse' : '1', 'prodID' : prodID },   
		evalResponse: 	true,
		onRequest: function() {
			
		},  
		onSuccess: function(html){
			$('options_content_container').set('html', '');
			$('options_content_container').adopt(html);
		},
		//update: $('options_content_container'),  
		onComplete: function(response) { 
			$('options_top').getElement('a').addClass('show');
			$('modalwindow').addEvent('click', function(){
				hideOptionsWindow();
			});
		}
	}).send();
};

function setModal(){
	var arrayPageSize = getPageSize();
	
	$('modalwindow').setStyle('display', '');
	$('modalwindow').setStyle('z-index', '99');
	$('modalwindow').setStyle('position', 'absolute');
	$('modalwindow').setStyle('top', '0');
	$('modalwindow').setStyle('left', '0');
	$('modalwindow').setStyle('width', arrayPageSize[0] - 0  + 'px');
	$('modalwindow').setStyle('height', arrayPageSize[1] - 0 + 'px');
	$('modalwindow').setStyle('background-color', '#000');
	$('modalwindow').setStyle('opacity', '0.65');
	$('modalwindow').setStyle('filter', 'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=65)');
}

function resizeModal(){
	
}

function removeModal(){
	$('modalwindow').setStyle('display', '');
	$('modalwindow').setStyle('display', 'none');
}

function setOptionsWindowPos(){
	setModal();
	
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();
	var optionsWindowLeft = ((arrayPageSize[0] - 700) / 2) + 'px';
	var optionsWindowTop = arrayPageScroll[1] + (arrayPageSize[3] / 15) + 'px';

	var div = $('product_options_container'); 
	div.setStyle('position','absolute'); 
	div.setStyle('left', optionsWindowLeft);  
	div.setStyle('top', optionsWindowTop);
	
	
}

 
function hideOptionsWindow(){
	removeModal();
	
	$('product_options_container').setStyle('display', 'none');
	$('product_options_container').setStyle('left', '');
	$('product_options_container').setStyle('top', '');
}

function invokeOptions(prodID, element){
	setOptionsWindowPos();
	sendRequest(prodID, element);
	///
}

// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

// -----------------------------------------------------------------------------------

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function getURLVar(urlVarName) {
//divide the URL in half at the '?'
	var urlHalves = String(document.location).split('?');
	var urlVarValue = 0;
	if(urlHalves[1]){
		//load all the name/value pairs into an array
		var urlVars = urlHalves[1].split('&');
		//loop over the list, and find the specified url variable
		for(i=0; i<=(urlVars.length); i++){
			if(urlVars[i]){
				//load the name/value pair into an array
				var urlVarPair = urlVars[i].split('=');
				if (urlVarPair[0] && urlVarPair[0] == urlVarName) {
					//I found a variable that matches, load it's value into the return variable
					urlVarValue = urlVarPair[1];
				}
			}
		}
	}
	if(isNaN(urlVarValue)) urlVarValue = 0;
	return urlVarValue;   
}
