/**
 * @author Ellyx Christian
 */

/**
 * @param Int w: width, require
 * @param Int x: x position
 * @param Int y: y position
 * @param String pic : optional, picture location eg.: ./images/popup/
 * remember don't overwite funciton moobbiePopuHide;
 */
function MoobbiePopup(w,x,y,pic){
	if(!pic)
	this.pictureLocation='images/popup/';
	else
	this.pictureLocation=pic;
	var windsize=this._getWindowSize();
	var srcollPos=this._getScrollPosition();
	this.divLoading;
	this.continer;
	if(!x){
		x= (windsize[0]/2)+ srcollPos[0];
	}
	x=parseInt(x)-(parseInt(w/2));
	if(!y){
		y=(windsize[1]/2)+ srcollPos[1];
	}
	this.w=w;
	this.yPos=y;
	this.xPos=x;
	this.div;
	this.divContent;
	this.popupFooter;
	this.actionCancel='';
	if(!document.getElementById('moobbiePopupDiv')){
		this.div = document.createElement('div');
		this.div.style.position='absolute';
		this.div.id='moobbiePopupDiv';
		
		this.divContent = document.createElement('div');
		this.divContent.id='moobbiePopupDivContent';
		
		var table=document.createElement('table');
		table.width="100%";
		//table.setAttributeNode('cellpadding');
		//table.setAttributeNode('cellspacing');
		//table.setAttributeNode('border');
		//table.setAttribute('cellpadding','0');
		//table.setAttribute('cellspacing','0');
		//table.setAttribute('border',0);
		//table.setAttributeNode("cellpadding");
		table.cellPadding="0";
		table.cellSpacing="0";
		table.border="0";
		var tbody= document.createElement('tbody');
		//create first tr
		var tr1= this._createTr();
		//top left td
		tr1.appendChild(this._createTd('moobbie_popup_tleft',this.pictureLocation+'popup_01.png'));
		//top middle td
		tr1.appendChild(this._createTd('moobbie_popup_mtop',this.pictureLocation+'popup_03.png'));
		//top right td
		tr1.appendChild(this._createTd('moobbie_popup_trigtht',this.pictureLocation+'popup_08.png'));
		tbody.appendChild(tr1);
		//create second tr
		var tr2=this._createTr();
		tr2.appendChild(this._createTd('moobbie_popup_mleft',this.pictureLocation+'popup_10.png'));
		//create div logo
		var dvl=this._createDiv('moobbie_popup_logo',this.pictureLocation+'logo.png');
		//create div title
		var dvt=this._createDiv('moobbie_popup_title',false,'moobbiePopupTitle');
		var td=this._createTd('moobbie_popup_head');
		td.appendChild(dvl);
		td.appendChild(dvt);
		td.setAttribute('valign','middle');
		tr2.appendChild(td);
		tr2.appendChild(this._createTd('moobbie_popup_mright',this.pictureLocation+'popup_09.png'));
		tbody.appendChild(tr2);
		//create 3th tr
		var tr3 = this._createTr();
		tr3.appendChild(this._createTd('moobbie_popup_mleft',this.pictureLocation+'popup_10.png'))
		var td= this._createTd('moobbie_popup_content');
		//creating content continer
		//var div1=this._createDiv('moobbie_popup_frame');
		//this.iframe=document.createElement('iframe');
		//this.iframe.id='moobbiePopupIframe';
		//div1.appendChild(this.divContent);
		//div1.appendChild(this.iframe);
		td.appendChild(this.divContent);
		tr3.appendChild(td);
		tr3.appendChild(this._createTd('moobbie_popup_mright',this.pictureLocation+'popup_09.png'));
		tbody.appendChild(tr3);
		//create 4th tr
		var tr4=this._createTr();
		tr4.appendChild(this._createTd('moobbie_popup_mleft',this.pictureLocation+'popup_10.png'))
		var td=this._createTd('moobbie_popup_footer');
		this.popupFooter=document.createElement('div');
		this.popupFooter.id='moobbiePopupFooter';
		this.popupFooter.style.padding='5px';
		this.popupFooter.style.paddingRight='10px';
		this.popupFooter.align='right';
		//td.innerHTML='&nbsp;';
		td.appendChild(this.popupFooter);
		tr4.appendChild(td);
		tr4.appendChild(this._createTd('moobbie_popup_mright',this.pictureLocation+'popup_09.png'));
		tbody.appendChild(tr4);
		//create last tr
		var tr5 = this._createTr();
		tr5.appendChild(this._createTd('moobbie_popup_bleft',this.pictureLocation+'popup_25.png'));
		tr5.appendChild(this._createTd('moobbie_popup_mbottom',this.pictureLocation+'popup_12.png'));
		tr5.appendChild(this._createTd('moobbie_popup_bright',this.pictureLocation+'popup_11.png'));
		
		tbody.appendChild(tr5);
		table.appendChild(tbody);
		this.div.appendChild(table);
	}else{
		this.div = document.getElementById('moobbiePopupDiv');
		this.divContent = document.getElementById('moobbiePopupDivContent');
		this.popupFooter= document.getElementById('moobbiePopupFooter');
		//this.iframe=document.getElementById('moobbiePopupIframe');
	}
	//this.div.style.height=h+'px';
	this.div.style.width=w+'px';
	this.div.style.top=y+'px';
	this.div.style.left=x+'px';
	this.div.style.zIndex=999;
	document.body.appendChild(this.div);
	this._showLoading();
	this.div.style.display='none';
	this._createCancelButton();
}
MoobbiePopup.prototype.setCancelAction=function(act){
	if(act) this.actionCancel=act;
}
MoobbiePopup.prototype._createCancelButton=function(){
	var btn= document.createElement('button');
	btn.innerHTML='Cancel';
	btn.onclick=function(that){
		return function(){
			eval(that.actionCancel);
			that.hide();
		}
	}(this);
	this.popupFooter.innerHTML='';
	this.popupFooter.appendChild(btn);
}
/**
 * create other button eg.: Save button
 * @param {Object} txt button text
 * @param {Object} act button action
 * @param Boolean  hd hide popup
 * the action will execute and will close the popup if hd=true
 * if hd=false user must be call function moobbiePopuHide in this file from act function
 * the button will add first element popup footer
 */
