
var currentInfo = "productDetail";

function enlargedView () {

	url = '/productEnlargedView.php?url=' + eval('enlarge_'+ colorOn);

	lgView = window.open(url,"enlargedView","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=770,height=550,left=170,top=70");
	lgView.focus();
}

function showEmailFriend() {

	url = '/include/emailFriend.php?id='+ productId;

	innerPage(url +'&view=modal','600','400');
	
//	efView = window.open(url,"emailFriendView","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,width=650,height=550,left=170,top=70");
//	efView.focus();

}



function productNav (key,state) {
	if (key == currentInfo) {
	
	} else {
		switchNav(key +'N',state);
	}
}

function openInfo (key) {
	
	if (key == currentInfo) {
	
	} else {
		switchNav(currentInfo +'N',0);
		changeImages('',key +'N',key +'NOn');
		setClass(currentInfo,'optionBand',key,'optionDetail');
		currentInfo = key;
		lineUpInfo();
	//	if(key == 'productFabrics')
	//	{
	//		if(document.getElementById('fabricDiv').style.display == 'none')
	//		{
	//			document.getElementById('fabricDiv').style.display = 'block';
	//		}
	//	} else {
	//		document.getElementById('fabricDiv').style.display = 'none';
	//	}
	}

}


alOn = '';
function alNav(key,state) {
	if (key == alOn) {
	} else {
		if (state) {
			switchNav('aN'+ key +'i',1);
		} else {
			switchNav('aN'+ key +'i',0);
		}
	}
	// top.alert(('aN'+ key +'i.src'))
}

function alSwitch (key) {
	if (key == alOn) {
	
	} else {
		if (alOn != '') {
			switchNav('aN'+ alOn +'i',0);
			showHideLayers('','pa'+ alOn,'hidden')
		}
		if (key != '') {
			changeImages('','aN'+ key +'i','navaN'+ key +'iView')
			showHideLayers('','pa'+ key,'visible')
		}
		alOn = key
	}
}

	
function loadFlashZoom (viewerUrl,args) {

	var url = viewerUrl +'/flash/genericzoom.swf?'+ args;
	document.write(''+
//		'<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab" width="693" height="474" id="circ_player" align="middle">'+
		'<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab" width="340" height="380" id="productZoom">'+
		'	<param name="movie" value="' + url + '"/>'+
		'	<PARAM NAME=menu VALUE=false><PARAM NAME=scale VALUE=noscale> <PARAM NAME=salign VALUE=LT>'+
		'	<param name="quality" value="high" />'+
		'	<param name="play" value="true">' + 
		'	<param name="WMode" value="Transparent">' + 
		'	<embed src="' + url + '" swliveconnect="true" name="productZoom" '+
		'		quality="high" SWLIVECONNECT="true"'+
		'		width="340" '+
		'		height="380" '+
		'		border="0" wmode="transparent" '+
		'		type="application/x-shockwave-flash" '+
		'		flashvars="" ' + 
		'		pluginspage="http://www.adobe.com/go/getflashplayer" />'+
		'</object>'+
		'');

}

	function checkHeight () {
		top.alert(document.getElementById('innerFrame').innerHeight)
	}

	waitImg = newImage('/images/waitanimation.gif');
	lastGallElm = false;
	var lastProductId = false;
