/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;

//loading popup with jQuery magic!

//functions for iframe
function gerUrlParamByName(_url,_name){
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+_name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	//var results = regex.exec( window.location.href );
	var results = regex.exec(_url);
	
	if( results == null )
		return '';
	else
		return results[1];
}
function stripURLparams( url ){	
	var _url  = url;
	var _qsStart = _url.indexOf('?');
	
	if(_qsStart >0 ){
		return _url.substr(0,_qsStart);
	}else{
		return url;	
	}
}
//END:functions for iframe.src
function loadPopup(_type,_cid,_pcid,_img,_coid,_IGID){
	//loads popup only if it is disabled
	var _iFrame 	= document.getElementById('popUpiFrame');
	var _src 		= stripURLparams(_iFrame.src)+'?type='+_type+'&';
	
	if(_cid > 0){
		_src = _src+'cid='+_cid+'&';
	}
	if(_pcid > 0){
		_src = _src+'pcid='+_pcid+'&';
	}
	if(_img != undefined){
		if(_img.length > 0){
			_src = _src+'img='+_img+'&';
		}
	}
	if(_IGID > 0){
		_src = _src+'IGID='+_IGID+'&';
	}

	if(_coid > 0){
		_src = _src+'coid='+_coid+'&';
	}

	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#popUpiFrame").fadeIn("slow");
		popupStatus = 1;
	}
	
	//replace the iframe src with the iframe src+the new url params:type,customerid,postcustomerid
	_iFrame.src = _src;
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#popUpiFrame").fadeOut("slow");
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popUpiFrame").height();
	var popupWidth = $("#popUpiFrame").width();
	var scrollPosX= (document.all)?document.body.scrollLeft:window.pageXOffset;
	var scrollPosY= (document.all)?document.body.scrollTop:window.pageYOffset;
	
	//centering
	$("#popUpiFrame").css({
		"position": "absolute",
		"top": ((windowHeight-popupHeight)/2) + scrollPosY,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	$("#backgroundPopup").css({
		"height": windowHeight
	});
	
}

//manual call
function popUpWindow(_type,_cid,_pcid,_img,_coid,_IGID){
	centerPopup();
	loadPopup(_type,_cid,_pcid,_img,_coid,_IGID);
}


//resize iframe
function getElement1(aID){
	return (document.getElementById) ?
		document.getElementById(aID) : document.all[aID];
}
function getIFrameDocument1(aID){ 
	var rv = null; 
	var frame=getElement1(aID);
	// if contentDocument exists, W3C compliant (e.g. Mozilla) 

	if (frame.contentDocument)
		rv = frame.contentDocument;
	else // bad IE  ;)
		rv = document.frames[aID].document;
	return rv;}
function adjustPopUp(){
	var frame = getElement1("popUpiFrame");
	var frameDoc = getIFrameDocument1("popUpiFrame");
	//frame.height = frameDoc.body.offsetHeight;
	frame.height = document.getElementById('popUpiFrame').contentWindow.document.body.scrollHeight+20;
	//alert(frame.height);
}
//resize

/*//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){	
	//LOADING POPUP
	//Click the button event!
	$("#cmdLoadSMS").click(function(){
		//centering with css
		centerPopup();
		//load popup
		loadPopup();
	});
				
	//CLOSING POPUP
	//Click the x event!
	//NOTE: were doing this manually now
	//$("#popUpiFrameClose").click(function(){
//		disablePopup();
//	});
	//Click out event!
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});

});*/