MoobbiePopup.prototype.setOtherAction=function(txt,act,hd){
	var btn= document.createElement('button');
	btn.innerHTML=txt;
	btn.onclick=function(that){
		return function(){
			if(typeof act == 'function') act();
			if(hd)
			that.hide();
		}
	}(this);
	var btnb4=this.popupFooter.childNodes;
	this.popupFooter.appendChild(btn);
	for(var i=0; btnb4[i]; i++){
		this.popupFooter.appendChild(btnb4[i]);
	}
}
/**
 * Add button to the popup
 * @param {String} txt : button text
 * @param {function} act : callback function
 * @param {Boolean} hd : true if you want to hide popup after execute callback function
 */
MoobbiePopup.prototype.addButton=function(txt,act,hd){
	var btn= document.createElement('button');
	btn.innerHTML=txt;
	btn.onclick=function(that){
		return function(){
			if(act){
				act();
			}
			if(hd)
			that.hide();
		}
	}(this);
	var btnb4=this.popupFooter.childNodes;
	this.popupFooter.appendChild(btn);
	for(var i=0; btnb4[i]; i++){
		this.popupFooter.appendChild(btnb4[i]);
	}	
}
MoobbiePopup.prototype.getContentId=function(){
	return this.divContent.id;
}
/**
 * To assign content
 * @param {String} ct
 */
MoobbiePopup.prototype.setContent=function(ct){
	this.divContent.innerHTML=ct;
}
MoobbiePopup.prototype.setTitle=function(tl){
	document.getElementById('moobbiePopupTitle').innerHTML=tl;
}
MoobbiePopup.prototype.show=function(){
	//get hight
	this.div.style.display='block';
	var h=this.div.offsetHeight ? this.div.offsetHeight : this.div.clientHeight;
	//alert(this.div.offsetHeight);
	var wsize=this._getWindowSize();
	var h1=$(window).height()-80;
	//alert(h+':'+h1);
	if(h>h1){
		h=h1;
		var foterh=this.popupFooter.offsetHeight ? this.popupFooter.offsetHeight : this.popupFooter.clientHeight;
		this.divContent.style.height=(h-20-foterh)+'px';
		this.divContent.style.overflow='auto';
		this.divContent.scrollTop=0;
		//h=h+20;
	}
	this.div.style.top=(parseInt(this.yPos)-(h/2)-20)+'px';
	this.continer.style.display='block';
	if(this._checkBrowser()=='Explorer'){
		this.continer.innerHTML='<iframe class="moobbie_popup_frame_iframe" style="height:'+(h-20)+'px; width:'+(this.w-20)+'px; top:'+(parseInt(this.div.style.top)+10)+'px; left:'+(parseInt(this.div.style.left)+10)+'px;"></iframe>';
	}
	this.divLoading.style.display='none';
}
MoobbiePopup.prototype.hide=function(){
	this.div.style.display='none';
	this.setContent('');
	this.continer.style.display='none';
	this.popupFooter.innerHTML='';
	this.divContent.style.height="auto";
	this.divLoading.style.display='none';
}
/**
 * create div element
 * @param {String} cl className
 * @param {String} pic background picture
 */