//	var galleryObjs = new array();

	function loadGallery (id,videoFirst) {
		lastProductId = id;
		var req = new Ajax.Request('/quickview/'+ id +'/',
			{
			method:'get',
			parameters: { media_only:'true', video:videoFirst }, 
			onSuccess: ShowGallery,
			onFailure: GalleryFailure
			});
		if (videoFirst) {
			urchinTracker('/product_images/'+ id)
		} else {
			urchinTracker('/product_video/'+ id)
		}

	}
	
	function GalleryFailure () {
		lastProductId = false;
		$('modalContent').update('<b>We are sorry but an error prevented us from loading this gallery</b>');
	}
	
	function ShowGallery (response) {
	
		assetXml = response.responseXML;

		$('modalNavContent').update('<div class=gallNav id=gNav></div>');
		
		items = assetXml.getElementsByTagName('item');
	
		for (i = 0; i < items.length; i++) {
			itm = items[i];

			thumbs = itm.getElementsByTagName('thumb');
			thumbValue = xmlGetValue(thumbs[0]);

			urls = itm.getElementsByTagName('url');

			if (urls.length > 0) {
				urlValue = xmlGetValue(urls[0]);
				var newElm = new Element('a', { 'id': 'img'+ i, 'style': "background-image:url('"+thumbValue+"')", 'rel': urlValue });
				$(newElm).observe('click', function(event){
						elm = Event.element(event);
				  		loadImageInModal($(elm).readAttribute('rel'));
				  		$(elm).addClassName('on');
				  if (lastGallElm) {
				      $(lastGallElm).removeClassName('on');
				  }
				  lastGallElm = elm;
			 	})
			}
			
			$('gNav').insert(newElm);
			
			if (i == 0) {
				loadImageInModal(urlValue);
				lastGallElm = newElm;
			}
		
		}
		
	}

	function modalMedia (media,track) {
		
		showModal();
		
		sizeWorkspace(480,500);
	//	$('modalNavContent').update('<div style="padding:10px">loading</div>');
	//	$('modalNavContent').style.float = 'left';
		$('modalContent').update('<img src="'+ waitImg.src +'">');
		document.getElementById('workspace').style.visibility = 'visible';
		
		loadImageInModal(media,track);
		
	}
	
	function xmlGetValue (xmlNode) {
		if (xmlNode.hasChildNodes()) {
			xmlNodeChild = xmlNode.firstChild;
			if (xmlNodeChild.nodeType == 3) {
				return xmlNodeChild.nodeValue;
			}
		}
		return false;
	}

	function productGallery (id,video) {
		
		showModal();
		
		if (id.length > 4) {
			id = id.substr(-4)
		}

		sizeWorkspace(480,500);
		$('modalNavContent').update('<div style="padding:10px">loading</div>');
	//	$('modalNavContent').style.float = 'left';
		$('modalContent').update('<img src="'+ waitImg.src +'">');
		document.getElementById('workspace').style.visibility = 'visible';
		
		loadGallery(id,video);	
	}
	
	function sizeWorkspace (w,h) {
		if (w) {
			$('workspace').style.width = w +'px';
			$('modalContent').style.width = w +'px';
		}
		if (h) {
			$('modalContent').style.height = h +'px';
		}

		mw = $('workspace').getWidth();
		mh = $('workspace').getHeight();
		
		page_pos  = getPagePosition();
		page_size = getPageSizeWithScroll();
		// win_size = document.viewport.getDimensions();
		
		var win_size = getInnerWindowSize();
		var uw = win_size[0];
		var uh = win_size[1];
		
		leftPos = ((uw - mw)/2);
		topPos = ((uh - mh)/2);
		//top.alert((uw) +" - "+ mh +" = "+ (uh - mh) + " ("+ topPos +")");
		leftPos = leftPos + page_pos[0];
		topPos = topPos + page_pos[1];
		
		if (topPos < 0) {
			topPos = 0;
		}
		
	//	top.alert(win_size.width +':'+ win_size.height);

		$('workspace').style.left = leftPos +'px';
		$('workspace').style.top = topPos +'px';
		
		 		
	}
	
	var imgElm;
	
	function loadImageInModal(url,productId) {
		
		$('modalContent').update('<img src="'+ waitImg.src +'">');
		if (url.indexOf('.flv') > 0) {
			// it is a video
			var FO_1 = {
				movie:"/catalog/singleVideo.swf",width:"480",height:"295",majorversion:"8",build:"0",allowfullscreen:'true',bgcolor:"#FFFFFF",
				flashvars:'videoUrl='+ encodeURI(url) +'&startVideo=true&fullExpand=true&trackingId='+ lastProductId
			 };
			UFO.create(	FO_1, "modalContent");
			sizeWorkspace(480,295);
		} else {
			imgElm = new Image();
			$(imgElm).observe('load', function (event) {
				// elm = Event.element(event);
				$('modalContent').update('<img src="'+ imgElm.src +'">');
				sizeWorkspace(imgElm.width,imgElm.height);
			})
			imgElm.src = url;
		}
	
	}


	function innerPage (pageUrl,w,h) {

		
		showModal();
		
		sizeWorkspace(w,h);
	//	document.getElementById('workspace').style.height = h +'px';
		document.getElementById('workspace').style.visibility = 'visible';
		document.getElementById('modalContent').innerHTML = "<iframe src='"+ pageUrl +"' id='innerFrame' width='100%' height='"+ h +"' frameborder=0 ></iframe>";
		
	}


	function loadModalView () {
	
		document.getElementById('workspace').innerHTML = "<div class=modalPadding><table cellspacing=0 border=0 cellpadding=0><tr><td id='modalContent' align=center></td></tr></table><div id='modalNav'><a href='javascript:hideModal()' id='modalClose'>CLOSE X</a><div id='modalNavContent'></div><div style='clear:both;height:0px;overflow:hidden'></div></div></div>";		
	
	}


	function showModal () {
	
		
		if (!document.getElementById('modalCover')) {
			body  = document.getElementsByTagName('body');
			cover = document.createElement('div');
			cover.setAttribute('id','modalCover');
			cover.setAttribute('class','coverOn');
			cover.setAttribute('onClick','hideModal()');
			body[0].appendChild(cover);
		}
		if (!document.getElementById('workspace')) {
			body  = document.getElementsByTagName('body');
			workspace = document.createElement('div');
			workspace.setAttribute('id','workspace');
			workspace.setAttribute('align','center');
			body[0].appendChild(workspace);
		}

		document.getElementById('modalCover').className = 'coverOn';
		resizeCover('modalCover');
		document.getElementById('workspace').style.display = 'block'
	
		objects = document.getElementsByTagName('object');
		for (i = 0; i < objects.length; i++) {
			$(objects[i]).hide();
		}
	
		if (!document.getElementById('modalContent')) {
			loadModalView();
		}
		
	}

	function hideModal () {

		objects = document.getElementsByTagName('object');
		for (i = 0; i < objects.length; i++) {
			$(objects[i]).show();
		}
	
		document.getElementById('modalCover').className = 'coverOff';
		document.getElementById('modalContent').innerHTML = '';
		document.getElementById('modalNavContent').innerHTML = '';
		document.getElementById('workspace').style.visibility = 'hidden';
		document.getElementById('workspace').style.display = 'none';
	
	}
	


