//Inject heder js

var wgt_inj_host = 'http://www.thesearchprogram.com';
//var wgt_inj_host = 'http://thesearchprogram.hal9000.adtnet.net';

//############################################################################################################
// CONFIGURATION STARTS
// INJECTION LOGIC
var wgt_inj_anchor = 'seo_wgt';			// Defines the div name the widget will create to inject itself
var wgt_inj_container = '';				// Defines container element (div) for this widget. Empty = body node 
var wgt_inj_end = false;				// Defines if the widget will be injected at the end of the container.

// CONTENT
var wgt_con_images = '/seo/Widgets/view/germain/Nissan/images';		// Base URL for the image folder
var wgt_con_closeAlt = 'close';				// Alt tag displayed on close button
var wgt_con_closeImg = 'btn_close.gif'; 	// Close image name 
var wgt_con_openAlt = 'open'; 				// Alt tag displayed on open button
var wgt_con_openImg = 'btn_open.gif'; 		// Open image name
var wgt_con_background = 'bg_header.gif';	// Background image for the title
var wgt_title_text = '';					// Text in the title

// BEHAVIOR
var wgt_beh_forceOpen = true;			// Widget will close first time no matter what
var wgt_beh_close = 3500;				// Time out to close
var wgt_beh_open = 1;					// Time out to open
var wgt_beh_steps = 10;					// Divisions in the height to close wodget
var wgt_beh_stepTime = 150;				// Miliseconds to expand/collapse one step
// TRACK CODES
var wgt_track_display = '2818';				// Tracking for the widget display
var wgt_track_open = '2820';				// Tracking for the widget open
var wgt_track_close = '2819';				// Tracking for the widget close
var wgt_track_submit = '2822';				// Tracking for the widget submit
var wgt_track_reopen = '2820';				// Tracking for the widget reopen
var wgt_track_earlyClose = '2819';			// Tracking for the widget early close

// DIMENSIONS and styles
var wgt_dimension_width = 988;		// Widget width
var wgt_dimension_height = 249;		// Widget height
var wgt_dimension_title = 56;			// Widget title height
var wgt__frameStyle = '';				// Frame style (optional, will overide default)
//var wgt__fixed_frameStyle = 'height:' + wgt_dimension_height + 'px !important; width:'	+ wgt_dimension_width	+ ';margin: 0 auto!important;display:block;';				// Style used for the IFRAME
var wgt__fixed_frameStyle = 'height:' + wgt_dimension_height + 'px !important; width:'	+ wgt_dimension_width	+ ';';				// Style used for the IFRAME
var wgt__headStyle = '';				// Head style (optional, will overide default)   
var wgt__btnStyle = ''; 					// Expand collapse button style (optional, will overide default)
var wgt__textStyle = ''; 				// Style for the text in the header		
// CONFIGURATION ENDS
//############################################################################################################
	
var wgt_WebSiteId = 999;		// dispatcher changes this
var wgt_view =wgt_inj_host + '/seo/Widgets/view/germain/Nissan/index.php';			// dispatcher sets this


// INIT
wgt_con_closeImg = wgt_inj_host + wgt_con_images + "/" + wgt_con_closeImg;
wgt_con_openImg = wgt_inj_host + wgt_con_images + "/" + wgt_con_openImg;


if(wgt__headStyle == ''){
	wgt__headStyle = 'width:' + wgt_dimension_width 
		+ 'px!important; height:' + wgt_dimension_title 
		+  'px!important; background: url(' 
		+ wgt_inj_host + wgt_con_images + '/' + wgt_con_background 
		+ ') center center !important;';
		//+ ') center center !important;margin-left:auto!important; margin-right:auto!important;';
}
if(wgt__btnStyle == ''){
	wgt__btnStyle = "float:right!important;"
}
if(wgt__frameStyle == ''){
	wgt__frameStyle = 'overflow: hidden; height:0px; width:' 
		+ wgt_dimension_width 
		+ '';
		//+ ';margin: 0 auto!important;display:block;';
}
if(wgt_view == ''){
	wgt__headStyle = "display:none";
	wgt__frameStyle = "display:none";
}
// load referer and qs
var r = document.referrer;
r = r.replace(/&/g, "@@");
var qs = location.search.substring(1);
qs = qs.replace(/&/g,"@@");

// Decalre variables
var movableObjects = new Array();
var movableObectsTop = new Array();
var firstLoad = false;
var formActive = true;
var formActiveIE = false;		
var wgt_Frame = wgt_inj_anchor + "_frame";			
var secondCheck = false;
var activeMouse = false;
var currentH = 0;
var slide_direction_down = false; 
var blnCompleted = false;
var wgt_enableToggle = false;
 
