//Set custom configurations
var config = {
     sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)
     interval: 100, // number = milliseconds for onMouseOver polling interval
     over: megaHoverOver, // function = onMouseOver callback (REQUIRED)
     timeout: 500, // number = milliseconds delay before onMouseOut
     out: megaHoverOut // function = onMouseOut callback (REQUIRED)
};
var lastDiv = "default";
var faqDivlist = "whyvilla difference occupancy nights perperson highlow deposits tax staff services meals costs cellphone internet power currency transport packing hurricanes security payments cancel airfare surcharge";

$("ul#navUL li .sub").css({'opacity':'0'}); //Fade sub nav to 0 opacity on default
$("ul#navUL li").hoverIntent(config); //Trigger Hover intent with custom configurations

function megaHoverOver(){
    $(this).find(".sub").stop().fadeTo('fast', 1).show(); //Find sub and fade it in
    (function($) {
        //Function to calculate total width of all ul's
        jQuery.fn.calcSubWidth = function() {
            rowWidth = 0;
            //Calculate row
            $(this).find("ul").each(function() { //for each ul...
                rowWidth += $(this).width(); //Add each ul's width together
            });
        };
    })(jQuery); 

    if ( $(this).find(".row").length > 0 ) { //If row exists...

        var biggestRow = 0;	

        $(this).find(".row").each(function() {	//for each row...
            $(this).calcSubWidth(); //Call function to calculate width of all ul's
            //Find biggest row
            if(rowWidth > biggestRow) {
                biggestRow = rowWidth;
            }
        });

        $(this).find(".sub").css({'width' :biggestRow}); //Set width
        $(this).find(".row:last").css({'margin':'0'});  //Kill last row's margin

    } else { //If row does not exist...

        $(this).calcSubWidth();  //Call function to calculate width of all ul's
        $(this).find(".sub").css({'width' : rowWidth}); //Set Width

    }
}
//On Hover Out
function megaHoverOut(){
  $(this).find(".sub").stop().fadeTo('fast', 0, function() { //Fade to 0 opactiy
      $(this).hide();  //after fading, hide it
  });
}

function attachOverHandlers(el) {
	for (i=0; i<el.childNodes.length; i++) {
		node = el.childNodes[i];
		if (node.nodeName=="LI") {
			node.onmouseover=function() {this.className+=" over";}
			node.onmouseout=function() {
				this.className=this.className.replace(" over", "");
			}
		}
	}
}
function fixIEHoverId(rootUL) {
	if (document.all && document.getElementById) {
		navRoot = document.getElementById(rootUL);
		attachOverHandlers(navRoot);
	}
}
function fixIEHoverClass(elementClass) {
	if (document.all && document.getElementById) {
		navRoots = $("." + elementClass);
		for (j=0; j< navRoots.length;j++) {
			attachOverHandlers(navRoots[j]);
		}
	}
}
function showFaqAnswer() {
	return function(e) {
		//alert(e.currentTarget.getAttribute('href').split("#")[1].split("_")[0]);
		showFaqDiv(e.currentTarget.getAttribute('href').split("#")[1].split("_")[0]);
		e.preventDefault();
		e.stopPropagation();
		return false;
	}
}
function showFaqDiv(divIDlist) {
/*hides lastDiv and shows divID by removing hidediv class
	 add hidediv class to lastDiv divs to hide from view */
	var hideIDArray = lastDiv.split(" ");
	for (var i=0;i<hideIDArray.length;i++) {
		$('#'+hideIDArray[i]).addClass('hidediv');
	}
	/* remove hidediv class from divIDlist to show */
	var showIDArray = divIDlist.split(" ");
	for (i=0;i<showIDArray.length;i++) {
		$('#'+showIDArray[i]).removeClass('hidediv');
	}
	/* save divIDlist in lastDiv*/
	lastDiv = divIDlist;
}
var specialURL = "http://www.villasinjamaica.com/specials.shtml";
//var specialURL = "localhost/specials.shtml";