MoobbiePopup.prototype._createDiv=function(cl,pic,id){
	var div = document.createElement('div');
	if(cl){
		div.className=cl;
	}
	if(pic){
		if(this._checkBrowser()=='Explorer'){
			div.style.background='none';
			div.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+pic+"',sizingMethod='image')";
		}
	}
	if(id){
		div.id=id;
	}
	return div;
}
MoobbiePopup.prototype._getWindowSizeWithScroll=function(){
	if (window.innerHeight && window.scrollMaxY) {// Firefox
		yWithScroll = window.innerHeight + window.scrollMaxY;
		xWithScroll = window.innerWidth + window.scrollMaxX;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		yWithScroll = document.body.scrollHeight;
		xWithScroll = document.body.scrollWidth;
	} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		yWithScroll = document.body.offsetHeight;
		xWithScroll = document.body.offsetWidth;
  	}
	arrayPageSizeWithScroll = new Array(xWithScroll,yWithScroll);
	//alert( 'The height is ' + yWithScroll + ' and the width is ' + xWithScroll );
	return arrayPageSizeWithScroll;
}
MoobbiePopup.prototype._getScrollPosition=function(){
	var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  var arrayScrollPos= new Array(scrOfX,scrOfY);
  return arrayScrollPos;
}
MoobbiePopup.prototype._getWindowSize=function(){
	var viewportwidth;
	var viewportheight;
	 
	 // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	 
	 if (typeof window.innerWidth != 'undefined')
	 {
	      viewportwidth = window.innerWidth,
	      viewportheight = window.innerHeight
	 }
	 
	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
	
	 else if (typeof document.documentElement != 'undefined'
	     && typeof document.documentElement.clientWidth !=
	     'undefined' && document.documentElement.clientWidth != 0)
	 {
	       viewportwidth = document.documentElement.clientWidth,
	       viewportheight = document.documentElement.clientHeight
	 }
	 
	 // older versions of IE
	 
	 else
	 {
	       viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
	       viewportheight = document.getElementsByTagName('body')[0].clientHeight
	 }
	 return new Array(viewportwidth,viewportheight);
}
MoobbiePopup.prototype._createTr=function(){
	var tr=document.createElement('tr');
	return tr;
}
/**
 * To define picture location
 * @param {String} lc, ie.:image/picture/
 */
MoobbiePopup.prototype.setPictureLoc=function(lc){
	this.pictureLocation=lc;
}
MoobbiePopup.prototype._createTd=function(cl,clpic){
	var td=document.createElement('td');
	if(cl){
		td.className=cl;
	}
	if(clpic){
		//fix css for IE
		if(this._checkBrowser()=='Explorer'){
			td.style.background='none';
			td.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+clpic+"',sizingMethod='scale')";
		}
	}
	return td;
}
MoobbiePopup.prototype._checkBrowser=function(){
	this.BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
this.BrowserDetect.init();
return this.BrowserDetect.browser;
}
MoobbiePopup.prototype._showLoading=function(){
	if(!document.getElementById('moobbiePopupLoading')){
		this.divLoading= document.createElement('div');
		this.divLoading.id='moobbiePopupLoading';
	}else{
		this.divLoading=document.getElementById('moobbiePopupLoading');
	}
var wsize=this._getWindowSize();
	this.divLoading.style.backgroundColor="#eeeeee"; //65AFF8
	this.divLoading.style.border='1px solid #000033';
	//this.divLoading.style.borderWidth='1px';
	//this.divLoading.style.borderColor='#65AFF8';
	//this.divLoading.style.borderStyle='solid';
	this.divLoading.style.padding='5px';
	this.divLoading.innerHTML='<font size="+1">Loading...Please wait</font>';
	this.divLoading.style.height='50px';
	this.divLoading.style.width='400px';
	var h=this.divLoading.offsetHeight ? this.divLoading.offsetHeight : this.divLoading.clientHeight;
	this.divLoading.style.top=(parseInt(this.yPos)-(h/2))+'px';
	this.divLoading.style.left=((parseInt(wsize[0])-400)/2)+'px';
	this.divLoading.style.position='absolute';
	this.divLoading.style.zIndex=998;
	this.divLoading.style.opacity=1;
	this.divLoading.style.filter="alpha(opacity=100)";
	this._createContiner();
	//this.continer.appendChild(this.divLoading);
	document.body.appendChild(this.divLoading);
	this.divLoading.style.display='block';
}
MoobbiePopup.prototype._createContiner=function(){
	var psize= this._getWindowSizeWithScroll();
	if(!document.getElementById('moobbiePopupDivContiner')){
		this.continer = document.createElement('div');
		this.continer.id='moobbiePopupDivContiner';
		this.continer.style.width='100%';
		this.continer.style.height= psize[1]+'px';
	}else{
		this.continer=document.getElementById('moobbiePopupDivContiner');
	}
	this.continer.style.background='#ffffff';
	this.continer.style.opacity=0;
	this.continer.style.filter="alpha(opacity=0)";
	this.continer.style.top='0px';
	this.continer.style.left='0px';
	this.continer.style.zIndex=997;
	this.continer.style.position='absolute';
	this.continer.style.display='block';
	document.body.appendChild(this.continer);
}
function moobbiePopuHide(){
	document.getElementById('moobbiePopupDiv').style.display='none';
	document.getElementById('moobbiePopupDivContiner').style.display='none';
	document.getElementById('moobbiePopupDivContent').innerHTML='';//moobbiePopupFooter
	document.getElementById('moobbiePopupFooter').innerHTML='';
}