function centerDiv(objId,width)
{

	obj = document.getElementById(objId);
	page_pos = getPagePosition();
	page_size = getPageSizeWithScroll();
	leftPos = ((page_size[0] - width)/2);
	if (leftPos < 0) leftPos = 0; 

	obj.style.width = width +'px';
	obj.style.left  = leftPos +'px';
	
}


function repositionDiv(objId,minDistance,plusDistance)
{

	obj = document.getElementById(objId);
	page_pos = getPagePosition();
	document.getElementById(objId).style.top = (page_pos[1] < minDistance ? minDistance : page_pos[1] + plusDistance) +'px';
	
}

function resizeCover (objId) {

	page_dims = getPageSizeWithScroll();
	document.getElementById(objId).style.height = page_dims[1]+'px';

}

function getPageSizeWithScroll()
{
	if (window.innerHeight && window.scrollMaxY) {// Firefox
		yWithScroll = window.innerHeight + window.scrollMaxY;
		xWithScroll = window.innerWidth + window.scrollMaxX;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		yWithScroll = document.body.scrollHeight;
		xWithScroll = document.body.scrollWidth;
	} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		yWithScroll = document.body.offsetHeight;
		xWithScroll = document.body.offsetWidth;
  	}
	arrayPageSizeWithScroll = new Array(xWithScroll,yWithScroll);
	return arrayPageSizeWithScroll;
}

function getPagePosition ()
{
  var scrOfX = 0, scrOfY = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}
	return [ scrOfX, scrOfY ];
}
	
