/*
    Based on Lightbox JS by Lokesh Dhakar - http://www.huddletogether.com
      - Licensed under the Creative Commons Attribution 2.5 License - http://creativecommons.org/licenses/by/2.5/
*/

var currentView = 0;
var currentType = '';

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;
}

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 showProductView(){
	
    // prep objects
    var objOverlay = $('product-views-overlay');
    var objViews = $('product-views');
	
	document.body.style.position='relative';
	document.body.style.left='0px';
	document.body.style.top='0px';
	document.body.style.width='100%';
	
	$(document.body).insert ( objOverlay, { position:'bottom' });   
	objOverlay.style.position='absolute';
	objOverlay.style.top='0px';
	objOverlay.style.left='0px';
	objOverlay.style.background='#000000';
	objOverlay.style.opacity='.8';
	objOverlay.style.filter='alpha(opacity=80)'; 
	
	$(document.body).insert ( objViews, { position:'bottom' });
	objViews.style.position='absolute';
	objViews.style.top='0px';
	objViews.style.left='0px';	
	objViews.style.zIndex='100';
    
    var arrayPageSize = getPageSize();
    var arrayPageScroll = getPageScroll();

    // set height of Overlay to take up whole page and show
    objOverlay.style.height = (arrayPageSize[1] + 'px');
    objOverlay.style.display = 'block';

    // center lightbox and make sure that the top and left values are not negative
    // and the image placed outside the viewport
    var lightboxTop = ((arrayPageSize[3] - 35 - 472) / 2);
    var lightboxLeft = ((arrayPageSize[0] - 16 - 780) / 2);
    
    objViews.style.top = (parseInt(lightboxTop) < 0) ? "2px" : lightboxTop + "px";
    objViews.style.left = (parseInt(lightboxLeft) < 0) ? "0px" : lightboxLeft + "px";

    // Hide select boxes as they will 'peek' through the image in IE
    selects = document.getElementsByTagName("select");
    for (i = 0; i != selects.length; i++) {
        selects[i].style.visibility = "hidden";
    }

    objViews.style.display = 'block';

    arrayPageSize = getPageSize();
    objOverlay.style.height = (arrayPageSize[1] + 'px');

    setProductView(1);
}

function hideProductView()
{
    // get objects
    var objOverlay = $('product-views-overlay');
    var objViews = $('product-views');

    // hide lightbox and overlay
    objOverlay.style.display = 'none';
    objViews.style.display = 'none';

    // make select boxes visible
    selects = document.getElementsByTagName("select");
    for (i = 0; i != selects.length; i++) {
        selects[i].style.visibility = "visible";
    }

    // disable keypress listener
    document.onkeypress = '';
}

var embedMediaObjRaw
function setMediaType(type){
	setProductView(0, true);

	//stop all media from playing
	for(var i;i<document.embeds.length;i++){
		if(typeof(document.embeds[i].stop)=='function')
			document.embeds[i].stop(); 
	}

	if (cViews[type] > 1)  {
		$('view-prev').setStyle({display: 'block'});
		$('view-next').setStyle({display: 'block'});
	} else {
		$('view-prev').setStyle({display: 'none'});
		$('view-next').setStyle({display: 'none'});
	}

	if (currentType) {
		$('view-'+currentType).setStyle({display: 'none'});
		$('view-ctrl-'+currentType).setStyle({display: 'none'});
		$('media-type-' + currentType).removeClassName('current');
	}
	currentType = type;
	var viewCurrentType=$('view-'+currentType)
	
	//play embed on load, if it exists
	var embedRaw;
	if(viewCurrentType){ 
		viewCurrentTypeRaw=document.getElementById( 'view-'+currentType );
		if(viewCurrentTypeRaw){
			embedsRaw=viewCurrentTypeRaw.getElementsByTagName('embed');
			if(embedsRaw.length>0){
				embedRaw=embedsRaw[0]; 
				if( embedRaw && embedRaw.nodeName.toLowerCase()=='embed' ){ 
					embedRaw.autostart=true;   
					setTimeout('if(document.embeds.length && document.embeds["'+embedRaw.name+'"] && typeof(document.embeds["'+embedRaw.name+'"].Play)=="function") document.embeds["'+embedRaw.name+'"].Play()',1000); 					
				}	
			}
		}
	}
	
	viewCurrentType.setStyle({display: 'block'});
	$('view-ctrl-'+type).setStyle({display: 'block'});	
	$('media-type-' + type).addClassName('current'); 
	
	setProductView(1);
}

function setProductView(idx, force)
{
	if (force != true) {
    	if (idx < 1) idx = cViews[currentType];
	    if (idx > cViews[currentType]) idx = 1;
	}

    if (currentView != 0) {
    
    	var ele_1 = 'view-a-'+currentType+'-'+currentView;
    	var ele_2 = 'view-'+currentType+'-'+currentView;
    	
    	if( $(ele_1) )
        	$(ele_1).removeClassName('current');
		if( $(ele_2) )	
        	$(ele_2).setStyle({display: 'none'});
    }

    currentView = idx;

    if (idx != 0) {    	
     	img = $('view-'+currentType+'-'+currentView);
    	$('view-a-'+currentType+'-'+currentView).addClassName('current');
    	// must be two separate steps for IE; image has no size while hidden.
    	
    	// i don't know why but doing the audio paging screws up in Safari unless I give this a slight timeout
    	
    	setTimeout(function() {
	    	$('view-'+currentType+'-'+currentView).setStyle({display: 'block'});
    		$('view-'+currentType+'-'+currentView).setStyle({top: (408 - img.height)/2 + 'px', left: (590 - img.width)/2 + 'px'});
    	}, 100);
	}

    return currentView;
}

function zoomProductView()
{
    // TBD
}

