
//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 getURLParameter(name){
	  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	  var regexS = "[\\?&]"+name+"=([^&#]*)";
	  var regex = new RegExp( regexS );
	  var results = regex.exec( window.location.href );
	  if( results == null )
	    return "";
	  else
	    return results[1];	
}
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;

    trace('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;
	}
	if (colorID!=0){
		JQ("#visualizerColorName").text(collectionName);
	}
	
	if (colorID == 0 | isNaN(colorID)==true) 
	{
		visualizeChooseColor();
	}
	
	else {
		visualize(imgURI);
	}
    //trace('primary visualized = ' + collectionName + ' ' + colorID);
}
/* 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;
	
    //trace('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()
{

	JQ('.primaryVisualizerTarget').change(function(event){
		visualizePrimary(JQ('option:selected', this).text(),this.value);
	})
	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()
			//trace('primary visualized nested table= ' + targetName + ' ' + targetValue);
			
		}

		else{
			targetValue = JQ('.primaryVisualizer .primaryVisualizerTarget:visible').attr('value');
			targetName = JQ('.primaryVisualizer .primaryVisualizerTarget:visible option:selected ').text()
			//trace('primary visualized non-nested table= ' + targetName + ', ' + targetValue);
			
			
		} 
		
	}
	//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;

	
	

	if (targetValue == 0 | isNaN(targetValue)==true) {//if the select's value is 0 or (for some weird reason) return something that isn't a #, put "please choose a color" instead of nothing.
		
		visualizeChooseColor();
	}
	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 

	targetValue=JQ('.secondaryVisualizerTarget:visible').attr('value')//the value of the target that's passed to scene 7's viewer

	if (targetValue == 0) {//if the select's value is 0, put "please choose a color" instead of nothing.
		visualizeChooseColor();
	}
	else{
		visualizeSecondary(targetName,targetValue); 
	}


}

function trace(s) {
	  if (this.console && typeof console.log != "undefined")
	    console.log(s);
	  // the line below you might want to comment out, so it dies silent
	  // but nice for seeing when the console is available or not.
	  //else alert(s);
	}


function visualizeChooseColor(){
	visualize('ChooseColor');
	JQ("#visualizerColorName").text('No Color Selected');
}
///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);
		
		//set default image
		JQ("#visualizerColorName").text('No Color Selected');
		
		//set width from 0 to 200 (see it)
		JQ("#visualizerTD").css('width','200px');

		JQ("primaryVisualizerTarget").css('color','red');
		
		trace('onready target member length=' + JQ('.primaryVisualizerTarget').length )
        
		visualize('ChooseColor');
		//var str = JQ(e).parents("select").map(function() { return this.tagName; }).get().join(", ");
		//alert(str);
		
		

		//end startup 
		
		
		JQ('.primaryVisualizerTarget').change(function(event){
			trace('primary visualized nonvisible target fired= ' + targetName + ', ' + targetValue);
			visualizePrimary(JQ('option:selected', this).text(),this.value);
			
		})

		/*
        JQ('.primaryVisualizerTarget:visible').change(function(event){

			trace('primary visualized visible target fired= ' + targetName + ', ' + targetValue);
			JQ('.primaryVisualizerTarget').css("background-color","yellow");
			
			
			
			                            
			visualizePrimary(JQ('option:selected', this).text(),this.value);
			 
 
			
		})
		*/
		JQ('.primaryVisualizer').change(function(event){//put an event on the targets (ie hack)  
			                            
			refreshPrimary();
			trace('primary visualizer (plain) fired= '); 
			
 
			
		})		
		
		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');
			}
		})
			
    }
});


