var timeInterval = 0;
var m_names = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec");
var caching = false;			//ajax caching

$(document).ready(
	function(){
		timeInterval = setInterval(getTimeDate, 1000);
		initMenuHover();
		initDomainSearch();
		initPortfolio();
	}
);


function getTimeDate(){
	var d = new Date();
	var curr_date = d.getDate();
	var curr_month = d.getMonth();
	var curr_year = d.getFullYear();
	
	var curr_hour = d.getHours();
	var curr_min = d.getMinutes();
	var curr_sec = d.getSeconds();
	
	if(curr_date < 10){
		curr_date = "0" + curr_date;
	}
	
	if(curr_hour < 10){
		curr_hour = "0" + curr_hour;
	}
	
	if(curr_min < 10){
		curr_min = "0" + curr_min;
	}
	
	if(curr_sec < 10){
		curr_sec = "0" + curr_sec;
	}
	
	str = curr_date + " " + m_names[curr_month] + " " + curr_year + " | " + curr_hour + ":" + curr_min + ":" + curr_sec;
	$("#timeClock").html(str);
}



function checkClearBox(obj, text){
	if($(obj).attr('value') == text){
		$(obj).attr('value','');
	}
}

function checkFillBox(obj, text){
	if($(obj).attr('value') == ''){
		$(obj).attr('value',text);
	}
}

function switchSearch(text, searchType){
	var curentText = $("#searchWords").attr('value');
	if (curentText != text + " Search"){
		$("#searchWords").attr('value', text);
		$("#searchWords").attr('onFocus', "checkClearBox(this,  'text');");
		$("#searchWords").attr('onBlur', "checkFillBox(this, 'text');");
	}

	$('#searchIn').attr('value', searchType);
	$('.searchTypeItems').hide();
}

function showHideTLD(){
	if($('#hiddenTLD').is(':hidden')){
		$('#hiddenTLD').css('display', 'block');
		$('#allTldSwitch').attr('src', '/templates/leafSkin/images/colapse.gif');
	}
	else{
		$('#hiddenTLD').css('display', 'none');
		$('#allTldSwitch').attr('src', '/templates/leafSkin/images/expand.gif');
	}
}


basicWebsite = 0;
advancedWebsite = 0;
basicWebsiteSmallID = '/buy-basic-website-plan-with-stock-template.html';
basicWebsiteBigID = '/buy-basic-website-plan-with-custom-template.html';
advancedWebsiteSmallID = '/buy-advanced-website-plan-with-stock-template.html';
advancedWebsiteBigID = '/buy-advanced-website-plan-with-custom-template.html';

function updatePrice(ID, price){
	if(ID == "basicWebsite"){
		if (((basicWebsite == 0) && (price < 0)) || ((basicWebsite == 1) && (price > 0))){
			return false;
		}
		else{
			basicWebsite = (basicWebsite - 1) * ( -1 );
			if(price < 0){
				newId = basicWebsiteSmallID;
			}
			else{
				newId = basicWebsiteBigID;
			}
		}
	}
	else{
		if(ID == "advancedWebsite"){
			if (((advancedWebsite == 0) && (price < 0)) || ((advancedWebsite == 1) && (price > 0))){
				return false;
			}
			else{
				advancedWebsite = (advancedWebsite - 1) * ( -1 );
				if(price < 0){
					newId = advancedWebsiteSmallID;
				}
				else{
					newId = advancedWebsiteBigID;
				}
			}
		}
		else{
			return false;
		}
	}
	
	price = $("#"+ID).html() - (price * (-1));
	$("#"+ID).html(price);
	$("#"+ID+"Order").attr("href", newId);
}


/*******************************************
	Slide all the menus up and down
*******************************************/

function initMenuHover(){
	$('.hasSubmenu').hover(
		function (){
			slideElement = $(this).children("div");
			if(slideElement.is(':animated')){
				slideElement.stop().hide().css("height", "auto").slideDown(500);
			}
			else{
				slideElement.slideDown(500);
			}
		}
		,
		function (){
			slideElement = $(this).children("div");
			if(slideElement.is(':animated')){
				slideElement.stop().slideUp(500, function(){$(this).css("height", "auto");});
			}
			else{
				slideElement.slideUp(500);
			}
		}
	);
	
	$('.hasSubmenuInstant').hover(
		function (){
			slideElement = $(this).children(".content").children("div").children("div");
			slideElement.show();
		}
		,
		function (){
			slideElement = $(this).children(".content").children("div").children("div");
			slideElement.hide();
		}
	);
}

function initDomainSearch(){
	$('.searchRow').each(checkDomain);
}

function checkDomain(){
	var topParent = this;
	var domainName = $(this).children('.s13').html();
	
	$.ajax({
		url: "/domainCheckAjax.php",
		type: "POST",
		cache: caching,
		data: "domainNames[]="+domainName,
		success: function(html){
			$('#tmpHolder').html(html);
			//$('#tmpHolder').children('div').each(updateDomain);
			$(topParent).html($('#tmpHolder').children('div').html());
			$('#tmpHolder').html('');
		}
	});
}
/*
function updateDomain(){
	var id = $(this).attr('id');
	alert($('#tmpHolder').html());
	$('.searchRow' + id).html($(this).html());
}
*/

function initPortfolio(){
	$('.portfolioPictures').children('div').click(switchPicture);
}

function switchPicture(){
	if($(this).hasClass('inactive')){
		$(this).parent().children('div').removeClass('active');
		$(this).parent().children('div').addClass('inactive');
		$(this).addClass('active');
		$(this).removeClass('inactive');
		newImg = '0' + $(this).html();
		imageHolder = $(this).parent().parent().children('.image').children('img');
		newImg = $(imageHolder).attr('src').replace(/[0-9]{2}/, newImg);
		$(imageHolder).parent().children('.loader').show().fadeTo(500, 0.8);
		var img = new Image();
		$(img).load(function () {
			$(imageHolder).parent().children('.loader').fadeTo(500, 0, function (){$(imageHolder).attr('src', newImg);});
			
		}).attr('src', newImg);
		
	}
}

