(function($) {
$(document).ready(function(){
	$("#twitter").mouseenter(function() {
		$('#twitter-feeds').show();
		return true;
	}).mouseleave(function() {
		$('#twitter-feeds').slideUp(150);
		return true;
	});
	$("ul.nav").superfish({
		hoverClass	: "sfHover",
		delay		: 800,
		animation   : { opacity:.8},
		speed		: "fast",
		oldJquery	: false, /* set to true if using jQuery version below 1.2 */
		disableHI	: false, /* set to true to disable hoverIntent detection */
		onInit		: function(){},
		onBeforeShow: function(){},
		onShow		: function(){},
		onHide		: function(){}
	});
	
	$('#cp_login_btn').click(function() {
		display_cp_login();
		return false;
		
	});
	
	
});

$(window).load(function() {
	$('#esprit_form :submit').bind('click',function() {
		submit_cp_login();
		return true;
	});
});

function display_cp_login() {
	$('#cp_login').fadeIn('fast');
	var cp_val = null;
	var __cp_timer;
	var cp_def = {username: 'demosite',
	              domain  : 'example.com',
				  password: 'demosite'};
	$('#esprit_form > input').focus(function () {
		cp_val  = $(this).val();
		cp_name = $(this).attr('name');
		if (cp_def[cp_name] == cp_val) {
			cp_val = $(this).val();
			$(this).val("");
		}
	}).click(function () { $(this).unbind(); });

	$('#cp_login').mouseleave(function() {
		var $t = $(this);
		__cp_timer = setTimeout(function() { $t.fadeOut('fast'); }, 750);
		
	}).mouseenter(function() {
		clearTimeout(__cp_timer);
	}).click(function(e) {
		if (e.target.id == 'cp-login-close' || 
			e.target.id == 'cp-submit') 
		{
			$(this).fadeOut('fast');
		}
		$(this).unbind('mouseleave mouseenter');
		if (e.target.type == 'input') {
			return true;
		} 
		return false;
	});	
}

function submit_cp_login() {
	$.modal($('<div class="clear">&nbsp;</div><span id="loading">Logging In</span><div class="clear">&nbsp;</div>'),
                {
					close: true,
					full: false,
					closeTitle: 'close',
					title: 'control panel login',
					onShow: function(){
						$('#loading').css('left', ($('#modalContainer').width() - $('#loading').outerWidth()) / 2);
					}
		});
		var formdata = $('#esprit_form').serializeArray();
		$.post('/pages/ajax_engine?engine=esprit_login',formdata, function (data) {
			if (!data.status) {
				$('#modalMsg').html("<div id='err_container' align='left'><strong>There appears to have been some errors:</strong></div>");
				var el = $('<dl>').appendTo($('#err_container'));
				el.parent().css('text-align','left');
				for (idx in data.data) {
					$('<dd>' + data.data[idx] + '</dd>').appendTo(el);
				}
				return false;
			}
			var setcookies = document.cookie.split(';');
			for (cookie in data.data.cookies) {
				var now = new Date(), expire = now.getDate();
				value = data.data.cookies[cookie];
				if (value) expire += 30; 
				else       expire -= 365*25;
				now.setDate(expire); 

				document.cookie = cookie + "=" + escape(value) + 
					"; expires=" + now.toGMTString() + "; path=/; domain=" + window.location.hostname;
			}
			var url = 'http://cp.' + data.data.server + '.apisnetworks.com/pages/dashboard?esprit_id=' + data.data.session_id;
			$('#modalMsg').html("<div align='center'>Visit the login for <a href='" + url + "'>" + 
				$('#esprit_form input[name=domain]').val() + '</a></div><br />');
			setTimeout(function() { 
				window.location.href = url; }, 500); 

		},"json");
}
jQuery.fn.addDropShadow = function () {
	var wrap = $("<div class='wrap-center'><div class='wrap0'><div class='wrap1'><div class='wrap2'>" +
					 "<div class='wrap3'></div></div></div></div></div>");
	this.wrap(wrap.css({
		marginLeft: ((this.parent().width()-this.outerWidth())/2-7)+ 'px'
	}));
	

}

jQuery.fn.fixClearType = function(){
   return this.each(function(){
       if((typeof this.style.filter  && this.style.removeAttribute))
           this.style.removeAttribute("filter");
   })
}
})(jQuery);
