﻿/****************************************************/
/****************************************************/
//	ezprints.com skus.js - handles the following functions for the client:

//	- launch product builder ( takes in productName, activeSku, skuList )
//	- populate drop down with related size skus, where applicable
//	- updates widget if related size is selected from the drop down
//	- add all skus to drop down for quick launch / demo purposes mainly
//	- set Single Product Description ( if there are no related skus, it lists the active product and pricing )
//  - set product title and description ( if there is an additional container to hold more description, it populates the container with title and description -- you will need to create your own array for product descriptions though and match on them )
/****************************************************/
/****************************************************/

// clear vars
var widget = null;
var sku = null;
var productBuilderUrl = null;
var templateId = null;
var screenResUri = null;
var printResUri = null;
var printResHeight = null;
var printResWidth = null;
var productNameContainer = null;
var productDescriptionContainer = null;
var productDescriptionTitle = null;
var productDescriptionCopy = null;
var singleProductDescriptionContainer = null;
var skuProductName = null;
var productSelectDropDown = null;
var loadSetPhoto = false;

// construct array to contain query string parms
var qsParm = new Array();

// ezp constructor
if( !ezp ) var ezp = {};

ezp.skus = {

	init: function( )
	{
		this._parseQuery();
	}
	
	,launchProductBuilder: function( productName, activeSku, skuList )
	{
		var finalUrl = productBuilderUrl;
		
		if( productName.match("%") )
		{
			activeSku = productName.split("%%%%%")[1];
			productName = productName.split("%%%%%")[0];
		}

		if( typeof( activeSku ) == "undefined" )
		{
			activeSku = this._getActiveSku( productName );
		} 
		
		if( typeof( skuList ) == "undefined" )
		{
			skuList = this._getSkuList( productName );
		}
		
		finalUrl += "?productName=" + productName;
	
		if (typeof(activeSku)!="undefined") {
			finalUrl += "&activeSku=" + activeSku;
		}
	
		if ( skuList != "") {
			finalUrl += "&skus=" + skuList;
		}

		document.location = finalUrl;
		return false;	
	}
	
	,launchProductBuilderwJSON: function( parms )
	{
	    var finalUrl = productBuilderUrl;
	    
	    if( parms.productName )
	        productName = parms.productName;
	        
	    if( typeof( parms.activeSku ) == "undefined" )
	        activeSku = this._getActiveSku( productName );
	    else
	        activeSku = parms.activeSku;
	        
	    if( typeof( parms.skuList ) == "undefined" )
            skuList = this._getSkuList( productName );	        
	    else
	        skuList = parms.skuList;
	    
	    finalUrl += "?productName=" + productName;
	    
	    if (typeof(activeSku)!="undefined")
			finalUrl += "&activeSku=" + activeSku;
	
		if ( skuList != "")
			finalUrl += "&skus=" + skuList;
		
		if( parms.templateId )
	        finalUrl += "&templateId=" + parms.templateId;
	        
	    if( parms.screenResUri )
	        finalUrl += "&screenResUri=" + parms.screenResUri;
	    
	    if( parms.printResUri )
	        finalUrl += "&printResUri=" + parms.printResUri;
	    
	    if( parms.printResHeight )
	        finalUrl += "&printResHeight=" + parms.printResHeight;
	        
	    if( parms.printResWidth )
	        finalUrl += "&printResWidth=" + parms.printResWidth;

		document.location = finalUrl;
		return false;	
	       
	} 
	
	// return active sku for product builder parm
	,_getActiveSku: function( productName )
	{
		var skus = new Array();
		
		skuArray.each( function( item )
		{
			if( item.productName.toLowerCase() == productName.toLowerCase() )
			{
				skus.push( item.sku );
			}
		} );
		
		if( skus )
			return skus.first();
	}
	
	// return sku list for product builder parm
	,_getSkuList: function( productName )
	{		
		var skuList = "";
		skuArray.each( function ( item )
		{
			if( item.productName.toLowerCase() == productName.toLowerCase() )
			{
				skuList += item.sku + ",";
			}
		} );
		
		return skuList;
	} 
	
	// adds each sku as a new option to drop down
	,_addOption: function( selectbox, text, value, productName, price )
	{
		var optn = document.createElement("OPTION");
		if( price )
			optn.text = text + " --- $" + price;
		else	
		optn.text = text;
		if( productName )
			optn.value = productName + "%%%%%" + value;
		else	
			optn.value = value;
		if( $( selectbox ).options )
			$( selectbox ).options.add( optn );
		else 
			$( selectbox ).add(optn);

		if( optn.value == sku )
			optn.selected = "selected";

	}
	
	// populate a combo box with all available skus
	,addAllSkus: function( selectbox )
	{		
		var This = this;
		this._addOption( selectbox, "Please select a product" );
		skuArray.each( function( item )
		{
			This._addOption( selectbox, item.description, item.sku, item.productName, item.price );
		} );
	}
	
	,addAllSkusSimple: function( selectbox )
	{		
		var This = this;
		this._addOption( selectbox, "Please select a product" );
		skuArray.each( function( item )
		{
			This._addOption( selectbox, item.description, item.sku, null, item.price );
		} );
	}	
	
	// pull the keys and values from the query string
	// process the query string array to display product name, set active sku, 
	// and populate the related sku drop down
	,_parseQuery: function( )
	{
		qsParm["productName"] = null;
		qsParm["activeSku"] = null;
		qsParm["skus"] = null;
		
		var query = window.location.search.substring(1);
		var parms = query.split('&');
		for (var i=0; i<parms.length; i++) {
			var pos = parms[i].indexOf('=');
			if (pos > 0) {
				var key = parms[i].substring(0,pos);
				var val = parms[i].substring(pos+1);
				qsParm[key] = val;
			}
		}
		
		if( qsParm["templateId"] )
		    templateId = qsParm["templateId"];
	        
	    if( qsParm["screenResUri"] )
	        screenResUri = qsParm["screenResUri"];
	    
	    if( qsParm["printResUri"] )
	        printResUri = qsParm["printResUri"];
	        
	    if( qsParm["printResHeight"] )
	        printResHeight = qsParm["printResHeight"];
	        
	    if( qsParm["printResWidth"] )
	        printResWidth = qsParm["printResWidth"];
		
		if( qsParm["activeSku"] )
		{
			// update sku from querystring
			this.setActiveSku( qsParm["activeSku"] );
		}
		// check to see if only activeSku is provided, no skuList also
		if( qsParm["activeSku"] && qsParm["skus"] == null)
		{
			Event.observe( window, "load", function( evt )
			{	
				ezp.skus._emptySkuListHandler();
			} );
		}
	}
	
	// pull activeSku from querystring, drop down or elsewhere and update sku var
	,setActiveSku: function( activeSku )
	{
		if( activeSku ) 
		{
			sku = activeSku; 
		}
			
			ezp.skus._updateWidget();	
	}
	
	// either creates widget or passes active sku to widget
	,_updateWidget: function( )
	{
		if (widget)
		{
			if( productNameContainer )	
				this.setProductTitle( );
			if( productDescriptionContainer )
				this.setProductDescription();
			widget.setSku(sku);
		}
		else
		{
			// if widget has not yet loaded
			Event.observe( window, "load", function( evt )
			{
				if( productNameContainer )	
					ezp.skus.setProductTitle( );
				if( productDescriptionContainer )
					ezp.skus.setProductDescription();
				createWidget();
				
				// if a photo uri is passed in
				if( screenResUri )
				{
				   loadSetPhoto = true;  
				}
			} );
		}
	}
	
	// returns productName to populate skuList of related skus
	,_getProductName: function( )
	{
		var skuProductName = "";
		
		skuArray.each( function( item )
		{
			if( item.sku == sku )
			{
				skuProductName = item.productName;
			}
		} );
		
		return skuProductName;
	}
	// add related sku to drop down
	,addRelatedSkus: function( selectbox )
	{
		var This = this;
		if( qsParm["skus"] )
		{
			skus =  qsParm["skus"];
			var skuList = new Array();
			skuList = skus.split(',');
			if( skuList.length > 2 )
			{
				// hide single product description container
				if( $( singleProductDescriptionContainer ) )
					$( singleProductDescriptionContainer ).hide();
				// restore select box to default
				$( selectbox ).length = 0;
				$( selectbox ).show();
				This._addOption( $( selectbox ), "Select a different size", "" );
				
				skuList.each( function( item )
				{
					var description = "";
					var price = "";
					if( item != "" )
					{
						// get item description from sku array
						skuArray.each( function( skuItem )
						{
							if( skuItem.sku == item )
							{
								description = skuItem.description;
								price = skuItem.price;
								return;
							}
						} );
						// add related sku to drop down
						This._addOption( $( selectbox ), description, item, '', price );
					}
				} );
			}
			else {
				$( selectbox ).hide();
				if( $( singleProductDescriptionContainer ) )
				{
					this.setSingleProductDescription();
				}
			}
		}
	}
	
	// if the builder page is accessed directly, with only an activeSku query
	,_emptySkuListHandler: function( )
	{
		skuProductName = ezp.skus._getProductName( );
		qsParm["skus"]  = ezp.skus._getSkuList( skuProductName );
		ezp.skus.addRelatedSkus( productSelectDropDown );
	}
	
	// text update on UI for single product description container
	,setSingleProductDescription: function( )
	{
		$( singleProductDescriptionContainer ).show();
		skuArray.each( function( item )
		{
			if( item.sku == sku )
			{
				var price = item.price != "" ? " : $" + item.price : "";
				$( singleProductDescriptionContainer ).innerHTML = item.productName + price;		
			}
		} );
	}
	// text update on UI for product name
	,setProductTitle: function( )
	{
		if( sku )
		{
			skuArray.each( function( item )
			{
				if( item.sku == sku )
				{
					$( productNameContainer ).innerHTML = item.description;
				}
			} );
		}
	}
	// if additional column for extra copy, this will find and populate it
	,setProductDescription: function( )
	{
		var hasCopy = false;
		if( sku )
		{
			productDescriptionArray.each( function( item )
			{
				if( item.sku == sku )
				{
					hasCopy = true;
					$( productDescriptionContainer ).show();
					$( 'builderContainer' ).className = "bldrMinScreen";
					
					if( $( productDescriptionTitle ) )
					{
						$( productDescriptionTitle ).innerHTML = item.name;
					}
					if( $( productDescriptionCopy ) )
					{
						$( productDescriptionCopy ).innerHTML = item.description;
					}
				}
			} );
		}
		// if no match found
		if( !hasCopy )
		{
			$( productDescriptionContainer ).hide();
			$( 'builderContainer' ).className = "bldrFullScreen";
		}
	}
}

// initialize skus
ezp.skus.init();