/*------------------------------------------------------------------------ //
   USVI / Film USVI
   -----------------------------------------------------------------------
   Description:  Custom JavaScript for the Film USVI website
   Filename:     custom.js
   ----------------------------------------------------------------------- */

/* ======================================================================= //
   Index of scripts used throughout the site.

   1. MENU
   2. HIGHLIGHT
   3. VALIDATION
   4. EXTRAS
   5. FIXES
   6. LOAD-TIME

   Used in: 
   ======================================================================= */

/* ======================================================================= //
   1. MENU:
   The following code is used to load the common menu throughout the site
   and changes according to the detected page.
   ======================================================================= */

var menu = {
	page: ["","index","locations","locations_landmarks","locations_shore","locations_scenic","locations_other","locations_events","prod_resources","our_islands","our_islands_stcroix","our_islands_stjohn","our_islands_stthomas","accommodations","accommodations_stcroix","accommodations_stjohn","accommodations_stthomas","contact_us","confirmation"],
	section: ["hdr_menu_home","hdr_menu_home","hdr_menu_locations", "hdr_menu_locations", "hdr_menu_locations", "hdr_menu_locations", "hdr_menu_locations", "hdr_menu_locations", "hdr_menu_production", "hdr_menu_islands", "hdr_menu_islands", "hdr_menu_islands", "hdr_menu_islands","hdr_menu_accommodations","hdr_menu_accommodations","hdr_menu_accommodations","hdr_menu_accommodations","hdr_contact","hdr_contact"],
	submenu: ["","","hdr_smenu_locations","hdr_smenu_locations","hdr_smenu_locations","hdr_smenu_locations","hdr_smenu_locations","hdr_smenu_locations","","hdr_smenu_islands","hdr_smenu_islands","hdr_smenu_islands","hdr_smenu_islands","hdr_smenu_accommodations","hdr_smenu_accommodations","hdr_smenu_accommodations","hdr_smenu_accommodations","",""],
	menuitem: ["","","","hdr_smenu_locations_landmarks","hdr_smenu_locations_shore","hdr_smenu_locations_scenic","hdr_smenu_locations_other","hdr_smenu_locations_events","","","hdr_smenu_islands_stcroix","hdr_smenu_islands_stjohn","hdr_smenu_islands_stthomas","","hdr_smenu_accommodations_stcroix","hdr_smenu_accommodations_stjohn","hdr_smenu_accommodations_stthomas","",""],
	init: function() {
		$("#hdr_main").load("includes/menu.html", function() {
			this.now= extras.current();
			
			var i = 0;
			
			while(menu.page[i] != this.now)
				i++;
			
			$("#" + menu.section[i]).css("background-position", "bottom left");
			$("#" + menu.submenu[i]).css("display","block")
			$("#" + menu.menuitem[i]).css("background-position", "bottom left");
			
			$("#hdr_menu ul li ul li a").mouseout(function(){
				if($(this).attr("id") != $("#" + menu.menuitem[i]).attr("id"))
					$(this).css("background-position", "top left")
			});
			$("#hdr_menu ul li ul li a").mouseover(function(){
				$(this).css("background-position", "bottom left")
			});
			
			fixes.init();
		});
	}
}

/* ======================================================================= //
   2. HIGHLIGHT:
   The following code is used to load the desired content in the Location
   + Events section pages.
   ======================================================================= */
