/****************************************
GLOBAL.JS
- This file is used to store functions that are called on everypage
****************************************/


/****************************************
$().ready()
- this function is triggered when all page elements have been loaded
and are ready to be manipulated. just like onload="init()"
****************************************/
$().ready(function(){
				   
	$('.popupForm').hide();
	$('.popupSubmited').hide();
	$('div#detail-left').hide();
	$('div#detail-right').hide();
	

	$("#txtSearch").focus(function() {
	   if (this.value == "keyword or item #"){
		 this.value = "";		 
	   }
	 });
	$("#txtSearch").blur(function() {
	   if (this.value == ""){
		 this.value = "keyword or item #";
	   }
	 }); 
	
	//Top menu rollover
	$('.topMenu-class').bind('mouseover',function(){		
		$(this).children().attr('src','/content/products/tag/img/menu-class-on.gif');				
	});
	$('.topMenu-class').bind('mouseout',function(){		
		$(this).children().attr('src','/content/products/tag/img/menu-class.gif');				
	});
	$('.topMenu-store').bind('mouseover',function(){		
		$(this).children().attr('src','/content/products/tag/img/menu-store-on.gif');				
	});
	$('.topMenu-store').bind('mouseout',function(){		
		$(this).children().attr('src','/content/products/tag/img/menu-store.gif');				
	});
	$('.topMenu-resources').bind('mouseover',function(){		
		$(this).children().attr('src','/content/products/tag/img/menu-resources-on.gif');				
	});
	$('.topMenu-resources').bind('mouseout',function(){		
		$(this).children().attr('src','/content/products/tag/img/menu-resources.gif');				
	});
	$('.topMenu-support').bind('mouseover',function(){		
		$(this).children().attr('src','/content/products/tag/img/menu-support-on.gif');				
	});
	$('.topMenu-support').bind('mouseout',function(){		
		$(this).children().attr('src','/content/products/tag/img/menu-support.gif');				
	});
	$('.topMenu-about').bind('mouseover',function(){		
		$(this).children().attr('src','/content/products/tag/img/menu-about-on.gif');				
	});
	$('.topMenu-about').bind('mouseout',function(){		
		$(this).children().attr('src','/content/products/tag/img/menu-about.gif');				
	});
	
	//$('img#tab-content').attr('src','/content/products/tag/img/area-classics.jpg');

	// expand - collapse details box [pricing page]
	$('.toggleDetails').bind('click',function(){		
		$(this).next().toggle();				
		if ($(this).children().attr("class") == 'pricing-on'  )		{
			$(this).children().removeClass();
			$(this).children().addClass("pricing-off");			
		} else if ($(this).children().attr("class") == 'pricing-off'  )		{
			$(this).children().removeClass();
			$(this).children().addClass("pricing-on");			
		}
	});			
		
	// popup Details at left [library page]
	$(".lnkDetails-lft").bind('mouseover',function(){
		var param = $(this).attr('param');
		$('.popupDetails').hide();
		$('.pop-main').hide();
		$('div#detail-left').css("left",getAbsolutePosition(this).x+100)
		$('div#detail-left').css("top",getAbsolutePosition(this).y-200)
		$('div#detail-left').show();
		$('div#detail-' + param).show();
	});
	
	$(".popupDetails").bind('mouseout',function(){
		$('.popupDetails').hide();
		$('.pop-main').hide();
	});
	
		
	// popup Details at right [library page]
	$(".lnkDetails-rgt").bind('mouseover',function(){
		var param = $(this).attr('param');
		$('.popupDetails').hide();
		$('.pop-main').hide();
		$('div#detail-right').css("left",getAbsolutePosition(this).x-425)
		$('div#detail-right').css("top",getAbsolutePosition(this).y-200)
		$('div#detail-right').show();
		$('div#detail-' + param).show();
	});
	
	
	$(".lnkClosePopupDetails").bind('click',function(){
		$('.popupDetails').hide();
	});
	
	// popup form [home page]
	$(".lnkShowPopupForm").bind('click',function(){
		$('.popupForm').css("left",getAbsolutePosition(this).x+100)
		$('.popupForm').css("top",getAbsolutePosition(this).y-600)
		$('.popupSubmited').css("left",getAbsolutePosition(this).x+100)
		$('.popupSubmited').css("top",getAbsolutePosition(this).y-600)
		$('.popupForm').show();
		$('.popupSubmited').hide();
		$('#txtFirstName').focus();
		
	});
	
	$(".lnkClosePopupForm").bind('click',function(){
		$('.popupForm').hide();
	});
	
	$(".lnkClosePopupSubmited").bind('click',function(){
		$('.popupSubmited').hide();
	});
	
	// Handled by validation check in Ajax.js
	//$(".lnkSignIn").bind('click',function(){
	//	$('.popupForm').hide();
	//	$('.popupSubmited').show();
	//});
	
	// Binding explore tabs [home page]
	bindExploreTabs();

});

function bindExploreTabs() {
	$('div.portlet-tab').children().bind('click',function(){
		
		if ($(this).attr("class").indexOf('tab-lft-off') >= 0) {
			$(this).removeClass('tab-lft-off');
			$(this).addClass('tab-lft-on');
			$('.tab-cnt-on').removeClass('tab-cnt-on').addClass('tab-cnt-off-rgt');
			$('.tab-cnt-off-lft').removeClass('tab-cnt-off-lft').addClass('tab-cnt-off-rgt');
			$('.tab-rgt-on').removeClass('tab-rgt-on').addClass('tab-rgt-off');
			$('div#area-readers').hide();
			$('div#area-classics').hide();
			$('div#area-storybooks').show();
		} else {
			if (($(this).attr("class").indexOf('tab-cnt-off-rgt') >= 0) || ($(this).attr("class").indexOf('tab-cnt-off-lft') >= 0)){
				$(this).removeClass('tab-cnt-off-rgt');
				$(this).removeClass('tab-cnt-off-lft');
				$(this).addClass('tab-cnt-on');
				$('.tab-lft-on').removeClass('tab-lft-on').addClass('tab-lft-off');
				$('.tab-rgt-on').removeClass('tab-rgt-on').addClass('tab-rgt-off');
				$('div#area-storybooks').hide();
				$('div#area-classics').hide();
				$('div#area-readers').show();
			} else {
				if ($(this).attr("class").indexOf('tab-rgt-off') >= 0) {
					$(this).removeClass('tab-rgt-off');
					$(this).addClass('tab-rgt-on');
					$('.tab-cnt-on').removeClass('tab-cnt-on').addClass('tab-cnt-off-lft');
					$('.tab-cnt-off-rgt').removeClass('tab-cnt-off-rgt').addClass('tab-cnt-off-lft');
					$('.tab-lft-on').removeClass('tab-lft-on').addClass('tab-lft-off');					
					$('div#area-storybooks').hide();
					$('div#area-readers').hide();
					$('div#area-classics').show();
				}		
			}		
		}		
	});
}

function getAbsolutePosition(element) {
    var r = { x: element.offsetLeft, y: element.offsetTop };
    if (element.offsetParent) {
      var tmp = getAbsolutePosition(element.offsetParent);
      r.x += tmp.x;
	  if ($.browser.msie)
      	r.y += tmp.y +25;
	  else
	  	r.y += tmp.y;
    }
    return r;
 }