/**
 * @author neila
 */
$(document).ready(function() {
	/* Round corners 
	$('#nav li.level1').corner("notch 2px");
	$('p.shop_button').corner('');
	*/
		
	/* IE6 Transparancies */
	if(browser_is_ie6()) {
		$('#header').supersleight();
		$('.item_holder,.main_product,.product_holder,.cart_item').supersleight();
	}
	
	/* Search box */
	$('#search_query').focus(function() {
		if($(this).val() == 'Search our Shop')
			$(this).val('');	
	});
	$('#search_query').blur(function() {
		if($(this).val() == '')
			$(this).val('Search our Shop');	
	});
	
	
	$('body').addClass('js_enabled');
	
	/* Footer links shade layer handler */
	$('li.footer_link a.content').bind('click keyup',function(e) {
		
		e.preventDefault();
		
		content_id = $(this).attr('class').split(' ').pop();
		link_ref = '?page=content&amp;id=' + content_id;
		 
		shade_layer('content','744','496','B3B7BD',link_ref,'','');
	});
	
	initOverLabels();
});

function check_account(source)
{
	if(source.val().length > 5 && $("#email_login_prompt").attr('id')) {
	
		$.get("ajax/",{
		"page":"login",
	 	"action": "check_account",
	 	"email": source.val()
	 	},
	 	function(response) {
			
			if(response == 1) {
				
				$("#email_login_prompt").show();
				
				href = $("#email_login_prompt a.login").attr("href");
				
				split_href = href.split("&");
				
				if(source.attr("id") == "email") {
					redirect="my_account";
				}
				else if(source.attr("id") == "b_email") {
					redirect = "checkout";
				}
				
				split_href[1] = "redirect=" + redirect;
				
				href = split_href.join("&");
				
				$("#email_login_prompt a.login").attr("href",href);

				$('#cart_continue input').attr("disabled","disabled");
			}
			else {

				$("#email_login_prompt").hide();
				$('#cart_continue input').removeAttr("disabled");
			}
		});
	}
	else {
		$("#email_login_prompt").hide();
		$('#cart_continue input').removeAttr("disabled");		
	}
}
function send_new_password(email) {

	$.get("ajax/index.php",{
 		"page": "login",
		"action":"lost_password",
 		"email": email
 	},
	function(response) {
		
		if(response == 1) {
			$("#email_login_prompt").html(
				"<p>A new password has been sent to your email address</p>"+
				"<p>You may now <a href=\"?page=login&redirect=checkout\">Login</a> with your new password</p>"
			);	
		}
	});
}
/* IE6 fix for suckerfish menu */
sfHover = function() {
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

/*
	Style active nodes for suckerfish
*/
function style_menu(category_id) {
	
	source = $("#node_" + category_id);
	source.addClass('selected');
	source.parents('li').addClass('selected');
}

function browser_is_ie6() {
	
	if(typeof document.body.style.maxHeight === "undefined") {
	        return true;
	} else {
	        return false;
	}
}

function initOverLabels () {
  if (!document.getElementById) return;      

  var labels, id, field;

  // Set focus and blur handlers to hide and show 
  // labels with 'overlabel' class names.
  labels = document.getElementsByTagName('label');
  for (var i = 0; i < labels.length; i++) {

    if (labels[i].className == 'overlabel') {

      // Skip labels that do not have a named association
      // with another field.
      id = labels[i].htmlFor || labels[i].getAttribute('for');
      if (!id || !(field = document.getElementById(id))) {
        continue;
      } 

      // Change the applied class to hover the label 
      // over the form field.
      labels[i].className = 'overlabel-apply';

      // Hide any fields having an initial value.
      if (field.value !== '') {
        hideLabel(field.getAttribute('id'), true);
      }

      // Set handlers to show and hide labels.
      field.onfocus = function () {
        hideLabel(this.getAttribute('id'), true);
      };
      field.onblur = function () {
        if (this.value === '') {
          hideLabel(this.getAttribute('id'), false);
        }
      };

      // Handle clicks to label elements (for Safari).
      labels[i].onclick = function () {
        var id, field;
        id = this.getAttribute('for');
        if (id && (field = document.getElementById(id))) {
          field.focus();
        }
      };

    }
  }
};

function hideLabel (field_id, hide) {
  var field_for;
  var labels = document.getElementsByTagName('label');
  for (var i = 0; i < labels.length; i++) {
    field_for = labels[i].htmlFor || labels[i].getAttribute('for');
    if (field_for == field_id) {
      labels[i].style.textIndent = (hide) ? '-1000px' : '0px';
      return true;
    }
  }
}



function showHideDel(objToSwitch) {
	if (document.getElementById(objToSwitch).style.display == 'none') document.getElementById(objToSwitch).style.display = 'block';
	else document.getElementById(objToSwitch).style.display = 'none';
}

function checkTicks(objToCheck) {
	if (document.getElementById(objToCheck).checked == "true") {
		document.getElementById(objToCheck).style.display = 'block';
	} else {
		document.getElementById(objToCheck).style.display = 'none';
		document.getElementById(objToCheck).checked = "false";
	}
}

/* voucher check functions in right column shopping cart */
$(document).ready(function() {
  $('#vcheck').focus(function() {
		if($(this).val() == 'enter voucher code')
			$(this).val('');	
	});
	$('#vcheck').blur(function() {
		if($(this).val() == '')
			$(this).val('enter voucher code');	
	});
});




