/*
#####################################################################
###	--------------------------------------------------------------------------------------------------------
###  CSMODALS by Rich Cooper
###	--------------------------------------------------------------------------------------------------------
#####################################################################

############################# MAIN FUNCTIONS ###########################
############################# MAIN FUNCTIONS ###########################
############################# MAIN FUNCTIONS ###########################
*/

var cs_siteroot = 'http://standalone.coopa.net';

// ---------------------------------------------------------------------------------------
// ALLOWS QUICK ACCESS TO getElementById
function csgt(name){
	return document.getElementById(name);
}

// ---------------------------------------------------------------------------------------
// CLOSE THE CSMODAL AND RESET THE IFRAME
function csmodal_close(){
	csgt('csmodalcontainer').style.display='none';
	window.top.focus();
	return false;
}

// ---------------------------------------------------------------------------------------
// OPEN THE CSMODAL AND POPULATE THE IFRAME
function csmodal_open(url,title){
	destroy_and_recreate_iframe();
	IFrameObj = csgt('csmodalpaneliframe');
	try
	{
		var IFrameDoc;
		if (IFrameObj.contentDocument) {
			IFrameDoc = IFrameObj.contentDocument; 
		} else if (IFrameObj.contentWindow) {
			IFrameDoc = IFrameObj.contentWindow.document;
		} else if (IFrameObj.document) {
			IFrameDoc = IFrameObj.document;
		}
	}
	catch(err)
	{
		// Soemetimes getting IFrameDoc fails in IE6 due to a race condition
		// (the document object of the newly created iframe isn't yet available)
		// so just retry
		csmodal_open(url,title);
		return false;
	}
	IFrameDoc.location.replace(url); // Replace doesn't add to the browsers history
	csgt('csmodalcontainer').style.display='block';
	return false;
}

// ---------------------------------------------------------------------------------------
// MAKE THE IFRAME
function destroy_and_recreate_iframe()
{
	// Destroy and recrteate iframe to prevent same-origin policy violations,
	// when closing and re-oprning the iframe
	if (csgt('csmodalpaneliframe')) csgt('csmodalpanel').removeChild(csgt('csmodalpaneliframe'));
	var	csmodalpaneliframeif	= document.createElement('iframe'); 	
			csmodalpaneliframeif.setAttribute('id','csmodalpaneliframe'); 
			csgt('csmodalpanel').appendChild(csmodalpaneliframeif);
	csmodal_detectIEandFixHeight();
}

// ---------------------------------------------------------------------------------------
// INJECT THE CSS AND DIV TAGS INTO THE BODY OF THE PAGE
function csmodal_setup(e){
	if  (csmodal_alreadyrunflag) return;
    csmodal_alreadyrunflag=1;

	var ie6 = navigator.appVersion.indexOf("MSIE 6.")!=-1;
	
	var newSS=document.createElement('link');
	newSS.rel  = 'stylesheet';
	newSS.type = 'text/css';
	newSS.href = cs_siteroot+'/styles/csmodal.css';
	document.getElementsByTagName("head")[0].appendChild(newSS);
	
	if (ie6)
	{
		var newSS=document.createElement('link');
		newSS.rel  = 'stylesheet';
		newSS.type = 'text/css';
		newSS.href = cs_siteroot+'/styles/ie6.css';
		document.getElementsByTagName("head")[0].appendChild(newSS);
	}

	var	csmodalcontainerdiv	= document.createElement('div');	
			csmodalcontainerdiv.setAttribute('id','csmodalcontainer'); 
			
	if (ie6)
	{
		csmodalcontainerdiv.style.width  = (document.documentElement.clientWidth - 100) + 'px';
		csmodalcontainerdiv.style.height = (document.documentElement.clientHeight - 100) + 'px';
	}

	var	csmodalbardiv			= document.createElement('div'); 	
			csmodalbardiv.setAttribute('id','csmodalbar'); 
			csmodalbardiv.innerHTML='<p></p><a href="javascript:;" onclick="return csmodal_close();">Close</a>';
			csmodalcontainerdiv.appendChild(csmodalbardiv);

	var	csmodaldiv				= document.createElement('div'); 
			csmodaldiv.setAttribute('id','csmodal'); 
			csmodalcontainerdiv.appendChild(csmodaldiv);
			
	var csmodalpaneldiv			= document.createElement('div'); 	
			csmodalpaneldiv.setAttribute('id','csmodalpanel'); 
			csmodalpaneldiv.innerHTML='';
			csmodalcontainerdiv.appendChild(csmodalpaneldiv);
	
	if (ie6)
	{
		csmodalpaneldiv.style.height = (document.documentElement.clientHeight - 100) + 'px';
	}

	document.getElementsByTagName("body")[0].appendChild(csmodalcontainerdiv);
}

/*
############################# IFRANE SIZE ###########################
############################# IFRANE SIZE ###########################
############################# IFRANE SIZE ###########################
*/

// ---------------------------------------------------------------------------------------
// DETECT IF THE USER IS USING IE6/7 AND IF SO RESIZE IFRAME ACCORDINGLY
function csmodal_detectIEandFixHeight(){

	if((document.all))  {  
		if((navigator.appVersion.indexOf("MSIE 7.")!=-1))  {  
			//alert("ie7");
			csmodal_resizeIframe();
			window.onresize = function(){
				csmodal_resizeIframe();
			}
		}  
	}
	if(navigator.appVersion.indexOf("MSIE 6.")!=-1){  
		// alert("ie6");
		csmodal_resizeIframe();
			window.onresize = function(){
				csmodal_resizeIframe();
			}
	}    
}


// ---------------------------------------------------------------------------------------
// RETURN THE HEIGHT OF THE INSIDE OF THE WINDOW
function csmodal_getBodyHeight(){
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myHeight = document.documentElement.clientHeight;
	} else {
		myHeight = document.body.clientHeight;
	}
	return myHeight;
}

// ---------------------------------------------------------------------------------------
// DO THE IFRAME RESIZE BASED ON BODY HEIGHT
function csmodal_resizeIframe() {
	var height = csmodal_getBodyHeight() - 135;
    document.getElementById('csmodalpaneliframe').style.height = height +"px";
}

/*
############################# DOM READY ###########################
############################# DOM READY ###########################
############################# DOM READY ###########################
*/

var csmodal_alreadyrunflag=0 //flag to indicate whether target function has already been run

// ---------------------------------------------------------------------------------------
// IF DOM IS READY RUN THE csmodal_setup
if (document.addEventListener)
	document.addEventListener("DOMContentLoaded", function(){csmodal_setup();}, false)
else if (document.all && !window.opera){
	document.write('<script type="text/javascript" id="contentloadtag" defer="defer" src="javascript:void(0)"><\/script>')
	var contentloadtag=document.getElementById("contentloadtag")
	contentloadtag.onreadystatechange=function(){
		if (this.readyState=="complete"){
		  csmodal_setup();
		}
	}
}
window.onload=function(){
	setTimeout("if (!csmodal_alreadyrunflag){csmodal_setup()}", 0)
}