function slide(){
	try{
		var fra = document.getElementById('wgt_statusback');			
		if (slide_direction_down && currentH < wgt_dimension_height ){				
			currentH +=(wgt_dimension_height/wgt_beh_steps);		
			if (currentH > wgt_dimension_height ){
				currentH = wgt_dimension_height;
			}				
			fra.style.height = currentH + "px";
			fra.height = currentH;			
			setTimeout('slide();',wgt_beh_stepTime);	
		}
		if (!slide_direction_down && currentH > 0){				
			currentH -=(wgt_dimension_height/wgt_beh_steps);	
			if (currentH < 0 ){
				currentH =0;
			}	
						
			fra.style.height = currentH  + "px";					
			fra.height = currentH;
			setTimeout('slide();',wgt_beh_stepTime);							
		}	
		if (currentH == 0 ){
			document.getElementById('wgt_butimage').src=wgt_con_openImg;
			document.getElementById('wgt_butimage').alt=wgt_con_openAlt;		
		}
		if (currentH == wgt_dimension_height){
			document.getElementById('wgt_butimage').src=wgt_con_closeImg;
			document.getElementById('wgt_butimage').alt=wgt_con_closeAlt;			
		}
					
		// Set movable objects
		// General movable
		if(!navigator.appName.match("Internet Explorer")){
			// FF
		}else{
			// IE
		}
		var movableTop = 0;
		for(i=0 ; i < movableObjects.length;i++){
			if(movableObjects[i] != null){
				movableTop = movableObectsTop[i] + currentH;	
				movableObjects[i].style.position = 'absolute';
				movableObjects[i].style.top = movableTop + 'px' ;
			}
		}	
	}catch(e){
		return;		
	}	
}

function mouseOut(){
	var imgObj = document.getElementById('wgt_butimage');
	if(imgObj != null){
		if (currentH == 0 ){	
			if(imgObj.src != wgt_con_openImg){
				imgObj.setAttribute('src', wgt_con_openImg);
				imgObj.setAttribute('alt', wgt_con_openAlt);
			}
		}
		if (currentH == wgt_Height){
			if(imgObj.src != wgt_con_closeImg){
				imgObj.setAttribute('src', wgt_con_closeImg);
				imgObj.setAttribute('alt', wgt_con_closeAlt);
			}	
		}
	}
}

function slidedown(){
	slide_direction_down = true; 
	slide();
}
 
function slideup(){
	slide_direction_down = false;	
	slide();
}

function close(){	
	if (!(formActive || formActiveIE || activeMouse ) || !wgt_beh_forceOpen){
		if (secondCheck){
			blnCompleted = true;
			slideup();	
			secondCheck = false;
			wgt_beh_forceOpen = true;
		}else{
			secondCheck = true;
		}
	}else{
		secondCheck = false;
	}			
	setTimeout('close();',wgt_beh_close);	
}

function togglePushdown(){
	if(wgt_enableToggle){
		try{
			if (slide_direction_down){
				slideup();	
				if(!blnCompleted){
					trackPixel( wgt_track_earlyClose);	
				}	
			} else {
				slidedown();		
				trackPixel(wgt_track_reopen);
			}
		}catch(e){
			return;	
		}	
	}
}

function wait4Response(){
		if (firstLoad){
			//trackPixel(wgt_track_submit);			
			//alert("Your request was received\r\n       Thank you!");		
			formActive = false;	
			formActiveIE = false;			
			slideup();															
		}else{
			firstLoad = true;
		}			
}

function showFirst(){
	setTimeout('firstOpen();',wgt_beh_open);	
}

function firstOpen(){
	wgt_enableToggle = true;
	//slidedown();
	//setTimeout('close();',wgt_beh_close);		
	trackPixel(wgt_track_display);
}

function setMovableObject(tagName, attName, attValue, movTop, movIndex){	
	if(movableObjects.length > movIndex ){
		if(movableObjects[movIndex] != null){
			return;
		}		
	}	
	var toMove = document.getElementsByTagName(tagName);
	var toMoveContent = '';	
	movableObjects[movIndex] = null;
	for(i=0; i<toMove.length; i++){		
		if(toMove[i].getAttribute(attName) == attValue){
			movableObjects[movIndex] = toMove[i];
			movableObectsTop[movIndex] = movTop; 
			i = toMove.length;		
		}
	}
}

var tracking_pixel_counter = 0;
function trackPixel(){
	
	//Checking if the arguments are only two (website and item to track)
	if(arguments.length == 1) {
		var intWebsiteId = wgt_WebSiteId;		
		var intItemId = arguments[0];
		if(intItemId == '')return;
		if(!isNaN(intWebsiteId) && !isNaN(intItemId) && intWebsiteId != null && intItemId != null) {
		
			if(intWebsiteId != 0 && intItemId != 0) {				
				var img = document.createElement("img");
				img.style.display = "none";
				img.src = wgt_inj_host + "/seo/site-track/trackingimage2007.php?b2e84=" 
				+ tracking_pixel_counter + "&custID=" + intWebsiteId 
				+ "&page=" + intItemId + "&r=" + r + "&qs=" + qs;
				document.body.appendChild(img);
				document.body.appendChild(img);
			}

		}	//End of the if that checks if the parameters are numbers
	
	}	//End of the if that checks the amount of parameters
	tracking_pixel_counter++;
	
}

