/* !Drop Down Menu */
function startlist(){
	if (document.all) {
		navRoot = document.getElementById("main_nav");
		for (i=0; i<navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName=="LI") {
				node.onmouseover=function() {
					this.className+=" over";
				}
				node.onmouseout=function() {
					this.className=this.className.replace 
					(" over", "");
				}
			}
		}
	}
}
/* !jQuery */
$(function(){	
	startlist();
	$.ajaxSetup ({  cache: false  });
	/* !About Data Dialog Box */
	$('#dialogAboutData').jqm();
	if($('#aboutDataBtn')){
		$('#aboutDataBtn').click(function(){
			$('#dialogAboutData').jqmShow();
			return false;
		});
		$('#closeAbout').click(function(){
			$('#dialogAboutData').jqmHide();
			return false;
		});
	}	
	$('#dialogBookmark').jqm();
	
	$('#closeBookmark').click(function(){
		$('#dialogBookmark').jqmHide();
		return false;
	});	
	
	$('#errorWindow').jqm();
	$('#avoActionSubmit').click(function() { 
		var checkVal1	= true;
		var checkVal2	= true;
		var aFirstName	= $('#avoaction_first_name').val();
		var aEmail		= $('#avoaction_email').val();
		
		checkVal1		= checkBlank('#avoaction_first_name');
		checkVal2		= checkBlank('#avoaction_email');
		
		if(checkVal1 && checkVal2) {
			var urlPath 		= window.location.protocol + "//" + window.location.host + "/";
			var avoActionPath	= "avocation";
			var avoActionThank	= urlPath+"thank-you";
			$.ajax({
				type: "POST",
				url: avoActionPath,
				data: "first_name="+aFirstName+"&email="+aEmail,
				success: function(msg){
					if(msg == "SUCCESS!!!") {
						window.location = avoActionThank;
					} else {
						$('#errorWindow').html(msg);
						$('#errorWindow').jqmShow();
					}
				}			
			});
		}						
		return false;		
	});
	
	/* !Book Mark Widget */
	var base_url = "http://"+window.location.host+"/";	
	var COOKIE_LINKS	= 'bookmarks';
	var currentPage	= window.location;
	/* !About Hab Fix */
	if(currentPage == base_url+"about-hab"){
		currentPage = "About HAB";
	}
	if(currentPage == base_url+"about-hab/annual-reports"){
		currentPage = "About HAB/annual-reports";
	}
	if(currentPage == base_url+"about-hab/overview"){
		currentPage = "About HAB/overview";
	}
	if(currentPage == base_url+"about-hab/hab-board-members"){
		currentPage = "About HAB/HAB-board-members";
	}
	if(currentPage == base_url+"about-hab/hab-staff"){
		currentPage = "About HAB/HAB-staff";
	}

	if(currentPage == base_url+"about-hab/meetings-events"){
		currentPage = "About HAB /Meetings & Events";
	}
	if(currentPage == base_url+"about-hab/links"){
		currentPage = "About HAB/links";
	}
	var editBookmarks	= false;	
	var options = { path: '/'};	
	displayBookMarks();	
	/* !_save book mark 	 */
	if($('#bookmark_icon')) {
		$('#bookmark_icon').parent().click(function(){
			addBookMark();
			return false;
		});
	}	
	/* !_add book mark 	 */
	function addBookMark(){
		setBookMark();
		displayBookMarks();		
		$('#dialogBookmark').jqmShow();
		return false;	
	}
	/* !set book mark */
	function setBookMark(){
		var cookieData		= $.cookie(COOKIE_LINKS);
		if(!cookieData) {
			var newCookieData = currentPage;
		} else {
			var cookieCheck	= checkBookMark();
			if(!cookieCheck) {
				var newCookieData	= cookieData + "," + currentPage;
			} else {
				var newCookieData = cookieData;
			}
		}		
		$.cookie(COOKIE_LINKS, newCookieData, options);		
	}	
	/* !check book mark */
	function checkBookMark(){
		var bookmarks			= getBookMarks();
		var cookieCheck		=	false;
		for (var i in bookmarks){
			if(currentPage == bookmarks[i]){
				cookieCheck = true;
			}
		} 
		return cookieCheck;
	}
	/* !get book mark */
	function getBookMarks(){
		var cookieBookMarks	=	$.cookie(COOKIE_LINKS);
		if(!cookieBookMarks) {
			return false;
		} else {
			var bookmarks				=	cookieBookMarks.split(",");		
			if(bookmarks.length > 0) {				
				// Strip bookmark of duplicates
				bookmarksUnique 		= unique(bookmarks);
				var cleanCookie		= bookmarksUnique.join(",");
				$.cookie(COOKIE_LINKS, cleanCookie, options);			
				return bookmarksUnique;
			}
			else {
				return false;
			}
		}
	}
	/* !display book mark */
	function displayBookMarks(){
		var bookmarks			= getBookMarks();			
		if(bookmarks.length > 0) {				
			var html					= "";
			for(var i in bookmarks) {
				var pageName	= bookmarks[i];
				pageN				= makeBookmarLinksReadable(pageName);
				pageName			= capitalizeMe(pageN);
				html	+= "<li><a href='"+bookmarks[i]+"'>"+pageName+"</a></li>";
			}		
			$("#bookmark_links").html(html);
		}
	}	
	function unique(a) {
	   var r = new Array();
	   o:for(var i = 0, n = a.length; i < n; i++){
	      for(var x = 0, y = r.length; x < y; x++){
	         if(r[x]==a[i]) continue o;
	      }
	      r[r.length] = a[i];
	   }
	   return r;
	}
	function capitalizeMe(pageName) {
		var val = pageName;
		var newVal = '';
		val = val.split(' ');
		for(var c=0; c < val.length; c++) {
			newVal += val[c].substring(0,1).toUpperCase() +
			val[c].substring(1,val[c].length) + ' ';
		}
		return newVal;
	}
	/* !delete book mark */	
	$('#editBookmarks').click(function(){
		if(!editBookmarks) {
			var bookmarks			= getBookMarks();			
			if(bookmarks.length > 0) {				
				var html					= "";
				for(var i in bookmarks) {
				var pageName	= bookmarks[i];
				pageN				= makeBookmarLinksReadable(pageName);
				pageName			= capitalizeMe(pageN);
					html	+= "<li><a href='"+bookmarks[i]+"'>"+pageName+"</a> - <a href='#' id='b_"+i+"' class='bookmarkedit' style='background-image:none;padding:0px;margin:0px;color:#aa0000;'>Delete</a></li>";
				}
				editBookmarks = true;		
				$("#bookmark_links").html(html);
				
				$('.bookmarkedit').click(function(){
					var bID = $(this).attr('id');
					$(this).parent().remove();
					bID = bID.replace("b_","");
					/*************************/
					if(bookmarks.length > 0) {				
						//// Strip bookmark of duplicates
						//bookmarksUnique 	= unique(bookmarks);
						bookmarks.splice(bID,1);						
						var updateCookie	= bookmarks.join(",");
						$.cookie(COOKIE_LINKS, updateCookie, options);			
						//return bookmarksUnique;
					}					
				});				
			}
		} else {
				editBookmarks = false;		
				displayBookMarks();
		}
		return false;
	});
	function makeBookmarLinksReadable(pageN){
		var p1			= pageN.replace(base_url,"");
		var p2			= p1.replace("#","");
		var p3			= p2.replace("-"," ");				
		var p3B			= p3.replace("-"," ");
		var p3C			= p3B.replace("/"," / ");
		var pageName	= capitalizeMe(p3C);
		return pageName;	
	}
	/* !Home Page Ajax Function */		
	$('#volume_tab').live('click',  function(){	homePageAjax('volume');	return false;	});
	$('#price_tab').live('click',   function(){	homePageAjax('price');	return false;	});
	$('#retail_tab').live('click',	function(){	homePageAjax('retail');	return false;	});
	$('#volume_link').live('click',	function(){	homePageAjax('volume');	return false;	});
	$('#price_link').live('click',	function(){	homePageAjax('price');	return false;	});
	$('#retail_link').live('click',	function(){	homePageAjax('retail');	return false;	});
	//
	function homePageAjax(page){
			var datetime	= new Date();
			var url = base_url + "ajax/"+page+"/text.html?time="+datetime;		
			var p1 = page;
			loadchart(p1);	
			$("#loadajaxchart").html("<div id='tabs_home_content'><div id='tabs_home_content_inner'><img id='ajaxloader' src='"+base_url+"assets/gifs/ajax-loader.gif' /></div></div>");
			$.get(url, function(html) { 			
				$("#loadajaxchart").html(html);
        });            			 
	}
	function loadchart(page){
        switch(page)
        {
            case 'volume':
                $('#volume_tab a').addClass('selected');
                $('#price_tab a').removeClass('selected');
                $('#retail_tab a').removeClass('selected');
                break;
            case 'price':
                $('#volume_tab a').removeClass('selected');
                $('#price_tab a').addClass('selected');
                $('#retail_tab a').removeClass('selected');
                break;
            case 'retail':
                $('#volume_tab a').removeClass('selected');
                $('#price_tab a').removeClass('selected');
                $('#retail_tab a').addClass('selected');
                break;
            default:
                break;
        }        
    }
    /* !Contact Form */
    if($('#contactSubmit')) {
    	$('#contactSubmit').click(function(){
    		var runValidation	= true;	    			    		
    		if(checkBlank('#contactFirstName') == false){
    			runValidation = false;
    		}
			if(checkBlank('#contactLastName')== false){
    			runValidation = false;
    		}
			if(checkBlank('#contactEmail')== false){
    			runValidation = false;
			}
    		if(runValidation == false) {
    			return false;
    		} else {
    			var firstName		= $('#contactFirstName').val();
    			$.cookie('first_name', firstName, options);	    			
    			var lastName		= $('#contactLastName').val();
    			$.cookie('last_name', lastName, options);		    			    			
    			var emailAddress	= $('#contactEmail').val(); 
    			$.cookie('email_address', emailAddress, options);		    			
    			var orginization	= $('#contactOrginization').val();
    			$.cookie('orginization', orginization, options);		    			
    			var address			= $('#contactAddress').val();
    			$.cookie('address', address, options);		    			
    			var city				= $('#contactCity').val();
    			$.cookie('city', city, options);		    			
    			var state			= $('#contactState').val();
    			$.cookie('state', state, options);		    			
    			var county			= $('#conactCountry').val();
    			$.cookie('county', county, options);		    			
    			var zip				= $('#contactZip').val();
    			$.cookie('zip', zip, options);		    			
    			var comments		= $('#contactComments').val();
    			$.cookie('comments', comments, options);
    			return true;		    			
    		}
    	});
    }    
    if($('#contact_information')){
    	var html				=
    		"<p><strong>First Name: </strong>"			+ $.cookie('first_name') 		+
    		"<br /><strong>Last Name: </strong>"		+ $.cookie('last_name')			+
    		"<br /><strong>Email Address: </strong>"	+ $.cookie('email_address');
    		
    	if($.cookie('orginization') != null){
    		html += "<br /><strong>Orginization: </strong>"	+ $.cookie('orginization');    		
    	}
    	if($.cookie('address') != null){
    		html += "<br /><strong>Address: </strong>"	+ $.cookie('address');
    	}
    	if($.cookie('city') != null){
			html += "<br /><strong>City: </strong>"		+ $.cookie('city');    	
    	}
    	if($.cookie('state') != null){
    		html += "<br /><strong>State: </strong>"		+ $.cookie('state');
    	}
    	if($.cookie('county') != null){
    		html += "<br /><strong>Country: </strong>"		+ $.cookie('country');
    	}
    	if($.cookie('zip') != null){
    		html += "<br /><strong>Zip: </strong>"		+ $.cookie('zip');
    	}
    	if($.cookie('comments') != null){
    		html += "<br /><strong>Comments: </strong>"		+ $.cookie('comments');
    	}
    	html		+= "</p>";
    	$('#contact_information').html(html);
    }
    /* !POS ORDER FORM VALIDATION */
    if($('#posFormSumit')){
	    $('#posFormSumit').click(function(){
				var posInputEntered = false;
				$('.posOrderInput').each(function(){
					var inputCheck1	= $(this).val().replace(" ", "");
					if((inputCheck1!='0') && (!isNaN(inputCheck1))){
						posInputEntered = true;
					} 				
				});			
				if(posInputEntered==false){
					alert("You have not selected any POS items to complete this form.");
					return false;
				}				
	    		var runValidation	= true;	    		
	    		if(checkBlank('#firstName') == false) {
	    			runValidation = false;
	    		};
				if(checkBlank('#lastName') == false) {
	    			runValidation = false;
	    		};
				if(checkBlank('#estabAddress') == false) {
	    			runValidation = false;
	    		};
				if(checkBlank('#emailAddress') == false) {
	    			runValidation = false;
	    		};
				if(checkBlank('#estabCity') == false) {
	    			runValidation = false;
	    		};
				if(checkBlank('#estabState') == false) {
	    			runValidation = false;
	    		};
				if(checkBlank('#estabZip') == false) {
	    			runValidation = false;
	    		};
				if(checkBlank('#estabPhone') == false) {
	    			runValidation = false;
	    		};
				if(checkBlank('#estabFax') == false) {
	    			runValidation = false;
	    		};
				if(checkBlank('#estabType') == false) {
	    			runValidation = false;
	    		};	    		
	    		if(runValidation == false) {
	    			return false;
	    		} else {
	    			return true;
	    		}
	    });
    }
	function checkBlank(idName) {
		var validateCheck = true;
		var inputValue= $(idName).val();
		if(inputValue == "" || inputValue == "blank") {
			validateCheck = false;
		}		
		if(idName == "#estabPhone" || idName == "#estabFax") {
			var num = inputValue.replace(/[^\d]/g,'');
			if(num.length < 10) {
				validateCheck = false;
			}
		}		
		if(idName == "#emailAddress"){
			if(!echeck(inputValue)){
				validateCheck = false;
			}
		}
		if(idName == "#contactEmail"){
			if(!echeck(inputValue)){
				validateCheck = false;
			}
		}
		if(idName == "#avoaction_email"){
			if(!echeck(inputValue)){
				validateCheck = false;
			}
		}
		if(!validateCheck){
			$(idName).addClass('validationError');	
		} else {
			$(idName).removeClass('validationError');	
		}
		return validateCheck;
	}
	/* return false; */
	function echeck(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;			
	}	
});