$(document).ready(function(){
	// Apply active state to clicked menu items
	$('.clickmenu a').click(function(){
		parentdiv = $(this).parent().get(0);
		$(parentdiv).children().removeClass('active');
		$(this).addClass('active');
	});

	// Cart actions
	$('#cart_button').click(function(){
		var active = $('#cart_button').hasClass('active');
			
		if(active)
			close_cart();
		else
			open_cart();
	});
	
	// Deal with default values in login forms
	var email_default = 'Email';
	var password_default = 'Password';
	
	if($('#login_username').val() == '')
	{
		$('#login_username').val(email_default);
		$('#login_password').hide();
		$('#login_password_cover').show();
		$('#login_password_cover').val(password_default);
	}
	else
	{
		$('#login_password_cover').hide();
		$('#login_password').show();
	}	
	
	$('#login_username').focus(function(){
		if($('#login_username').val() == email_default)
			$('#login_username').val("");
	});
	
	$('#login_password_cover').focus(function(){
		$('#login_password_cover').hide();
		$('#login_password').show();
		$('#login_password').focus();
	});
	
	// Initialise autocomplete
	$('#header_search').autocomplete
	(
		BASE + '/ajax/productsearch/',
		{
			width: 250,
			minChars: 3,
			delay: 1000,
			matchSubset: false,
			scrollHeight: 600,
			formatItem:function(item)
			{
				returnhtml = (item[1].length > 4) ? "<img class=\"right\" src=\"" + item[1] + "\" alt=\"\"/>\n" : "";
				return returnhtml  + item[0];
			}
		}
	);
	
	$('#header_search').autocomplete().result(
		function(event,item,formatted)
		{
			if(item[2].length > 4) location = item[2];
		}
	);
	
	$("#msgIFrame").contents().find("a").attr("target", "_top");
	
	// Display colorbox if verifyPolicy Div render
	if ($('#verifyPolicy').length > 0)
	{
		// TODO: Need to make dynamic code 
		var externalUrl = ($('#verifyPolicy').html() == 'profile') ? '/?policy' : '';
		
		$.fn.colorbox({
			href:BASE + '/ajax/acceptpolicy' + externalUrl, 
			open:true, 
			width: 555, 
			height:450, 
			escKey:false, 
			overlayClose:false, 
			onLoad: function(){
				$('#cboxClose').remove();
			}
		});
		
		$('#sendPolicyMail > a').live('click', function(){
			$(this).hide();
			$.ajax({
				type: "POST",
				url: BASE + '/ajax/acceptpolicy',
				data: "action=email",
				beforeSend: function()
				{
					$('#sendPolicyMail').css('background','url('+ BASE + '/img/ajax.gif) no-repeat 50% 0 transparent');
				},
				success: function(msg)
				{
					$('#sendPolicyMail').removeAttr('style');
					$('#sendPolicyMail').html('<span>' + msg + '</span>');
				}
			});
		});
		
		$('div#acceptPolicy > input[type^="image"]').live('click', function(){
			$.ajax({
				type: "POST",
				url: BASE + '/ajax/acceptpolicy',
				data: "action=update",
				success: function(msg)
				{
					if (msg == 'OK')
					{
						$.fn.colorbox.close();
					}
				}
			});
		});
	}
	
	// Display colorbox if password expired
	if ($('#passwordExpired').length > 0)
	{
		$.fn.colorbox({
			href: BASE + "/ajax/password", 
			open: true, 
			width: 450, 
			height: 310,
			escKey: false, 
			overlayClose: false, 
			onLoad: function(){
				$('#cboxClose').remove();
			}
		});
	}
	
	$('div#cmsSaveBar > a.cmsButton').live('click', function()
	{
		var html = "<div style=\"position: relative;\">" +
						"<img src=\""+BASE+"/img/loadingAnimation.gif\" id=\"waitMessageImg\">" +
						"<span id=\"waitMessageText\">Processing...</span>" +
					"</div>";
		$(this).parent().append(html);
	});
});

function submitFormByAjax(url, formId)
{	
	$.ajax({
		type: "POST",
		url: BASE + url,
		data: $('form#'+formId).serialize(),
		success: function(msg)
		{
			$('#cboxLoadedContent').html(msg);
			$.fn.colorbox.resize();
		}
	});
}

function closeColorBox()
{
	$.fn.colorbox.close();
}

function open_cart()
{
	$('#cart_button').addClass('active');
	$('#cart_details').slideDown();
	$('#header_search_form').hide();	
}

function close_cart()
{
	$('#cart_button').removeClass('active');
	$('#cart_details').slideUp();
	$('#header_search_form').slideDown();
}

function ajax_message(msg)
{
	$('#ajax_bar').slideDown().html(msg);
}

function ajax_complete()
{
	$('#ajax_bar').slideUp();
}

function search()
{
	/* Prepare URL to redirect to non-secure page */
	var searchUrl = $('#URL').val() + '/' + stripslashes($('#header_search').val());	// Convert all POST to GET
	window.location = searchUrl;
}

function stripslashes(str) {
	str=str.replace(/\//g,'');
	str=str.replace(/\\/g,'');
	str=str.replace(/\%/g,'');
	return str;
}
