function handleDocumentReady() {
	// See individual pages for specifics
	initPage();
}

// ****************************************** Admin

function initAdminCommon() {
	createNewWindowForExternalLinks();
	initializeForms();
	initializeCheckboxes();
}

function initializeCheckboxes(){
	$("#optionsCheckbox").checkbox({checked: "images/icons/chk_on.png", unchecked: "images/icons/chk_off.png"});		
}

// ****************************************** Site

function initSiteCommon() {
	createNewWindowForExternalLinks();
	roundSubNav();
}

function roundSubNav() {		
		$('#subNav > li:eq(0) a').addClass('subNav_first');
		$('#subNav > li:last a').addClass('subNav_last');
}

function topTen() {
	$('#favs').change(function() {
		var id = $(this).attr('value');
		$('.top-10').hide();
		$('#'+id).show();
	});
}

function slideShow() {

    var myArray = []; //declare and initialize array

	$.ajax({
		type: "GET",
		url: "assets/db/media_erl_siva.xml", //xml file 
		dataType: "xml",
		success: function(xml) {
			var count = 0; //counter
			$(xml).find('medium').each(function() {

				var caption = $(this).find('caption').text();
				var src = $(this).find('src').text();

				//append to array
				myArray[parseInt(count)] = new Array(src, '', '', caption); 
				
				count++;
			});
			

			var mygallery2 = new simpleGallery({
				wrapperid: "SS", //ID of main gallery container,
				dimensions: [560, 320], //width/height of gallery in pixels. Should reflect dimensions of the images exactly
				
				imagearray: myArray, //assing the array var to here
				autoplay: [true, 10000, 99], //[auto_play_boolean, delay_btw_slide_millisec, cycles_before_stopping_int]
				persist: true,
				fadeduration: 1000, //transition duration (milliseconds)
				oninit: function() { //event that fires when gallery has initialized/ ready to run
				},
				onslide: function(curslide, i) { //event that fires after each slide
				}
			})
		}
	});


}

// ******************************************* Forms

function initializeForms() {
	$("input.default-value").css("color", inactive_color);
  	$("input.default-value").focus(handleFocus);
  	$("input.default-value").blur(handleBlur);
 }
 
function handleFocus() {
	if (!aFormDefaultValues[this.id]) {
		aFormDefaultValues[this.id] = this.value;
	}
	if (this.value == aFormDefaultValues[this.id]) {
	  this.value = '';
	  this.style.color = active_color;
	}		
}

function handleBlur() {
	if (this.value == '') {
		this.style.color = inactive_color;
		this.value = aFormDefaultValues[this.id];
	}
}


// ******************************************* jQuery Wrappers

function defineTabs(sDivContainer,effect) {
	var oFX;

	switch (effect) {
		default: // case "fade":
			oFX = { opacity: 'toggle' }
			break;
	}
	$("#" + sDivContainer + " > ul").tabs({ fx:  oFX});
}

/**
 * Div appears when checkbox active.
 *
 * @param	cbQuery:String	String passed to jQuery to fetch checkboxes
 * @param	options:Object	{fadeSpeed:Number,slideSpeed:Number}
 *
 * Note: To fetch all checkboxes: cbQuery = "input[type=checkbox]"
 */
function relateDivToCheckbox(cbQuery,options) {
	$(cbQuery).click(function(){
		divClass = $(this).attr("class");
	
	  	if ($(this).is(":checked")) {
		  	$("#" + divClass).fadeIn(options.fadeSpeed);
		}
	  	else if($(this).not(":checked")) {
		  	$("#" + divClass).slideUp(options.slideSpeed);
		}
	});
}

/**
 * Create Flash element
 *
 * Example Usage:
 *
 * var divID = "flashElement";
 * var src = "assets/swf/file.swf"
 * var width = 560;
 * var height = 165;
 * var flashvars = { id:'erl', settings:'assets/db/settings_erl.xml', list:'assets/db/list_erl.xml' }
 */
