// JavaScript Document
function testInput() {
	var Email =  new String(document.inputForm.Email.value);
	var Correct = true;
	CharCount=1;
	strLength=Email.length;
	while ((CharCount < strLength) && (Email.charAt(CharCount) != '@'))
	{
		CharCount++;
	}
	if ((CharCount>= strLength) || (Email.charAt(CharCount) != '@')) 
	{ 
		Correct = false;
	}
	else
	{
		CharCount += 2;
	}
	while ((CharCount < strLength) && (Email.charAt(CharCount) != '.'))
	{
		CharCount++;
	}
	if ((CharCount>= strLength - 1) || (Email.charAt(CharCount) != '.'))
	{		
		Correct = false;
	}
	if (!Correct){
		alert ('Email address not valid.');
	}
	return Correct;
}

function setCookie(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function getCookie(c_name)
{
	if (document.cookie.length>0)
	  {
	  c_start=document.cookie.indexOf(c_name + "=");
	  if (c_start!=-1)
		{ 
		c_start=c_start + c_name.length+1; 
		c_end=document.cookie.indexOf(";",c_start);
		if (c_end==-1) c_end=document.cookie.length;
		return unescape(document.cookie.substring(c_start,c_end));
		} 
	  }
	return "";
}

$(document).ready(function(){
						   
	$('#content5').css('behavior','url("/sc_lib/js/iepngfix.htc")');
	
	var substate = false;
	
	$('#subscribelink').click(function(){
	
		$('#subscribebox')
			.css({
				 'display':'none',
				 'position':'absolute',
				 'z-index':'999',
				 'top':'20px',
				 'right':'20px',
				 'background':'#D5E8BB',
				 'border':'2px solid #5B8A3C',
				 'padding':'0',
				 'width':'355px'
				 })
			.load("/subscribe.html?v=2 #subscribecontent")
			.fadeIn('slow', function(){
					substate = true;	
					$('#subscribeboxclose').show();
			});			
	
		return false;
	
	});
	
	$('#content1, #content2, #content3, #content4').hover(function(){
		$(this).css('cursor','pointer');															   
	}, function(){
	   $(this).css('cursor','default');
	});
	
	$('body').click(function(e){
		var $target = $(e.target);
		if($target.is('.newsletterlinksubscribe')){
			
			if(testInput()){
				document.inputForm.Mode.value='update'; 
				$('#subscribebox').fadeOut('fast');
				document.inputForm.submit();
			}					
			
		}
		if($target.is('.newsletterlinkunsubscribe')){
			
			if(testInput()){
				document.inputForm.Mode.value='unsubscribe'; 
				$('#subscribebox').fadeOut('fast');
				document.inputForm.submit();
			}			
			
		}
		if($target.is('#subscribeboxclose')){
			$('#subscribebox').fadeOut('fast');
			return false;
		}	
		
	});
	
	
		
		$('#content1, #content1 h2, #content1 p').click(function(){
			window.location = 'index.php?section=145&page=91';
		});
		$('#content2, #content2 h2, #content2 p').click(function(){
			window.location = 'index.php?section=151&page=97';
		});	
		$('#content3, #content3 h2, #content3 p').click(function(){
			window.location = 'index.php?section=146&page=92';
		});	
		$('#content4, #content4 h2, #content4 p').click(function(){
			window.location = 'index.php?section=163&page=109';
		});
				
	
	
	
				

	$('.popupmore').prev().append('<a href="" class="popupmoreball">find out more</a>');
	$('#content1 .popupmoreball').click(function(e){
		var p = $('#content1 .popupmore').text();
		var t = $(this);
		
		$("div[rel='popup']:visible").fadeOut('fast');
		$('<div></div>')
			.append(p)
			.attr('rel','popup')
			.css(
				{
					'padding':'1em',
					'border':'1px solid #2B381A;',
					'background':'#BECAB2',
					'position':'absolute',
					'top':(0) + 'px',
					'right':(0) + 'px',
					'z-index':'300',
					'display':'none'
				}
			)	
			.insertAfter(t)
			.fadeIn('slow');
			
					//'top':(e.pageY) + 'px',
					//'left':(e.pageX) + 'px',
			
		return false;
	});
	
	$('#content2 .popupmoreball').click(function(e){
		var p = $('#content2 .popupmore').text();
		var t = $(this);
		
		$("div[rel='popup']:visible").fadeOut('fast');
		$('<div></div>')
			.append(p)
			.attr('rel','popup')
			.css(
				{
					'padding':'1em',
					'border':'1px solid #2B381A;',
					'background':'#BECAB2',
					'position':'absolute',
					'top':(0) + 'px',
					'left':(e.pageX - 200) + 'px',
					'z-index':'300',
					'display':'none'
				}
			)	
			.insertAfter(t)
			.fadeIn('slow');
			
					//'top':(e.pageY) + 'px',
					//'left':(e.pageX) + 'px',
			
		return false;
	});
	
	$('#content3 .popupmoreball').click(function(e){
		var p = $('#content3 .popupmore').text();
		var t = $(this);
		
		$("div[rel='popup']:visible").fadeOut('fast');
		$('<div></div>')
			.append(p)
			.attr('rel','popup')
			.css(
				{
					'padding':'1em',
					'border':'1px solid #2B381A;',
					'background':'#BECAB2',
					'position':'absolute',
					'top':(0) + 'px',
					'left':(e.pageX - 200) + 'px',
					'z-index':'300',
					'display':'none'
				}
			)	
			.insertAfter(t)
			.fadeIn('slow');
			
					//'top':(e.pageY) + 'px',
					//'left':(e.pageX) + 'px',
			
		return false;
	});
	
	$('#content4 .popupmoreball').click(function(e){
		var p = $('#content4 .popupmore').text();
		var t = $(this);
		
		$("div[rel='popup']:visible").fadeOut('fast');
		$('<div></div>')
			.append(p)
			.attr('rel','popup')
			.css(
				{
					'padding':'1em',
					'border':'1px solid #2B381A;',
					'background':'#BECAB2',
					'position':'absolute',
					'top':(0) + 'px',
					'left':(e.pageX - 200) + 'px',
					'z-index':'300',
					'display':'none'
				}
			)	
			.insertAfter(t)
			.fadeIn('slow');
			
					//'top':(e.pageY) + 'px',
					//'left':(e.pageX) + 'px',
			
		return false;
	});
	
	var hpitemsummarytext = $('div.hpitemsummary').html();
	var itemimgsrc = $('img:first', hpitemsummarytext).attr('src');
	var itemimgalt = $('img:first', hpitemsummarytext).attr('alt');
	if (itemimgsrc != ''){
		$('.prodimage').attr('src',itemimgsrc).attr('alt',itemimgalt);
	}
	
	$('#header .welcome span').hover(function(){
		$(this).css('cursor','pointer');								  
	}, function(){
		$(this).css('cursor','default');
	});
	
	var vidStr = $('#homepagevideo').html();
	
	$('#header .welcome span').click(function(){
				
		if($('#homepagevideo').is(':visible')){
			$('#homepagevideo').fadeOut('slow', function(){
				$(this).empty();													   
			});	
			$(this).text('Welcome Video');
		} else {
			$('#homepagevideo').empty().append(vidStr).fadeIn('slow');
			$(this).text('Hide Video');
		}
	});
		
	var showvid = getCookie('showvid');
	if (showvid != null && showvid != ''){
		// cookie exists so do nothing		
	} else {
		// show vid and set cookie
		$('#header .welcome span').click();
		setCookie('showvid','nope',365);
	}
	
	

});