var theme		     = 'default';
var themePath		 = 'themes';
var gatewayURL	     = 'gateway.php'
var pluginPath	     = 'js/voxoxplugins/voxox.'
var menuPageSize     = 16; // items per page
var menuColumns      = 4; // number of columns per page (not used)
var menuMouseEffects = true;
var extclicked       = false;
var myphones 		 = new Array();
(function(){
	var voxox = window.voxox = function(){};
	voxox.alerted = [];
	voxox.loadCountryXml = function() {
		$.get('flags.xml',function(data) {
			voxox.countryxml = data;
		//	if (voxox.userid && !voxox.userdata.hasValidated) {
				voxox.buildCountrySelect($("#smsccdropdown"));
				voxox.buildCountrySelect($("#smsresendccdropdown"));
		//	}
			voxox.buildCountrySelect($("#callback_connectme_select_sms")); 
            voxox.buildCountrySelect($("#callback_reachme_select"));
			voxox.buildCountrySelect($("#callback_connectme_select"));
		},'xml');
	};
	voxox.showPage = function(to, from) {
		if (!$("#"+to).length) $("<div>").attr("id",to).prependTo("body"); // create div if it doesn't already exist
		var menuItem = null;
		$("#"+to).hide();
		$.each(menuItems,function(i,val) { // get the menu item data
			if (val.id == to) {
				menuItem = val;
				return false;
			}
		});
		
		// build bread crumbs
		/*
		if ((to != 'home') && !$("#"+to+" > .buttonBar").length) { // check to see if we need a button bar
			$("<div>").addClass('buttonBar').append($("<div>").addClass('breadcrumb').text("Main").click(function(){ voxox.showPage("home",to); })).prependTo("#"+to);
			if(menuItem)
				$("#"+to+" > .buttonBar").append($("<div>").addClass('breadcrumb').text(menuItem.name))
		}
		*/
		//if ((to != 'home') && !$("#"+to+" > .titleBar").length && menuItem) // check to see if we need a title bar
		//	$("<div>").addClass('titleBar').addClass(to+"Title").text(menuItem.name).prependTo("#"+to);
		
		if (!voxox["build"+to]) // dynamically load the plugin if needed and call the build function to build the page
			$.getScript(pluginPath+to+".js", function(){ if (voxox["build"+to]) voxox["build"+to](); });
		else
			voxox["build"+to]();
			
		$("#"+to).css('display','normal');
		
		$.each(dialogs,function(i,val) {
			if (val.id == to) {
				voxox.alertOnce(val.id,val.title,val.content);
				return false;
			}
		})
		
		$("#"+to).show("slow"); // show the newly built div
		(from && from != to) ? $("#"+from).hide("slow") : $("#home").hide("slow"); // hide what is there now
		if (from == 'voicefax') voxox.getVmCount();
	};
	voxox.goHome = function() {
		$("body > div").hide();
		$("#home").show();
	};
	voxox.preloadImages = function () {
		for(var i = 0; i<arguments.length; i++)
			$("<img>").attr("src", arguments[i]);
	};
	voxox.buildCountrySelect = function (select) {
		var addOption = function (index, item) {
			$(select).append($("<option>").attr("value",$(item).find("countrycode").text()).text(" "+$(item).find("countryname").text()));//.prepend($("<img>").attr("src",$(item).find("icon").text())));
		}
		$(voxox.countryxml).find('flag').each(function(i,item) {addOption(i,item)});
	};
	voxox.showDialog = function () {
		$('#listDialog').jqm(
			{overlay:80,modal:true,onShow:function(h) {h.w.hide(); h.w.css("visibility","visible"); h.w.fadeIn("fast");},onHide:function(h) {h.w.fadeOut("fast");h.w.css("visibility","hidden");h.w.show();if(h.o)h.o.fadeOut("slow");}}
		).draggable(
			{handle:".dialog-title",start:function(){$(".dialog-content").css("opacity",".8")},stop:function(){$(".dialog-content").css("opacity","1")}}
		);
		$('#listDialog').jqmShow();
	};
	voxox.setUser = function() {
		if ($("#login input:first").val()) {
			voxox.username = $("#login input:first").val();
			voxox.password = $("#login input:last").val();
		} else {
			voxox.username = $(document).getUrlParam("login");
			voxox.password = $(document).getUrlParam("password");
		}
	};
	voxox.login = function (success, fail) {
		voxox.setUser();
		voxox.callGateway("login","checkLogin",[voxox.username,voxox.password,voxox.version],function(data) {
			if (data.success) {
				userData = data.result[0];
				voxox.userid = userData.i_account;
                voxox.voxoxid = userData.voxox_id;
				voxox.userdata = userData;
				voxox.encryptedPassword = userData.key;
				voxox.invalidlogin = false;
				
				voxox.getVmCount();
				setInterval ( function() {voxox.getVmCount()}, 900000 );
				
				if(success) success();
			} else {
				voxox.alert("loginerror", "Error", data.result);
				$("#login").show();
				if(fail) fail();
			}
		});
	};
	voxox.directlogin = function (username, password) {
		voxox.username = $("#login input:first").val(username);
		voxox.password = $("#login input:last").val(password);
		voxox.login(function(){voxox.home.show()});
	}
	voxox.getVmCount = function() {
		voxox.callGateway("um","getVmCount",[voxox.userdata.did],function(data) {
			if (data.success) {
				vmcount = data.result[0];
				$(".vmBadge").remove();
				
				if (vmcount && vmcount != "0") {
					voxox.userdata.vmcount = vmcount;
					var className = 'vmBadge';
					switch(vmcount.length) {
						case 1:
						case 2:
							className += ' vmBadge2'; break;
						case 3:
							className += ' vmBadge3'; break;
						case 4:
							className += ' vmBadge4'; break;
						case 5:
							className += ' vmBadge5'; break;
					}
					$(".voicefax").append('<span class="'+className+'">'+voxox.userdata.vmcount+'</span>');
				}
			}
		}, true);
	};
	voxox.getMyPhones = function() {
		voxox.callGateway("my_phones","getMyPhones",[voxox.username,voxox.password],function(data) {
			if(data.result.length > 0) {
				$(data.result).each(function(i, val) {
					var num   = this.phoneNumber;
						num   = num.replace(/\+/g,"").replace(/\-/g,"");
					var isdup = jQuery.inArray(num, myphones);
					if(isdup < 0 && num.length >= 10) { myphones.push(num); }
				});
				initComboBox();
			}
		});
	};
	voxox.callGateway = function (module, method, params, callback, hideLoading, keeploading) {
		var f;
		if (!hideLoading){
			$("#loading2").fadeIn("fast");
			if (keeploading)
				f = function(data) {callback(data);}
			else
				f = function(data) {$("#loading2").fadeOut("fast"); callback(data);}
		} else
			f = function(data) {callback(data);}
		$.post(gatewayURL,{"module":module,"method":method,"params[]":params,"login":voxox.username,"password":voxox.password},f,"json");
	};
	voxox.alert = function (key, title, message, options, callback) {
		$('#alertDialog').jqm(
			{overlay:80,modal:true,onShow:function(h) {h.w.fadeIn("fast");},onHide:function(h) {h.w.fadeOut("fast");if(h.o)h.o.fadeOut("slow"); if (callback) callback();}}
		).draggable();
		$('#alertDialog').jqmShow();
		$('#alertDialog > .alert-content > span').html(message);
		$('#alertDialog > .alert-title').text(title);
		var ch = $('#alertDialog > .alert-content').height();
		var sh = $('#alertDialog > .alert-content > span').height();
		$('#alertDialog > .alert-content > span').css("margin-top",Math.round((ch-sh)/2));
		if (options && options.align && options.align == "center")
			$('#alertDialog > .alert-content').css("text-align","center");
		else
			$('#alertDialog > .alert-content').css("text-align","justify");
	};
	voxox.alertOnce = function (key, title, message, options) {
		if ($.inArray(key, voxox.alerted) == -1 && !$.cookie(key)){
			voxox.alerted.push(key);
			$.cookie(key,"true");
			voxox.alert(key, title, message, options);
		} else
			return false;
	};
	voxox.alertSwitchPlan = function (key, title, message, options, callback) {
		$('#alertSwitchPlan').jqm(
			{overlay:80,modal:true,onShow:function(h) {h.w.fadeIn("fast");},onHide:function(h) {h.w.fadeOut("fast");if(h.o)h.o.fadeOut("slow"); if (callback) callback();}}
		).draggable();
		$('#alertSwitchPlan').jqmShow();
		$('#alertSwitchPlan > .alert-content > span').html(message);
		$('#alertSwitchPlan > .alert-title').text(title);
		var ch = $('#alertSwitchPlan > .alert-content').height();
		var sh = $('#alertSwitchPlan > .alert-content > span').height();
		$('#alertSwitchPlan > .alert-content > span').css("margin-top",Math.round((ch-sh)/2));
		if (options && options.align && options.align == "center")
			$('#alertSwitchPlan > .alert-content').css("text-align","center");
		else
			$('#alertSwitchPlan > .alert-content').css("text-align","justify");
			
	};
	voxox.alertCancelPlan = function (key, title, message, options, callback) {
		$('#alertCancelPlan').jqm(
			{overlay:80,modal:true,onShow:function(h) {h.w.fadeIn("fast");},onHide:function(h) {h.w.fadeOut("fast");if(h.o)h.o.fadeOut("slow"); if (callback) callback();}}
		).draggable();
		$('#alertCancelPlan').jqmShow();
		$('#alertCancelPlan > .alert-content > span').html(message);
		$('#alertCancelPlan > .alert-title').text(title);
		var ch = $('#alertCancelPlan > .alert-content').height();
		var sh = $('#alertCancelPlan > .alert-content > span').height();
		$('#alertCancelPlan > .alert-content > span').css("margin-top",Math.round((ch-sh)/2));
		if (options && options.align && options.align == "center")
			$('#alertCancelPlan > .alert-content').css("text-align","center");
		else
			$('#alertCancelPlan > .alert-content').css("text-align","justify");
			
		
	};
	voxox.alertUpgrade = function (key, title, message, options, callback) {
		$('#alertUpgrade').jqm(
			{overlay:80,modal:true,onShow:function(h) {h.w.fadeIn("fast");},onHide:function(h) {h.w.fadeOut("fast");if(h.o)h.o.fadeOut("slow"); if (callback) callback();}}
		).draggable();
		$('#alertUpgrade').jqmShow();
		$('#alertUpgrade > .alert-content > span').html(message);
		$('#alertUpgrade > .alert-title').text(title);
		var ch = $('#alertUpgrade > .alert-content').height();
		var sh = $('#alertUpgrade > .alert-content > span').height();
		$('#alertUpgrade > .alert-content > span').css("margin-top",Math.round((ch-sh)/2));
		if (options && options.align && options.align == "center")
			$('#alertUpgrade > .alert-content').css("text-align","center");
		else
			$('#alertUpgrade > .alert-content').css("text-align","justify");
			
		
	};
	voxox.dropdown = function(){};
	voxox.dropdown.month = function(id) {
		$("#"+id).append(
			"<option value='01'>01 - JAN</option>").append(
			"<option value='02'>02 - FEB</option>").append(
			"<option value='03'>03 - MAR</option>").append(
			"<option value='04'>04 - APR</option>").append(
			"<option value='05'>05 - MAY</option>").append(
			"<option value='06'>06 - JUN</option>").append(
			"<option value='07'>07 - JUL</option>").append(
			"<option value='08'>08 - AUG</option>").append(
			"<option value='09'>09 - SEP</option>").append(
			"<option value='10'>10 - OCT</option>").append(
			"<option value='11'>11 - NOV</option>").append(
			"<option value='12'>12 - DEC</option>");
	};
	voxox.dropdown.year = function(id) {
		var dd = $("#"+id);
		var d = new Date();
		var year = d.getFullYear();
		for (var i = year; i < year + 20; i++)
			dd.append($("<option>").val(i).text(i));
	};
	voxox.dropdown.country = function(id,value) {
		var c = $("#"+id);
		if (!voxox.dropdown.country.dd) {
			c.html('');
			voxox.callGateway("data_getter","getCountries",[],function(data) {
				if (data.success) {
					$(data.result).each(function(i,val) {
						c.append($("<option>").attr("value",val.Id).text(val.Name));
					});
					voxox.dropdown.country.dd = c.clone();
					if (value) c.val(value);
				}
			},true);
			return;
		}
		c.html(voxox.dropdown.country.dd.html());
		if (value) c.val(value);
	};
	voxox.dropdown.state = function(id,country,value) {
		var s = $("#"+id);
		s.html('');
		s.append($("<option>").attr("value","").text("Select State"));
		voxox.callGateway("data_getter","getCountrySubdivision",[country],function(data) {
			if (data.success) {
				$(data.result).each(function(i,val) {
					s.append($("<option>").attr("value",val.Id).text(val.Name));
				});
				if (value) s.val(value);
			}
		},true);
		return;
	};
	voxox.showDeepPage = function (pages, from) {
		var timer = 0;
		var previouspage = from;
		$(pages).each(function (i,val) {
			if (i == 0)
				voxox.showPage(val); // should figure out if we need to include from
			else
				// this should be hard coded to "myaccount" and we should offer from support
				setTimeout(function(){voxox.myaccount.showPage(val)},timer);
			previouspage = val;
			timer += 300;
		});
	}
	voxox.init = function() {
		//$("#home").load(function() { $("#home").fadeIn("slow"); $("#loading").fadeOut("fast"); })
		voxox.buildhome();
		voxox.loadCountryXml();
		voxox.getMyPhones();
		$("#home").disableTextSelect();
		$("#alertDialog").disableTextSelect();
	};
})();
