// 
 
window.onload = updateSearchField;
window.onload= populateValues;

var isIE6 = /msie|MSIE 6/.test(navigator.userAgent);
var MASKIMG = "/wp-content/themes/inove/img/50percent.png";
var GLOBALPOPID = "";

window.onresize= updatePopPos;
var fvArr = new Array();

/*********           SEARCH 		***************/
function updateSearchField(){
	var ref = document.getElementById("searchform");
	if(!ref){ return;}
	 if(ref.elements['s'].value == ""){
	 	ref.elements['s'].value = "Search";
	 }	
}
function  clearSearchField(){
	var ref = document.getElementById("searchform");
	if(!ref){ return;}
	 if(ref.elements['s'].value == "Search"){
	 	ref.elements['s'].value = "";
	 }
}
 
function doNothing(){ }

/*********           POPWINDOWS		***************/
/* IN CASE IE 6 IS USED, USE WINDOW POP */
function launchPopUpWindow(myOb){
	var mywindow = window.open (myOb.url+"?iframe=true",
	"mywindow","location=1,status=1,scrollbars=0,  width="+myOb.arguments.width+",height="+myOb.arguments.height);
	mywindow.moveTo(110,110);
 	mywindow.focus();
 }

/*********           in window pop - BUILD 		***************/

function launchWin(id){
	if(arguments.length>1){
		var myOb = parseQsArgs(arguments[1])
		
		if(isIE6){
			launchPopUpWindow(myOb);
			return;
		}
		
		var ref = document.getElementById(arguments[0]);

		if(!ref){ alert("No Ref for "+id); return false; }

		registerPop(arguments[0])

////////////////////////////////////STYLE		
		ref.style.width = myOb.arguments.width + 'px';
		ref.style.height = myOb.arguments.height + 'px'; 

		if(myOb.arguments && myOb.arguments.fixed == "true"){
		
			 var xpos = 10, ypos = 10;
			 var myWidth = 100, myHeight =100;
			 if( typeof( window.innerWidth ) == 'number' ) {
			    //Non-IE
			    myWidth = window.innerWidth;
			    myHeight = window.innerHeight;
			  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			    //IE 6+ in 'standards compliant mode'
			    myWidth = document.documentElement.clientWidth;
			    myHeight = document.documentElement.clientHeight;
			  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			    //IE 4 compatible
			    myWidth = document.body.clientWidth;
			    myHeight = document.body.clientHeight;
			  }
		
			var xpos = (myWidth/2) - (myOb.arguments.width/2);
			var ypos = (myHeight/2) - (myOb.arguments.height/2);
		
		//	alert("GOT FIXED POP! \n window width=" + myWidth+"\n pop posx = "+xpos )
		//	alert(" window height=" + myHeight+"\n pop posy = "+ypos )
			
			if(ypos <10 ){ ypos = 10; }
			if(xpos <10 ){ xpos = 10; } 

			ref.style.position = "fixed"; 
			ref.style.left = xpos+"px";
			ref.style.top = ypos+"px";
		}else{
				ref.style.position = 'absolute';
				ref.style.top = myOb.arguments.top + 'px'; 
				ref.style.left = myOb.arguments.left + 'px'; 
		}
		
		ref.style.border = '5px #336699 solid';
		ref.style.backgroundColor = '#f8f6f3';
		ref.style.overflow = 'hidden';

		if(myOb.arguments.mask=="true"){
			addMask()
			ref.style.zIndex = "100";	
		}

///////////////////////////////////CLEANUP
		if(ref.childNodes.length==4){ 
			ref.removeChild(ref.childNodes[3]); 
		} 
		var contents = document.createElement('iframe');
		contents.setAttribute('id', 'iframe');
		 
		contents.src= myOb.url+"?iframe=true";
		contents.scrolling = 'no';
		contents.frameBorder = '0';
		//contents.style.border = '2px black dotted';
		
//////////////////////////////////STYLE		
		contents.style.width = (myOb.arguments.width) + 'px';
		contents.style.height = (myOb.arguments.height) + 'px'; 
		contents.style.position = 'relative';
		contents.style.top = '0px';
		contents.style.left = '0px';
		contents.style.backgroundColor = '#f8f6f3';

/////////////////////////////////ADD		 
		ref.appendChild(contents);

////////////////////////////////ANIMATE
		appear(id);

	}else{
		///////////////////not really in use
		var ref = document.getElementById(id);
		if(!ref){ alert("missing ref:"+id); return false; }
	 
		ref.style.top = '200px';
		ref.style.left = '300px';
		appear(id);
	} 
}

