var xPosition;
var yPosition;

$(document).mousemove(function(e){		  
	  pageX = e.pageX;
	  pageY = e.pageY;
	  clientX = e.clientX;
	  clientY = e.clientY;		  
}); 	

$(window).hashchange( function(){	
	var pagehash = location.hash;	
	urlhash = pagehash.replace( /^#/, '' );
	hash = urlhash.split('_');	
	switch(hash[0])
	{
	case "itemID":
	 get_content(hash[1],hash[2]);
	 break;		    
	default:
	  none();
	}		
})	

$(document).ready(function() {	

	$('.form_fields').live('focusin', function() {
		$(this).removeClass("form_fields").addClass("form_fields_active");
	});	
	
	$('.form_fields_active').live('focusout', function() {
		$(this).removeClass("form_fields_active").addClass("form_fields");
	});	
		
	$("[tag='menuItem']").mouseleave(function() {
		$(this).css('cursor','default');
		$(this).removeClass('dropdown_menu_hover').addClass('dropdown_menu');
	});
	
	$("[tag='menuItem']").mouseenter(function() {
		$(this).css('cursor','default');
		$(this).removeClass('dropdown_menu').addClass('dropdown_menu_hover');
	});	
	
	$("[hoverEffect='Y']").mouseenter(function() {
		$(this).fadeTo('fast',.67);		
	}).mouseleave(function() {
		$(this).fadeTo('fast',1.0);		
	});  		
	
	$(".tab_active,.tab_inactive").mouseenter(function() {
		if($(this).hasClass('tab_inactive')) {
			$(this).removeClass('tab_inactive').addClass('tab_hover');
		}
	}).mouseleave(function() {
		$(this).css('cursor','default');
		if($(this).hasClass('tab_hover')) {
			id = $(this).attr('id');
			$(this).removeClass('tab_hover').addClass('tab_inactive');
		}
	});
	
	//Fade Out Menu
	$("[tag='menuTable']").mouseleave(function() {
		$(this).fadeOut();		
	}).click(function(){
		$(this).fadeOut();
	});
	
	$("[tag='tab']").mouseenter(function() {
		$(this).css('cursor','pointer');   
		id = $(this).attr('id');		
		$("[id^='menu']").filter("[id!=id]").hide();  //Hide other dropdowns
		$('#menu'+id).slideDown(); // Show Menu		
	});		
	
	$("#active"+$('#Item_ID').val()).show();  //show bold for active page
	$("#link"+$('#Item_ID').val()).hide();	  //hid link for active page
	
	//Go to Page
	var pagehash = location.hash;	
	urlhash = pagehash.replace( /^#/, '' );
	hash = urlhash.split('_');	
	if(hash[0] == '') {
		 $('#tblContent').show();	
	}
	switch(hash[0])	{			  	
	case 'itemID':		
		get_content(hash[1],hash[2],'N');
		$("[id^='link']").show();
		$("[id^='active']").hide();
		$("#active"+hash[1]).show();
		$("#link"+hash[1]).hide(); 
	default:	 
	  none();
	}	
});

function get_content(id,updateMenu,slide) { 	
	currentPage = $('#page').val();	
	$('#divSubMenu').hide();
	$('#tdSubMenu').html('');
	$('#tdContent').load(currentPage + '.php?Item_ID=' + id + ' #tblContent', function() {
		$(':text').addClass('form_fields');		
		
		if(updateMenu != 'N') {
			$("[id^='link']").show();
			$("[id^='active']").hide();
			$("#active"+id).show();
			$("#link"+id).hide(); 
			if(slide != 'N') {
				$('#tblContent').show("slide", { direction: "left" }, 333);	
			}
			else {
				$('#tblContent').show();
			}
						
		}
		scrollTo(0,0);
		
		//Load any submenu content
		if($('#tdSubMenu').length) {
			$('#tdSubMenu').load(currentPage + '.php?Item_ID=' + id + ' #divSubMenu');			
			$('#divSubMenu').slideDown();
		}
		
		//Load video player
		if($('#player').length) {
			flowplayer("player", "../flowplayer-3.2.7.swf");
		}
		
		//Load Photo Gallery
		if($("a[rel^='prettyPhoto']").length) {
			$("a[rel^='prettyPhoto']").prettyPhoto({
				animation_speed: 'fast', /* fast/slow/normal */
				slideshow: 5000, /* false OR interval time in ms */
				autoplay_slideshow: true,
				theme: 'pp_default',
				opacity: 0.50
			});
		}
		
	});	
}

function addOverlay() {
	if($('#overlay').length) {
		return;
	}
	/* Add overlay and make clickable*/ 
	 var w = $(window).width();  
	 var h = $(window).height(); 
	 var $overlay = $('<div/>', {   
		'id': 'overlay',   
		css: {   
			position   : 'fixed',    
			height     : h + 'px',    
			width      : w + 'px',    
			left       : 0,    
			top        : 0,    
			background : '#000',    
			opacity    : 0.7,    
			zIndex     : 99   }  
	}).appendTo('body');  
	
	/* Click overlay to remove */ 
	$('#overlay').click(function(){   
		$(this).remove(); 
		$("[tag='popupWindow']").fadeOut(); 
	}); 
}

function gallery(img,caption) {
	$("body").prepend('<p id="loading" style="z-index:106; position:absolute"><img src="http://www.smcpackaging.com/Assets/loader1.gif" width="48" height="48" /></p>');
	$('#loading').center().show();
	imgHTML = '<img id="imgImage" onclick="' + "$('#imageViewer').fadeOut();$('#overlay').remove()" + '"/>';
	
	if(caption != '' && caption != undefined) {		
		imgHTML += '<table class="small_text" align="center" cellpadding="7"><tr><td>' + caption + '</td></tr></table>'; 
	}
	addOverlay();
	$('#imageViewer').html(imgHTML);	
	$('#imgImage').attr("src",img).load(function() { 
		$('#loading').hide();
		$('#imageViewer').center().slideDown();
	});
}

function validateEmail(str) {
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if (str.indexOf(at)==-1){
	   return false;
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		return false;
	}

	 if (str.indexOf(at,(lat+1))!=-1){
		return false;
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		return false;
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
		return false;
	 }
	
	 if (str.indexOf(" ")!=-1){
		return false;
	 }

	 return true;					
}

function message_length(elementID) {
	var r = $.countLines('#'+elementID);
	if(r.actual > $('#'+elementID).attr("rows")) {
		$('#'+elementID).attr("rows",r.actual);
	}	
}


function none() {
	
}


function checkall(theElement) {	 
	 i=1;
	 a = theElement + i;
	 if(document.getElementById(theElement).checked == true) {
		 do {
			 a = theElement + i;
			 var b = document.getElementById(a);
			 if(document.getElementById(a)) {
			 	b.checked = true;
			 }
			 i++;
		 } while(document.getElementById(a))
	 }
	 else {
		  do {
			 a = theElement + i;
			 var b = document.getElementById(a);
			 if(document.getElementById(a)) {
				 b.checked = false;
			 }
			 i++;
		 } while(document.getElementById(a))		 
	 } 
}

function setCookie(cookieName,cookieValue,resetCookie,expireDays) {
	if(resetCookie != 'Y') {
		days = expireDays;
		if(expireDays == undefined || expireDays == '') {
			days = 365;
		}
		var exdate=new Date();exdate.setDate(exdate.getDate()+days);
		document.cookie=cookieName+ "=" +escape(cookieValue)+((days==null) ? "" : ";expires="+exdate.toGMTString());
		
	}
	else {	
		document.cookie = cookieName +'=; expires=Thu, 01-Jan-70 00:00:01 GMT;';		
	}
}

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 "";
}

function errorMessage(message) {
	$('#tdError').html(message);
	$('#divError').slideDown();
	setTimeout("$('#divError').slideUp()",15000);
}

function show_right(itemID,hide,effect,trans) {	 
	var x = document.getElementById(itemID);
	
	var $win = $(window);
	var winWidth = $win.width();
	var winHeight = $win.height() + $win.scrollTop();
	var popupWidth = $(x).width();
	var popupHeight = $(x).height();
	offset = 7;		
	
	/*set width*/
	if(xPosition != '') {		
		var lPos = parseInt(xPosition) + offset;		
		if((lPos + popupWidth) > winWidth) {		
			lPos = parseInt(xPosition) - popupWidth - (offset*2);			
		}
	}
	else {
		var lPos = parseInt(pageX) + offset;	
		if(lPos + popupWidth > winWidth) {		
			lPos = parseInt(pageX) - popupWidth - (offset*2);			
		}	
	}	
	
	if(lPos < 0) {
		lPos = offset;
	}	
		
	/*set height	*/
	if(yPosition != '') {	
		var tPosition = parseInt(yPosition) + offset;	
		if(popupHeight + tPosition > winHeight) {		
			tPosition = parseInt(yPosition) - popupHeight - (offset*2);		
		}	
	}
	else {
		var tPosition = parseInt(pageY) + offset;		
		if(popupHeight + tPosition > winHeight) {		
			tPosition = parseInt(pageY) - popupHeight - (offset*2);		
		}		
	}
	
	if(tPosition < 0) {
		tPosition = offset;
	}	
	
	x.style.position='absolute';	
	x.style.left=lPos + "px";	
	x.style.top=tPosition + "px";	
	
	if(hide != 'Y') {
		if(effect == 'slide') {
			$(x).show("slide", { direction: "left" }, 300);
		}
		else {
			$(x).slideDown("normal");
		}
	}
	else {
		$(x).fadeTo("fast",0.0); /*used for datepicker*/
		$("[tag='calendar']").focus();
	}
	
	if(trans > 0) {
		$(x).fadeTo('fast',trans);
	}
	else {
		$(x).fadeIn();	
	}
	
	xPosition = '';
	yPosition = '';
}


//OLD FUNCTIONS BELOW
function hide(itemID) {
	var x = document.getElementById(itemID);
	if(document.getElementById(itemID)) {
		x.style.display = 'none';
	}
}

function show(itemID) {
	var x = document.getElementById(itemID); 
	//if(document.getElementById(itemID)) {
		if(document.getElementById('debug')) {
			//document.getElementById('debug').innerHTML = x.tagName + " " + itemID;
			//document.getElementById('debug').innerHTML = "hello";
		}
		
		if(document.getElementById(itemID)) {
			var z = x.tagName;			
			
			switch (z)
			{
			case 'TABLE':
			  x.style.display = "inline";
			  break;
			case 'TR':
			  x.style.display = "block";
			  break; 
			case 'TD':
			  x.style.display = "block";
			  break;    
			default:	   
			  x.style.display = "inline";
			}	
			
			if(x.style.visibility != "visible") {
				x.style.visibility = "visible";
			}
		}
	//}
}



