
//var cvURL = 'http://s7d1.scene7.com/is-viewers-3.9/flash/flyoutviewer.swf?flyout=300,0,0,0&zoomFactor=2&transition=none,0.5&transparent=true&fmt=png&tips=Roll%20over%20image%20to%20zoom%20in,Roll%20off%20image%20to%20zoom%20out,000000,10&checkPolicy=true&image=Blinds/';
var cvURL = 'http://s7d1.scene7.com/is-viewers-3.9/flash/flyoutviewer.swf?stagesize=200,330&amp;flyout=200,200,0,340&amp;zoomFactor=3.5&amp;transition=wipe,0.5&transparent=true&fmt=png&tips=Roll%20over%20image%20to%20zoom%20in,Magnified%20image%20displayed%20below,000000,11&checkPolicy=true&image=Blinds/';
//this is the ROOT of the uri that's passed to scend 7
/*
 * URI format
 * cvURLproductID_primaryColorID_secondaryColorID
 * primary 
 */ 
/* Version 1.3 28 May 2008
   http://adamv.com/dev/javascript/qslicense.txt */

function Querystring(qs) {
    this.params = {};
    
    if (qs == null) qs = location.search.substring(1, location.search.length);
    if (qs.length == 0) return;

    qs = qs.replace(/\+/g, ' ');
    var args = qs.split('&');
    
    for (var i = 0; i < args.length; i++) {
        var pair = args[i].split('=');
        var name = decodeURIComponent(pair[0]);
        
        var value = (pair.length==2)
            ? decodeURIComponent(pair[1])
            : name;
        
        this.params[name] = value;
    }
}

Querystring.prototype.get = function(key, default_) {
    var value = this.params[key];
    return (value != null) ? value : default_;
}

Querystring.prototype.contains = function(key) {
    var value = this.params[key];
    return (value != null);
}


function changeImage(collectionName, colorID)
{
    var imagePath;

    eval('imagePath = ' + collectionName + 'Img[' + colorID + ']');
    JQ('#' + collectionName).attr('src', imagePath);
}

function visualize(imgURI)//this passes the url to s7 and refreshes the flash object (from /js/flashdetect.js)
{
    var myURL = cvURL + imgURI;
	if (typeof(console) !== 'undefined' && console != null) {
		console.log('myURL is ' + myURL);
	}	
        var so = new SWFObject(myURL, "movie", "100%", "340px", "8", "white");
        so.addParam('movie', myURL); 
        so.addParam("wmode", "transparent");
        so.addParam("allowScriptAccess", "always");
        so.write("colorVisualizer");
		


}
/* CHB 8/2009
 * this visualizes the main color of the picture (instead of say, the tapes) and pass it to the visualize() function
 */
function visualizePrimary(collectionName, colorID){
	var imgURI;
	
	var qs = new Querystring()
	if (colorID == 0) {
		window.PrimaryColor = null;
	}
	
	imgURI = qs.get('productID') + '_' + colorID;
	
	window.cvPrimaryColor = imgURI;
	if (window.cv2ndColor != null && window.cv2ndColor != undefined && window.cv2ndColor != 0) {
		imgURI += '_' + window.cv2ndColor;
	}
	
	JQ("#visualizerColorName").text(collectionName);
	
	if (colorID == 0) {
		visualize('ChooseColor');
	}
	
	else {
		visualize(imgURI);
	}
}
/* CHB 8/2009
 * * this visualizes the secondary color of the picture (like  tapes) and pass it to the visualize() function
 * 
 */
function visualizeSecondary(collectionName, colorID)
{
    var imgURI;

    if (!window.cvPrimaryColor)
    {
        imgURI = 'ColorNA'; 
    }
    else
    {
        imgURI = window.cvPrimaryColor + '_' + colorID;
    }

    window.cv2ndColor = colorID;
	
    if (typeof(console) !== 'undefined' && console != null) {
		console.log('secondary visualizer =' + imgURI);
	}
	targetname=JQ('option:selected', this).text();
	visualize(imgURI);
}

/*
 * these refreshes the flash picture, and figures out which values to pass to the primary/secondary visualizers
 */

function refreshPrimary()
{

	targetValue=0;
	targetName='';
	//did this have a parent (within a radio group) 
	if (JQ('span.primaryVisualizer').length > 0 ){
		//check to see if it's nested inside a table
		if (targetValue = JQ('.primaryVisualizer table:visible .primaryVisualizerTarget').attr('value')) {
			targetValue = JQ('.primaryVisualizer table:visible .primaryVisualizerTarget').attr('value');
			targetName = JQ('.primaryVisualizer table:visible .primaryVisualizerTarget option:selected ').text()
		}
		else{
			targetValue = JQ('.primaryVisualizer .primaryVisualizerTarget').attr('value');
			targetName = JQ('.primaryVisualizer .primaryVisualizerTarget option:selected ').text()
			
		} 
		
	}
	//no just a plain non-nested option
	else{
		targetValue=JQ('.primaryVisualizerTarget:selected').attr('value');	
		targetName=JQ('.primaryVisualizerTarget:selected').text();
	} 
	 
	secondaryTargetValue=JQ('.secondaryVisualizer table:visible .secondaryVisualizerTarget').attr('value');;
	
	//assign them to globals so they can be used in visualizeXX
	window.cvPrimaryColor=targetValue;
	window.cvPrimaryColorName=targetName;
	
	window.cv2ndColor=secondaryTargetValue;
	//console.log(secondaryTargetValue);
	
	

	if (targetValue == 0) {//if the select's value is 0, put "please choose a color" instead of nothing.
		visualize('ChooseColor');
	}
	else{
		
		visualizePrimary(targetName,targetValue); 
	}
	
	

}
function refreshSecondary()//I know there shouldn't be two of these but don't have time to combine, see comments in refreshPrimary()
{

	targetName=JQ('.secondaryVisualizerTarget:visible').attr('name')//dunno why we pass in the collection name, but not changing it in case 
	//console.log('target name is ' + targetName);
	targetValue=JQ('.secondaryVisualizerTarget:visible').attr('value')//the value of the target that's passed to scene 7's viewer
	//console.log('target value is ' + targetValue);
	if (targetValue == 0) {//if the select's value is 0, put "please choose a color" instead of nothing.
		visualize('ChooseColor');
	}
	else{
		visualizeSecondary(targetName,targetValue); 
	}


}
///this is kind of the constructor of the visualizer.  
JQ('document').ready(function() {

/*
 * 
 */

    if (isProductVisualizable)
    {
		//toggle the header iamge to be visible 
		
    	JQ('#visualizerHeaderImg').css('display','block'); 
		//set the product disclaimer and stuff it into the div
 			
		JQ("#visualizerProductDisclaimer").text(disclaimer_text);
		
		JQ("#visualizerColorName").text('No Color Selected');
		
		JQ("#visualizerTD").css('width','200px');
        visualize('ChooseColor');
		JQ('.primaryVisualizerTarget').change(function(event){//put an event on the targets (ie hack)  
			                            
			visualizePrimary(JQ('option:selected', this).text(),this.value);
			 
 
			
		})
		
		JQ('.secondaryVisualizerTarget').change(function(event){//put an event on the targets (ie hack)
			
			visualizeSecondary(this.name,this.value);
			
		})


		
		JQ('.missingVisualizer').change(function(event)
		//place an event on all of the neighbors of the select list
		{
			if( JQ('.missingVisualizer').is(":visible"))
			{
			visualize('ColorNA');
			console.log('missing fired');
			}
		})
		

    }
});

