/* -------------------------------------------------------------

  	 # Design Download JavaScript Application.		  #
   	 # Author: iBarnett.com (jason@ibarnett.com)	  # 
     # Date: 2/29/2008								  #
     # Version: 1.0									  #

------------------------------------------------------------- */


// ----------------------------------------------
// Setup global variables used in functions
// ----------------------------------------------
	var DESIGN_SELECTED = null;
	var COLOR_SELECTED  = null;
	var DESIGNS         = new Array();



// ----------------------------------------------
// "Object" to "register" deisgn packages
// ----------------------------------------------
	function Design(){
		
		var obj = new Object();
  		obj.design     = DESIGN_PATH + arguments[0];
		obj.designIcon = DESIGN_PATH + arguments[1];
		obj.colors     = new Array();		
		
		DESIGNS.push(obj);
		this.position = DESIGNS.length-1;
		this.addColor = _addColor;
	}
	function _addColor(_color,_colorIcon,_download){
		colObj = new Object();
		colObj.color     = COLOR_PATH + _color;
		colObj.colorIcon = COLOR_PATH + _colorIcon;
		colObj.download  = _download.indexOf("http")==0 ? _download : DOWNLOAD_PATH + _download;
		
		DESIGNS[this.position].colors.push(colObj);
	}
	
// ----------------------------------------------
// Functions to display buttons/links for designs
// and colors
// ----------------------------------------------
	function DisplayDesignIcons(){
		if(DESIGNS.length<=0){
			alert("Sorry. There are not any designs registered.");
			return;
		}
		
		str = "<ul>";
		for(x=0;x<DESIGNS.length;x++){
			str += "<li id='design_"+(x+1)+"'><a href='#' onclick='pageEventsTracking.setAsyncClickEvents(\"ChooseDesign\");SelectDesign("+x+")' /><span>Design "+(x+1)+"</span></a></li>";
		}
		str += "</ul>";
		
		div = document.getElementById("bottom");
		div.innerHTML = str;
	}
	function DisplayColorIcons(_designPosition){
	
		if(!_designPosition){ _designPosition=0; }
	
		str = "<ul>";
		for(x=0;x<DESIGNS[_designPosition].colors.length;x++){
			str += "<li><img src='"+DESIGNS[_designPosition].colors[x].colorIcon+"' border='0' onclick='pageEventsTracking.setAsyncClickEvents(\"ChooseColor\");SelectColor("+_designPosition+","+x+")' /></li>";
		}
		str += "</ul>";
		
		div = document.getElementById("left");
		div.innerHTML = str;
	}
	function DisplayDownloadButton(){
		str = '<a href="#" onclick="pageEventsTracking.setAsyncClickEvents(\'ProceedToDownload\');Disclaimer();return false;" />';

		div = document.getElementById("download");
		div.innerHTML = str;
	}
	function BuildStructure(){

		// Disclaimer Popup -->
		str  = '<div id="disclaimer"></div>';
		// Left Side: Color Selector -->
		str  += '<div id="left"></div>';
		// Display Area: Display of deisng -->
		str += '<div id="design_stage"></div>';
		// Bottom: Design Selector -->
		str += '<div id="bottom"></div>';
		// Bottom Left: Downlaod Button -->
		str += '<div id="download"></div>';

		div = document.getElementById("dd-container");
		div.innerHTML = str;

	}

