/**
 * @author neila
 */
$(document).ready(function() {
	/* Round corners 
	$('#nav li.level1').corner("notch 2px");
	$('p.shop_button').corner('');
	*/
		
	/* IE6 Transparancies */
	if(browser_is_ie6()) {
		//$('#content, #panoramic_background, #my_tools, #special_offers_div, #download_pdf').supersleight();
		$('#featured_products_div, #bestsellers_div, #products_wrap, #cart_item_wrap, #my_tools, #special_offers_div, #download_pdf').supersleight();
	}
	
	/* Search box */
	if($('#search_query').val() == ''){
		$('#search_query').val('Search our Shop');
	}
	
	$('#search_query').focus(function() {
		if($(this).val() == 'Search our Shop')
			$(this).val('');	
	});
	
	$('#search_query').blur(function() {
		if($(this).val() == '')
			$(this).val('Search our Shop');
	});
	
	/* header login */
	$('#header_username').val('your@email.com');
	//$('#header_password').val('password');
	
	$('#header_username').focus(function() {
		if($(this).val() == 'your@email.com')
			$(this).val('');	
	});
	
	$('#header_username').blur(function() {
		if($(this).val() == '')
			$(this).val('your@email.com');
	});
	
	/*$('#header_password').focus(function() {
		if($(this).val() == 'password')
			$(this).val('');
	});*/
	
	/*$('#header_password').blur(function() {
		if($(this).val() == '')
			$(this).val('password');
	});*/
	
	$('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();
	
	// Flash Banner
	flashVars = {};
	flashVars.clickTag = 'http://www.channel4learning.com/support/whatsnew/index.html';
	
	flashParam = {};
	
	flashAttr = {};
	
	/*swfobject.embedSWF(
		'http://www.channel4learning.com/support/global/header/banner/C4L_V4.swf',
		'header_flash_banner',
		'468',
		'60',
		'7',
		null,
		flashVars,
		flashParam,
		flashAttr
	);*/
});

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");
				
				if(!href){
					return;
				}
				
				split_href = href.split("&");
				
				if(source.attr("id") == "email") {
					redirect="my_account";
				}
				else if(source.attr("id") == "p_email") {
					redirect = "checkout";
				}
				
				if(split_href[1]){
					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;
    }
  }
}