/**
*
*  URL encode / decode
*  http://www.webtoolkit.info/
*
**/
 
var Url = {
 
	// public method for url encoding
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},
 
	// public method for url decoding
	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},
 
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
 
}


/* LOWER NAV
*************************************************************/
function setupLowerNav(){
	var self = this;
	self.timeout = null;
	self.timing = 1000;
	
	self.init = function(){
		$('#lower_nav a').each(function(){
			$(this).unbind('mouseover').mouseover(function(){
				var class_name = $(this).attr('class').replace('active','');
				$(".subnavigation").hide();
				if($('.subnavigation.active').length == 0){
					$(".subnavigation."+class_name).fadeTo( 10, .90);
				}
				$(".subnavigation."+class_name).show();
			});
			$(this).unbind('mouseout').mouseout(function(){
				if(self.timeout != null){
					clearTimeout(self.timeout);
				}
				self.timeout = setTimeout('lowerNavObj.revert()', self.timing);
			});
		});
		$(".subnavigation").mouseover(function(){
			if(self.timeout != null){
				clearTimeout(self.timeout);
			}
		});
		$(".subnavigation").unbind('mouseout').mouseout(function(){
			if(self.timeout != null){
				clearTimeout(self.timeout);
			}
			self.timeout = setTimeout('lowerNavObj.revert()', self.timing);
		});
	}
	
	self.revert = function(){
		if($('.subnavigation.active').length == 0){
			$(".subnavigation").fadeTo( 10, 1);
		}
		$(".subnavigation").hide();
		$(".subnavigation.active").show();
	}
	
	self.init();
}

/* LOCK OVERLAY
*************************************************************/
function Overlay() {
	var self = this;
	self.form_location = '/_lib/lg_form.php';
	self.support_location = '/_lib/support_form.php';
	self.ajax_call = null;
	
	self.init = function(){
		$("a[href$='.flv']").unbind('click').click(function(){
			self.setup_media_overlay($(this).attr('href'));
			return false;
		});
		$(".overlay").unbind('click').click(function(){
			self.setup_media_overlay($(this).attr('href'));
			return false;
		});
		$("#page_feature #feature_call a").unbind('click').click(function(){
			self.setup_gatekeeper($(this).attr('href'));
			return false;
		});
		$("#page_feature #feature_image a").unbind('click').click(function(){
			self.setup_gatekeeper($(this).attr('href'));
			return false;
		});
		$("a.lock_form").unbind('click').click(function(){
			self.setup_gatekeeper($(this).attr('href'));
			return false;
		});
		$(".support_contact").unbind('click').click(function(){
			self.setup_support_request();
			return false;
		});
		$(".pro_services_form").unbind('click').click(function(){
			self.setup_gatekeeper('9dcad198-3a12-11df-a9b2-0030487784b4');
			return false;
		});
	}
	
	self.setup_media_overlay = function(to_link){
		if($("#page_popover").length > 0 ){
			$("#page_popover").dialog("destroy");
			$("#page_popover").remove();
		}
		$('body').append('<div id="page_popover" class="big"></div>');
		
		$("#page_popover").html("<div id='inetco_form' class='big'><p>To view the demo in full screen mode, <br/>click the <img src='/_img/icons/icn_fullscreen.gif' width='19' height='18' border='0' /> button below</p><a href='" + to_link + "'>&nbsp;</a></div>");
		
		$("#page_popover").dialog({
			width: 660,
			moveable: false,
			resizable: false,
			modal: true
		});
		$("#page_popover a").addClass("media {width:620,height:465}");
		$("#page_popover a").media();
		
		$.scrollTo({top:0,left:0},250);
		$(".ui-dialog").addClass('big').css("width","660px").css("top","100px").css("left","100px");
		
		$('.ui-widget-overlay').fadeTo('slow',0.3);
			
		$(".ui-widget-overlay").unbind('click').click(function(){
			self.close_overlay();
			return false;
		});
		$(".ui-dialog-titlebar-close").click(function(){
			self.close_overlay();
			return false;
		});
	}
	
	self.setup_gatekeeper = function(to_link){
		if($("#page_popover").length > 0 ){
			$("#page_popover").dialog("destroy");
			$("#page_popover").remove();
		}
		$('body').append('<div id="page_popover"></div>');
		$.post(self.form_location,'&to_link='+to_link+'&from_link='+window.location,function(data){
			if(data.indexOf("|") != 0){
				$("#page_popover").html(data);
				if($("#inetco_form.big").length > 0){
					$("#page_popover").dialog({
						width: 660,
						moveable: false,
						resizable: false,
						modal: true
					});
				} else {
					$("#page_popover").dialog({
						width: 369,
						moveable: false,
						resizable: false,
						modal: true
					});
				}
				
				$("#inetco_form.big").parent().parent().addClass("big").css("width","660px").css("top","100px").css("left","100px");
				
				$('#inetco_form').validate({	
					messages: {
						first_name: "Please enter your first name",
						last_name: "Please enter your last name",
						login: "Please enter your login",
						password: "Please enter your password",
						title: "Please enter your title",
						company: "Please enter your company",
						telephone: "Please enter your phone number",
						email: "Please enter a valid email address"
					},
					submitHandler: function() {
						self.ajaxSubmit();
					}
				});
				$('.ui-widget-overlay').fadeTo('slow',0.3);
			} else {
				new_location = data.split("|");
				window.location=new_location[1];
			}
		});
			
		$(".ui-widget-overlay").unbind('click').click(function(){
			self.close_overlay();
			return false;
		});
		$(".ui-dialog-titlebar-close").click(function(){
			self.close_overlay();
			return false;
		});
	}
	
	self.ajaxSubmit = function(){
		var content = $("#page_popover form").serializeArray();
		if(self.ajax_call){
			self.ajax_call.abort();
		}
		self.ajax_call = $.post(self.form_location,content,function(data){
			if(data.indexOf("|") != 0){
				$("#page_popover").html(data);
				
				$("#inetco_form.big").parent().parent().addClass("big").css("width","660px").css("top","100px").css("left","100px");
				
				$('#inetco_form').validate({	
					messages: {
						first_name: "Please enter your first name",
						last_name: "Please enter your last name",
						login: "Please enter your login",
						password: "Please enter your password",
						title: "Please enter your title",
						company: "Please enter your company",
						telephone: "Please enter your phone number",
						email: "Please enter a valid email address"
					},
					submitHandler: function() {
						self.ajaxSubmit();
					}
				});
			} else {
				new_location = data.split("|");
				window.location=new_location[1];
			}
		});
	}
	self.close_overlay = function(){
		if($("#page_popover").length > 0 ){
			$("#page_popover").dialog("destroy");
			$("#page_popover").remove();
		}
	}
	
	self.setup_support_request = function(){
		if($("#page_popover").length > 0 ){
			$("#page_popover").dialog("destroy");
			$("#page_popover").remove();
		}
		$('body').append('<div id="page_popover"></div>');
		$.post(self.support_location,'&from_link='+window.location,function(data){
			if(data){
				$("#page_popover").html(data);
				$("#page_popover").dialog({
					width: 369,
					moveable: false,
					resizable: false,
					modal: true
				});
				$('#inetco_form.support').validate({	
					messages: {
						first_name: "Please enter your first name",
						last_name: "Please enter your last name",
						title: "Please enter your title",
						company: "Please enter your company",
						telephone: "Please enter your phone number",
						email: "Please enter a valid email address"
					},
					submitHandler: function() {
						self.ajaxSupportSubmit();
					}
				});
				$('.ui-widget-overlay').fadeTo('slow',0.3);
			
				$(".ui-widget-overlay").unbind('click').click(function(){
					self.close_overlay();
					return false;
				});
				$(".ui-dialog-titlebar-close").click(function(){
					self.close_overlay();
					return false;
				});
			}
		});
	}
	
	self.ajaxSupportSubmit = function(){
		var content = $("#page_popover form").serializeArray();
		if(self.ajax_call){
			self.ajax_call.abort();
		}
		self.ajax_call = $.post(self.support_location,content,function(data){
			if(data){
				$("#page_popover").html(data);
				$('#inetco_form').validate({	
					messages: {
						first_name: "Please enter your first name",
						last_name: "Please enter your last name",
						title: "Please enter your title",
						company: "Please enter your company",
						telephone: "Please enter your phone number",
						email: "Please enter a valid email address"
					},
					submitHandler: function() {
						self.ajaxSubmit();
					}
				});
			}
		});
	}
	
	self.init();
}