function flashElement(divID,src,width,height,flashvars) {
	//var base = "http://vwerl-stage.igfarm.com";
	
	var base = "http://" + window.location.hostname;
	
	var oFlash = {
		base:base,
		src:src,
		width: width,
		height:height,
		wmode: 'transparent',
		allowScriptAccess: 'samedomain',
		flashvars:flashvars
	};

	var options = { expressInstall: true };

	$("#" + divID).flash(oFlash,options);
}

function initCarousel () {
	flashElement("vwGroupWidget","assets/swf/carousel.swf",220,190);
}
	
function initPictureViewer () {
	flashElement("imageViewer","assets/swf/imageViewer.swf",560,400,{id:'erl_slideshow',settings:'assets/db/settings_erl_siva.xml'});
}
	
function initVideoPlayer (id) {
	flashElement("videoPlayer","video_erl.swf",560,280,{id:id,settings:'assets/db/settings_video.xml',media:'assets/db/media_video.xml',album:'assets/db/albums_video.xml'});
}

function initTopTenList() {
	flashElement("flashList","assets/swf/selector_list_erl.swf",220,335,{id:'topten',settings:'assets/db/settings_topten.xml',selector:'assets/db/selector_topten_remote.xml'});
}

function initList(id) {
	flashElement("flashList","assets/swf/selector_list_erl.swf",220,300,{id:'places',settings:'assets/db/settings_list.xml',selector:'assets/db/selector_places.xml'});
}

function initTileList(id,list) {
	flashElement("featureViewer","assets/swf/tileList_erl.swf",560,165,{id:id,settings:'assets/db/settings_tilelist.xml',list:list});
}


	function initAccessToggle() {
			
		// Check initial state of access level radio buttons		
		if($("input[name='access_level']:checked").val() == 'user') {
			$('#access_levels').show();
		}
		
		// Bind click event to show or hide user option
		// additional function - when admin clicked... change all access level checkboxes to checked
		$(".access_level").click(function() {
			if ($(this).val() == 'user'){
      	$('#access_levels').show();
	    } else {
	    	$('#access_levels').hide();
	    }
		})	
	}

// ******************************************* Javascript Carousel


	var carouselpage = '';
	
	function initJSCarousel() {
	
				carouselpage		=	$("#projects").parent().attr('class');
		var startPosition 	= getCarouselCookie(carouselpage);
						
		$("#projects").jcarousel({
	      scroll: 1,
	      easing: 'easeOutBack',
	      start: startPosition,
	      itemFirstInCallback: setCarouselCookie
	  });
	}


	function setCarouselCookie(carousel, li, index, state){
		
    // set cookie
    $.cookie(carouselpage+'_cookie', index, { expires: 10 });
		
	}
	
	
	function getCarouselCookie() {		
		if($.cookie(carouselpage+'_cookie') != '') {
			return $.cookie(carouselpage+'_cookie');
		}
		else 
		{
			return 1;
		}
	}



// ******************************************* Utilities (Create a general js file?)

function createNewWindowForExternalLinks() {
	//$("#main a[href^='http://']").not("a[href^='http://vwerl-stage.igfarm.com']").not("a[href^='http://www.vwerl-stage.igfarm.com']").each(
	$("#main a[href^='http://']").not("a[href^='http://vwerl.com']").not("a[href^='http://www.vwerl.com']").each(
	function(){
		if(this.href.indexOf(location.hostname) == -1) { 
			$(this).attr('target', '_blank');
	  	}
	})
}

//****************** Variable Declarations
	
// Global Variables

var aFormDefaultValues = new Array();
var active_color = '#333333'; // Color of user provided text
var inactive_color = '#C7C7C7'; // Color of default text

var carousel_cookie = 'carousel_cookie';
var options = { path: '/', expires: 10 };

// Document Ready
$(document).ready(handleDocumentReady);