function updatePopPos(){
	if(GLOBALPOPID == ""){  return false;} 
	if(document.getElementById(GLOBALPOPID)){
 		 var xpos = 10, ypos = 10;
		 var myWidth = 100, myHeight =100;
		 if( typeof( window.innerWidth ) == 'number' ) {
		    //Non-IE
		    myWidth = window.innerWidth;
		    myHeight = window.innerHeight;
		  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		    //IE 6+ in 'standards compliant mode'
		    myWidth = document.documentElement.clientWidth;
		    myHeight = document.documentElement.clientHeight;
		  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		    //IE 4 compatible
		    myWidth = document.body.clientWidth;
		    myHeight = document.body.clientHeight;
		  }
		var xpos = (myWidth/2) - (document.getElementById(GLOBALPOPID).style.width.replace("px","")/2);
		var ypos = (myHeight/2) - (document.getElementById(GLOBALPOPID).style.height.replace("px","")/2);
		
		//alert("myWidth = "+myWidth +"\n myHeight = " + myHeight)
		//alert("xpos = "+xpos)
		document.getElementById(GLOBALPOPID).style.left = xpos+"px";
	}

}

function registerPop(arg){
	if(arg){
		GLOBALPOPID = arg;
	}
}

/*********           in window pop - REM MASK AND LOADED PAGE	***************/
function remContent(){
	if(document.getElementById('iframe')){
		document.getElementById('iframe').src = "";
	}
}
function remMask(){
	//alert("removing mask")
	if(document.getElementById('mask')){
		document.getElementById('mask').style.display = 'none'; 
	}
	remContent();
}
/*********           BUILD A MASK		***************/
function addMask(){
	if(!document.getElementById('mask') ){
		//alert("adding mask")
		var mask = document.createElement('mask');
		mask.setAttribute('id', 'mask'); 
		mask.style.height = "100%";
		mask.style.width = "100%";
		mask.style.position = "fixed";
		mask.style.top = "10px";
		mask.style.left = "10px";
		mask.style.backgroundImage = "url("+MASKIMG+")"; //backgroundColor = "#666666";
		mask.style.zIndex = "99";
		document.body.appendChild(mask);
	}
		
	document.getElementById('mask').style.display = 'block'; 
	
}

/*
function addScrollie(elname){
	window.addEvent('scroll', function(){ $(elname).setStyle('top', Window.getScrollTop()+'px'); });
}
*/

/*********           AJAXY FORM SUBMISSION SUBSCRIBE AND UNSUBSCRIBE		***************/
function doFormSub(f){
	//alert(f.action)
	//alert("posting to "+f.action);///PHPWrapperSamples-1.2.3/Campaign.Create.php")
	xmlhttpPost(f.action,f)
}

function xmlhttpPost(strURL, f) {
 
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }  
   
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
    	//alert("self.xmlHttpReq.readyState "+self.xmlHttpReq.readyState)
    
        if (self.xmlHttpReq.readyState == 4) {
            updatepage(self.xmlHttpReq.responseText);
        }
    }
    self.xmlHttpReq.send(getquerystring(f));
}

function getquerystring(f) {
    var qstr = "";
    for(var i=0; i<f.elements.length; i++){
    qstr += escape(f.elements[i].name)+"="+escape(f.elements[i].value)+"&";
    }
    //alert(qstr)
    return qstr;
}

/*********           PROCESS RESULT FOR AJAXY FORM SUB		***************/
function updatepage(str){
	var ref = document.getElementById("sfresult");
    if(!ref){
    	alert('no ref'+id); return false; 
    	}
    ref.innerHTML = str;
} 