/* CONTENT UPDATER
*************************************************************/
function ContentUpdater(){
	var _self = this;
	_self.page_height = 0;
	_self.left_column_offset = 306;
	_self.td_height = 0;
	_self.row_count = 0;
	
	self.init = function(){
		if($("table").length > 0){
			self.setup_table();
		}
		_self.page_height = $('#content').height() +(parseInt($('#content').css('margin-top')) * 2);
		_self.page_height = (_self.page_height > 600) ? _self.page_height : 620;
		_self.left_column_offset = $("#lower_left").offset().top;
		if($("#home").length > 0){
			//$("#lower_left").css('height',(parseInt(_self.page_height)-_self.left_column_offset)+"px");
		}
	}
	
	self.setup_table = function(){
		$("table tr").each(function(){
			var text = $(this).children('td:first').html();
			_self.td_height = 0;
			if(_self.row_count % 2 == 0){
				$(this).children('td:first').addClass('tbl_head');
			} else {
				$(this).children('td:first').addClass('tbl_headb');
			}
			$(this).children('td').each(function(){
				if(parseInt($(this).height()) > _self.td_height){
					_self.td_height = parseInt($(this).height())+10;
				}
			});
			$(this).children('td').css('height',_self.td_height);
			$(this).children('td:first').html("<span class='tbl_head_cnt' style='height:"+(_self.td_height+3)+"px;'>" + text + "</span>");
			
			var description = $(this).children('td:eq(1)').html();
			$(this).children('td:eq(1)').html("<span class='tbl_crn_top' style='height:"+(_self.td_height+12)+"px;'><span class='tbl_crn_bottom'>"+description+"</span></span>")
			_self.row_count++;
		});
	}
	
	self.init();
}

/* MEDIA PLAYER DEFAULTS
*************************************************************/
$.fn.media.defaults.flvPlayer= '/_lib/player.swf';
$.fn.media.defaults.flashvars={overstretch: 1, controlbar: 'over',autostart: 'true',screencolor:'FFFFFF',backcolor:'FFFFFF'};
$.fn.media.defaults.params={allowfullscreen: 'true',wmode:'transparent'};

/* DOCUMENT READY FUNCTION
*************************************************************/
$(document).ready(function(){
	//lowerNavObj = new setupLowerNav();
	overlayObj = new Overlay();
	$('.media').media();
	
	$('#left_column input.text').each(function(){
		$(this).focus(function(){
			$(this).val('');
			$(this).unbind('focus');
		});
	});
});