Shadowbox.init({
    skipSetup: true,
	enableKeys:false
});

function getMini()
{
	$.getJSON("/cart/mini", function(data) {							 
		$("#miniData").html("");
		$("#miniData").html(data['items'] + " items");		
	});	
}

function added()
{
	Shadowbox.open({
		content:    '<div class="added2cart">Item added to cart<br /><span class="close">Click outisde this box to close this message or<br /><a href="/checkout" class="innerLinkVis">Click here to view your cart</a></span></div>',
		player:     "html",
		height:     250,
		width:      250
	});	
}

$(document).ready(function(){	
	
	getMini();
						   
	function bubbletarget(e)
	{
		if(e && e.originalTarget){
			var target = e.originalTarget.id;
		} else if(e && e.target){
			var target = e.target.id;
		} else if(document.activeElement && document.activeElement.tagName != "BODY"){
			var target = document.activeElement.id;
		}
		return target;
	}
	
	$("#command").on("click", function(e){
		Shadowbox.open({
			content:    '<div class="commandBox"><input type="text" id="commandToExecute" name="commandToExecute" value="type your command..."  onFocus="this.value=\'\'" /><input type="submit" name="submit" id="submitCommand" value="Execute Command" /><div id="commandOutput">&nbsp;</div></div>',
			player:     "html",
			height:     300,
			width:      500
		});
	});
	
	$("#submitCommand").live("click", function(e){		
		$.ajax({
		  url: window.location + "/executeCommand/" + encodeURIComponent($("#commandToExecute").val()),
		  cache: false,
		  success: function(html){
			$("#commandOutput").html("");
			$("#commandOutput").html(html);
		  }
		});
	});


	$(".add2cart").bind("click", function(e){
										  
		plan_id = bubbletarget(e);
		
		$.ajax({
		  url: "/cart/add/plan_id/" + plan_id,
		  cache: false,
		  success: function(){
			window.location.replace('/step2/type/hosting');
		  }
		});
	});
	
	$(".addContinue").submit(function(e){
		
		$this = $(this);
		
		$.ajax({
		  url: "/cart/add/plan_id/" + $this.find('input[name="plan_id"]').val(),
		  cache: false,
		  async: false
		});
		
		return true;
	});
	
	$(".addDomain2cart").submit(function(e){
		
		target = bubbletarget(e);
												
		$.post('/cart/add', $("#" + target).serialize(),
		function(data){
			window.location.replace('/step2/type/domains');
		});
		
		return false;
	});
	
	$("#addDomainFromSteps").submit(function(e){
		target = bubbletarget(e);									
		$.post('/cart/add', $("#" + target).serialize());
		return true;
	});
	
	$(".addinvoice").on("click", function(e){
										  
		invoice_id = bubbletarget(e);
		
		$.ajax({
		  url: "/cart/add/invoice_id/" + invoice_id,
		  cache: false,
		  success: function(){
			getMini();
		  }
		});
	});
	
	$(".renew").submit(function(e){
		
		target = bubbletarget(e);
												
		$.post('/cart/add', $("#" + target).serialize(),
		function(data){ 
			getMini();
		});
		
		added();
		
		return false;
	});
	
	$(".upgrade").submit(function(e){
		
		target = bubbletarget(e);
												
		$.post('/cart/add', $("#" + target).serialize(),
		function(data){ 
			getMini();
		});
		
		added();
		
		return false;
	});
	
	$(".remove").on("click", function(e){
										
		target = bubbletarget(e);
										
		$.ajax({
		  url: "/cart/remove/item/" + target,
		  cache: false,
		  success: function(html){
			window.location.reload();
		  }
		});
	});

	$(".whois").on("click", function(e){
		
		domain = bubbletarget(e);
		
		$.ajax({
		  url: "/info/whois?domain=" + domain,
		  cache: false,
		  success: function(html){
			$('#whois').empty();
			$('#whois').append(html);
		  }
		});
	});
	
	$('#checkDomainAvailableButton').bind("click", function(e){
										
		$('#status').html('<img src="/images/checking.gif" />');
		$('#checkDomainAvailableButtonDD').css("visibility", "hidden");
		$('#checkDomainAvailableButtonDD').css("display", "none");
		
		$.ajax({
		  url: "/info/checkdomain/domain/" + $('#checkDomainAvailable').val(),
		  cache: false,
		  success: function(html){
			
			if(html != "")
			{
				$('#status').html('<p class="innerP" style="color:green">Domain name is available</p>' + html);
			} else {
				$('#status').html('<p class="innerP" style="color:red">Domain name is not available, please try another domain.</p>');
			}
			
			$('#checkDomainAvailableButtonDD').css("visibility", "visible");
			$('#checkDomainAvailableButtonDD').css("display", "block");
		  }
		});
	});
	
	$('#loading').ajaxStart(function() {
		$('#whois').empty();
		$(this).show();
		}).ajaxStop(function() {
			$(this).hide();
		}
	);
	
	$(".generate.innerLinkVis").on("click", function(e){						 
		$this = $(this);
		$('#bannerCode').val('<div id="theserve"><a href="' + $this.data('banner') + '" title="Web Hosting, Virtual Servers, Domain Names"><img src="' + $this.data('url') + '" alt="Web Hosting, Virtual Servers, Domain Names" title="Web Hosting, Virtual Servers, Domain Names" /></a></div>');
	});
	
	$('article.text').one('mouseenter', function()
	{
		Socialite.load($(this)[0]);
	});
});