// Inject widget cotainer
try{			
	var scriptWidget = document.createElement('div');
	//var scriptWidget = document.createElement('center');
	scriptWidget.setAttribute('id', wgt_inj_anchor);
	//scriptWidget.setAttribute('style', 'width:' + wgt_dimension_width + ';margin-left:auto!important; margin-right:auto!important;');
	var wgt_container =  document.body;	
	if(wgt_inj_container != ''){		
		wgt_new_container =  document.getElementById(wgt_inj_container);
		if(wgt_new_container != null){
			wgt_container = wgt_new_container;
		}
	}	
	var ref_DH = wgt_container.childNodes[0];
	if(wgt_inj_end){
		ref_DH = wgt_container.childNodes[wgt_container.childNodes.length -1];
		document.body.insertAfter(scriptWidget, ref_DH);
	}else{
		document.body.insertBefore(scriptWidget, ref_DH);	
	}	
	var wgt_Title = '<div style="' + wgt__headStyle +  '"> '
		+ ' <a href="javascript:;" onClick="togglePushdown();">'
		+ ' <img id="wgt_butimage" name="wgt_butimage" src="'
		+ wgt_con_openImg +'" style="' 
		+ wgt__btnStyle + '"  border="0" alt="' 
		+ wgt_con_openAlt + '"/></a></div>';
	//wgt_statusback
	var wgt_Content = '<div name="wgt_statusback" id="wgt_statusback" '
		+ ' style="' + wgt__frameStyle + '"'
		+ '> <iframe name="" id="" '
		+ ' src="' + wgt_view + '?WebSiteId=' 
		+ wgt_WebSiteId + '&r=' + r +'&qs=' + qs + '" '
		+ ' height="' + wgt_dimension_height + '" width="' + wgt_dimension_width + '"'
		+ ' style="' + wgt__fixed_frameStyle + '"'
		+ ' scrolling="no" frameborder="0" '
		+ ' marginheight="0" marginwidth="0" '
		+ ' onmouseover="formActive =true;" '
		+ ' onmouseout="formActive =false;" '
		+ ' onfocus="formActiveIE =true;" '
		+ ' onblur="formActiveIE =false;" '		
		+ ' onload="wgt_load_status=true;wait4Response();"> '
		+ ' THERE IS NO IFRAME SUPPORT IN YOUR CLIENT!</iframe>';

	var scriptWidgetDiv = document.createElement('div');
	scriptWidgetDiv.setAttribute('id', wgt_Frame);
	//scriptWidgetDiv.setAttribute('style', 'width:' + wgt_dimension_width + ';margin: 0 auto;');
	scriptWidgetDiv.setAttribute('style', 'width:' + wgt_dimension_width);
	scriptWidgetDiv.innerHTML = wgt_Title + wgt_Content;
	scriptWidget.appendChild(scriptWidgetDiv);
	formActive	= false;
	formActiveIE = false;
	//Fix by Chris Johnson
	//showFirst();
	
	if(!navigator.appName.match("Internet Explorer")){
		//Attach it for all good browsers
		window.addEventListener("load", function(){showFirst();}, false);
	}else{
		//Attach the load event to crappy IE
		window.attachEvent("onload", function(){showFirst();});
	}
	
				
}catch(e){
	alert('Can\'t display slider widget' + e);}

var ns4=(document.layers)? true:false;
var ns6=(document.getElementById)? true:false;
var ie4=(document.all)? true:false;
var ie5=false;
if(ie4){
	if((navigator.userAgent.indexOf('MSIE 5')> 0)||(navigator.userAgent.indexOf('MSIE 6')> 0)){
		ie5=true;
	}
	if(ns6){
		ns6=false;
	}
}


if((ns4)||(ie4)||(ns6)){
	document.onmousemove=mouseMove
	if(ns4)document.captureEvents(Event.MOUSEMOVE)
}

function mouseMove(e){
	var targetObj =  document.getElementById('wgt_statusback')
	var ymin = targetObj.offsetTop -10
	var ymax = ymin + targetObj.clientHeight + 20
	var xmin = targetObj.offsetLeft -10
	var xmax = xmin + targetObj.clientWidth + 20
	var mouseX;
	var mouseY;
	if((ns4)||(ns6)){mouseX=e.pageX;mouseY=e.pageY;}
	if(ie4){mouseX=event.x;mouseY=event.y;}
	if(ie5){mouseX=event.x+self.document.body.scrollLeft;mouseY=event.y+self.document.body.scrollTop;}
	activeMouse = mouseX > xmin && mouseX < xmax && mouseY > (ymin - 40) && mouseY < ymax;
}