function parseSpecialInclude() {
	/*parse and build new list of list elements for display of specials */
	var specialsUL = $('#soSubUL'); // ul of specials
	var specialH3s = $('#rawInclude h3.specialname'); //special names h3
	var specialLabels = new Array;
	var thisLabel = "";
	var SLgroup;
	var SLspecial;
	for (var n=0;n<specialH3s.length;n++) {
		if (thisLabel != specialH3s[n].innerHTML) {
			SLgroup = specialH3s[n].innerHTML.split("*")[1];
			SLspecial = specialH3s[n].innerHTML.split("*")[2];
			specialLabels[specialLabels.length] = "<div class=\"specialgrp\">"+SLgroup+"</div> "+SLspecial;
			thisLabel = specialH3s[n].innerHTML;
		}
	}
	var specials = $('#rawInclude div.special'); // list of specials
	var currentSpecial;
	for (var specialId = 0;specialId < specials.length;specialId++) {// specials loop
		var currentSpecialLI = $('<li  class="subEl" />');//create new special LI
		var newLink = $('<a href="' + specialURL + '#special_' + specialId + '">' + specialLabels[specialId] + '</a>');//create new link to specials page for special
		currentSpecialLI.append(newLink); //append link to special LI
		/*
		var currentVillaAnchors = $('#rawInclude #specialId_'+specialId+' .villadiv a.proppopupv'); // get this special's villa divs
		if (currentVillaAnchors.length >0) {
			var currentVilladivUL = $('<ul class="subList" />');
			for (var vDiv = 0; vDiv<currentVillaAnchors.length; vDiv++) { // process this special's villadivs
				var thisVillaDivLI = $('<li />');
				var myNewAnchor = $(currentVillaAnchors[vDiv]).clone().remove('div.villapic');
				thisVillaDivLI.append(myNewAnchor);
				currentVilladivUL.append(thisVillaDivLI);
			}
			currentSpecialLI.append(currentVilladivUL); // append list of villadivs to specialLI
		}
		*/
		specialsUL.append(currentSpecialLI);
	}
}
function navStartup() {
	parseSpecialInclude();
	$('#rawInclude').remove();
	$('ul.level_2').addClass('sf-vertical');
	
	$('ul#navUL').supersubs({ 
            minWidth:    18,   // minimum width of sub-menus in em units 
            maxWidth:    20,   // maximum width of sub-menus in em units 
            extraWidth:  1     // extra width can ensure lines don't sometimes turn over 
                               // due to slight rounding differences and font-family 
        }).superfish({
		autoArrows : true,
		dropShadows: true	
	}).find('ul').bgIframe({opacity:false});
	
	// remove width:auto from specials links
	$('ul#soSubUL.level_2').css("width","");
	$('ul#soSubUL.level_2 li.subEl a').css("width","");
	
	var soSubElULs = $('ul#soSubUL.level_2 li.subEl ul');
	for (var i=0;i<soSubElULs.length;i++) {
		thisUL = $(soSubElULs[i]);
		if (thisUL[0].childNodes.length <10 ) {
			$(thisUL[0]).css("height","auto");
			$(thisUL[0]).css("width","512px");
			$(thisUL[0]).css("overflow","");
			$(thisUL[0]).css("margin-left","-748px");
		}
		else {
			$(thisUL[0]).css("height","210px");
			$(thisUL[0]).css("width","531px");
			$(thisUL[0]).css("overflow","auto");
			$(thisUL[0]).css("margin-left","-766px");
		}
	}
	$('ul#soSubUL.level_2 li.subEl ul li').css("width","");
	
	// add showAnswer function to qalink onclicks
	$('.qalink').bind("click",showFaqAnswer(),false);
	
	//hide faq answer divs for initial page state
	var dlist = faqDivlist.split(" ");
	for (var i = 0;i<dlist.length;i++) {
		$('div#'+dlist[i]).addClass('hidediv');
	}
}