// ----------------------------------------------
// Functions called when a link is clicked
// ----------------------------------------------
	
	function SelectDesign(_position){
		d = DESIGNS[_position];
		if(!d){ alert(" Could not find the design. "); return; }
		
		DisplayColorIcons(_position);
		SetStage(d.design);
		DESIGN_SELECTED = d;
		SelectColor(_position,0);
	}
	
	function SelectColor(_designPosition,_position){
		if( !DESIGN_SELECTED ){
			alert( "Sorry. You first need to pick a design before changing colors." );
			return;
		}

		d = DESIGNS[_designPosition];
		if(!d){ alert(" Could not find the design. "); return; }
		c = d.colors[_position];
		if(!d){ alert(" Could not find the color. "); return; }
		SetStage(c.color);
		COLOR_SELECTED = c;
	}
	
	function CloseDisclaimer(){
			div = document.getElementById("disclaimer");
			if(div){
				div.innerHTML = "";
				div.style.display="none";
			}
			
			document.body.onclick = null;
	}
	function Disclaimer(){
		if(!DISCLAIMER_USED){ 
			Download();
		}else{
			x = Math.ceil(((document.getElementById("dd-container").scrollWidth)-DISCLAIMER_WIDTH)/2);
			y = Math.ceil(((document.getElementById("dd-container").scrollHeight)-DISCLAIMER_HEIGHT)/2);

			disc = document.getElementById("disclaimer");
			disc.style.top    = y+"px";
			disc.style.left   = x+"px";
			disc.style.width  = DISCLAIMER_WIDTH+"px";
			disc.style.height = DISCLAIMER_HEIGHT+"px";

			if(DISCLAIMER_IMAGE){
				b1Click = DISCLAIMER_BUTTON1 ? "PopSite('"+DISCLAIMER_BUTTON1+"');" : "Download();return false;";
				b2Click = DISCLAIMER_BUTTON2 ? "PopSite('"+DISCLAIMER_BUTTON2+"');" : "Download();return false;";
				str = "<a id='button1' href='#' onclick=\"pageEventsTracking.setAsyncClickEvents('DownloadTemplate');"+b1Click+"\"></a><a id='button2' href='#' onclick=\"pageEventsTracking.setAsyncClickEvents('OfficeTrial');"+b2Click+"\"></a><img src='"+DISCLAIMER_IMAGE+"' border='0' alt='Disclaimer'/>";
			}else{
				str = "";
			}
			
			disc.innerHTML = str;
			disc.style.display="block";
			disc.style.cursor="pointer";
			disc.onclick=CloseDisclaimer;

			if(DISCLAIMER_AUTO_DOWNLOAD){
				setTimeout("Download();",300);
			}
			//setTimeout("document.body.onclick = CloseDisclaimer",301);
		}
	}
	function Download(){
		if( !DESIGN_SELECTED || !COLOR_SELECTED ){
			alert( "Sorry. You first need to pick a design & color before downloading." );
			return;
		}
            
		// download the file! (to the iFrame)
		switch(DOWNLOAD_TARGET.toLowerCase()){

			case "new" :
				win=open( COLOR_SELECTED.download, "download", "width=100,height=100,resizeable=yes,scrollbars=yes,menubars=no,toolbars=no" );
				win.focus();
				setTimeout('win.close();',3000);
				break;
			case "self" : 
			    win=open( COLOR_SELECTED.download, "_top" );
				break;
			default : 
			
				if(DOWNLOAD_TARGET.toLowerCase().indexOf("http:")>-1 || DOWNLOAD_TARGET.toLowerCase().indexOf("https:")>-1 || DOWNLOAD_TARGET.toLowerCase().indexOf("ftp:")>-1){
					file = DOWNLOAD_TARGET + "?download=" + escape(COLOR_SELECTED.download);
				}else{
					file = COLOR_SELECTED.download;
				}

				win=open( file, "download", "width=100,height=100,resizeable=yes,scrollbars=yes,menubars=no,toolbars=no" );
				win.focus();

		}
	}
	
	function PopSite(url){
		win=open(url,"newWindow","location=yes,address=yes,status=yes,toolbar=yes,menubars=yes,scrollbars=yes,resizable=yes");
		win.focus();
	}
	
	function SetStage(_img){
		stage = document.getElementById("design_stage");
		stage.innerHTML = "<img src='"+_img+"' border='0' />";
	}

// --------------------------------------------
// Function called onload of page with app
// --------------------------------------------
function dd_Init(){
	
	BuildStructure();
	
	DisplayDesignIcons();
	DisplayColorIcons();
	DisplayDownloadButton();
	
	// start off with design 1, color 1
	if(START_WITH_DESIGN){
		SelectDesign(0);
		SelectColor(0,0);
	}else{
		SetStage(START_IMAGE);
	}
}


// -----------------------------------------------------------------------
// EOF
// -----------------------------------------------------------------------