/*********          SHOW HIDE WINDOWS		***************/
function setOpacity(domId, val) {
	obj = document.getElementById(domId);
	obj.style.MozOpacity = val;
	obj.style.opacity = val/10;
	obj.style.filter = 'alpha(opacity=' + val*10 + ')';

};

function fade(domId){
	obj = document.getElementById(domId); //Get the Element
	
	 if(obj.style.display == "none") return false; //Return false if the element is already hidden
	//////////////////

	remMask()

	/////////////////
	 var alpha = 10; //Set the initial value of alpha to 10 (Opaque)
	 function f(){ //Internal function
	
	  alpha--; //Decrement the alpha value
	  setOpacity(domId, alpha); //Set the opacity of our element to the specified alpha
	
	  if(alpha > -1){ //If alpha is still bigger than -1 then..
	   setTimeout(f, 50); //..then call the function again after 100 milliseconds
	
	  }else{ //otherwise..
	   obj.style.display = 'none'; //..otherwise now that we cant see the element anyways, hide it
	
	  }
}
setTimeout(f, 100); //This is where we call the f() function for the first time

};
function appear(domId){
	obj = document.getElementById(domId); //Get the element
	 
	if(obj.style.display != "none") return false; //Return if it is already being displayed 
 	//ADDED
	setOpacity(domId, 0);
	
	obj.style.display = ''; //Un-hide the object before its animation
 

	 var alpha = 0; //Set the initial value of alpha to 0 (invisible)
	
		 function a(){ //Internal function
			  alpha++; //Increment alpha
			 //alert("got "+alpha)
			  setOpacity(domId, alpha); //Set the opacity of our element to the specified alpha
			  if(alpha < 11)setTimeout(a, 70);
			
			/*Till alpha is 10, keep calling the
			a() function after 100 milliseconds */
		}
	setTimeout(a, 100); //This is where we call the a() function for the first time
};



function parseQsArgs(qs){
	var myOb = new popObject()
	//sample:: http://www.sharpe-partners.com/Schwan/Flash/CCWishStar/star_portfolioVersion_728.html?keepThis=true&TB_iframe=true&height=750&width=768
	myOb.base = qs;
	myOb.url = qs.split("?")[0];
	myOb.arglist = qs.split("?")[1];
	myOb.pairs = myOb.arglist.split("&");
	myOb.arguments = makeArguments(myOb.pairs)

	//alert("top ="+myOb.arguments['top'])
	return myOb; 
}

function makeArguments(pairs){
	var ar = new Array();
	for(var i=0;i<pairs.length; i++)
	{
		var n= pairs[i].split("=")[0]
		var v= pairs[i].split("=")[1]
		ar[n] = v;
	}
	return ar;
}

function popObject(){  }

function registerFormValueUpdates(valuename,formname,elementname){
	//alert("in reg fvu");
	fvArr[fvArr.length] = new Array(valuename,formname,elementname);
}
function populateValues(){
	//alert("in popV "+fvArr.length)
	for(var i=0; i<fvArr.length; i++){
		populateValueFromUrl(fvArr[i][0],fvArr[i][1],fvArr[i][2] );
	}
}
function populateValueFromUrl(valuename,formname,elementname){
	//alert("in popVfU")
	if(!valuename){return false;}
	// get page and domain information from URL
	var urlparts = document.URL.split('?');
	var urlArr = urlparts[0].split('/');
	var qsArr = urlparts[1].split('&');
	var pairs = new Array();
	for(var i=0;i<qsArr.length;i++){
		var p= qsArr[i].split('=');
		pairs[ Url.decode(p[0]) ] = Url.decode(p[1]);
	}
//	alert("qsArr = "+qsArr+" \n valuename="+valuename+"\n formname= "+formname+"\n elementname="+elementname)
	if(pairs[valuename]){
		document.forms[formname].elements[elementname].value = pairs[valuename];
	}
//	alert("form: "+ document.forms[formname].elements[elementname])
}