var highlight = {
	locations: ["locations_landmarks","locations_shore","locations_scenic","locations_other","locations_events"],
	classname: ["lnk_landmarks_active","lnk_sun_active","lnk_rhombus_active","lnk_cross_active","lnk_cross_active"],
	menuname: ["cntr_list_location_other","cntr_list_location_other","cntr_list_location_other","cntr_list_location_other","cntr_list_location_other"],
	init: function() {
		this.now = extras.current();
		
		var i = 0;
		
		while(this.now !=  this.locations[i] && i < this.now.length-1 )
			i++;
		
		$("#" +  this.menuname[i] +" li a").attr("onclick", "javascript:return false;")
		
		var li = $("#" +  this.menuname[i] +" li");
		
		for(i = 0;i < li.length; i++) {
			eval("$(li[" + i + "]).click(function (){javascript:highlight.showhide(" + i + ");});");
		}
	},
	showhide: function(id) {
		var i = 0;
		
		while(this.now !=  this.locations[i] && i < this.now.length-1 )
			i++;
		
		$("#" +  this.menuname[i] +" li a").attr("onclick", "javascript:return false;")
		
		var li = $("#" +  this.menuname[i] +" li");
		
		$("#box_highlight_main div").addClass("test_inactive");
		$("#" +  this.menuname[i] +" li").removeClass();
		
		var div = $("#box_highlight_main div");
		
		this.now = extras.current() 
		
		var j = 0;
		
		while((this.now != this.locations[j]) && (j < this.locations.length-1)){
			j++;
		}
		
		for(i = 0;i < div.length; i++) {
			if(id == i) {
				$(div[i]).removeClass("test_inactive");
				$(li[i]).addClass(this.classname[j]);
			}
			else {
				$(li[i]).addClass("lnk_cross");
			}
		}
	}
}

/* ========================================================================//
   3. VALIDATION:
   The following code validates the contact-us form.
   ======================================================================= */
var validation = {
	init: function() {
		$("#frm_contact").validate({
			rules: {
				txtName: "required",
				txtBody: "required",
				txtSubject: "required",
				txteMail: {
					required: true,
					email: true
				}
			}
		
		});
		
		$("#btn_contact_send").click(function () {
			$("#frm_contact").submit(); 
		});
	}
}

/* ========================================================================//
   4. EXTRAS:
   The following code adds additional functionality to certain pages.
   ======================================================================= */
var extras = {
	
	init: function() {
		this.now = extras.current();
		
		switch(this.now){
			case "":
				swfobject.embedSWF("swf/virginIslands_Home.swf", "cntr_banner", "1024", "390", "9.0.0", "expressInstall.swf");
				break;
			case "index":
				swfobject.embedSWF("swf/virginIslands_Home.swf", "cntr_banner", "1024", "390", "9.0.0", "expressInstall.swf");
				break;
			case "contact_us":
				validation.init();
				break;
			case "locations_events":
				swfobject.embedSWF("swf/virgin_ImagesEvents.swf", "col_locations_left", "550", "390", "9.0.0", "expressInstall.swf");
				highlight.init();
				break;
			case "locations_landmarks":
				swfobject.embedSWF("swf/virgin_ImagesLandmarks.swf", "col_locations_left", "550", "390", "9.0.0", "expressInstall.swf");
				highlight.init();
				break;
			case "locations_other":
				swfobject.embedSWF("swf/virgin_ImagesOthers.swf", "col_locations_left", "550", "390", "9.0.0", "expressInstall.swf");
				highlight.init();
				break;
			case "locations_scenic":
				swfobject.embedSWF("swf/virgin_ImagesTrails.swf", "col_locations_left", "550", "390", "9.0.0", "expressInstall.swf");
				highlight.init();
				break;
			case "locations_shore":
				swfobject.embedSWF("swf/virgin_ImagesSea.swf", "col_locations_left", "550", "390", "9.0.0", "expressInstall.swf");
				highlight.init();
				break;
		}
	},
	current: function() {
			this.page = "" + window.location + "";
			this.page = this.page.split("/");
			this.page = this.page[this.page.length - 1]
			this.page = this.page.split(".");
			this.page = this.page[0];
			
			return this.page;
	}
}

/* ========================================================================//
   5. FIXES:
   The following code fixes certain browser behavior.
   ======================================================================= */
var fixes = {
	init: function() {
		if($.browser.safari)
		{
				$("#hdr_menu ul li ul li").css("float","none");
		}
		
	}
	
}

/* ========================================================================//
   6. LOAD-TIME:
   The following code will be executed as soon as the main page DOM is ready.
   ======================================================================= */

$(function(){
	menu.init();
	extras.init();
});