$(document).ready(function () {

	loadPreferences();
	
	$(".form_background").bind('click',function(){
		updateBackground(this.value);		
	});
	
	$(".form_masthead").bind('click',function(){
		updateMasthead(this.value);		
	});
	
	$(".form_photo").bind('click',function(){
		updatePhoto(this.value);		
	});
	
	$("#saveprefs").bind('click', function () {
	
		savePreferences();
	
		if($.cookie('prefs_navclick') !== null) {
			//alert('Your preferences have been saved.');
			window.location.hash = '';
			window.location.reload(true);
			//self.close();
		} else {
			alert('You must enable cookies in order to save your site preferences.');
		}
		return false;
	});
});

function loadPreferences()
{
	if ($.cookie('prefs_navclick') !== null && $.cookie('prefs_navclick') != '') {
		$(".form_navclick:eq(" + $.cookie('prefs_navclick') + ")").attr("checked","checked");
	} else {
		$(".form_navclick:eq(0)").attr("checked","checked");
	}

	if ($.cookie('prefs_accordion') !== null && $.cookie('prefs_accordion') != '') {
		$(".form_accordion:eq(" + $.cookie('prefs_accordion') + ")").attr("checked","checked");
	} else {
		$(".form_accordion:eq(0)").attr("checked","checked");
	}

	if ($.cookie('prefs_search') !== null && $.cookie('prefs_search') != '') {
		$(".form_search:eq(" + $.cookie('prefs_search') + ")").attr("checked","checked");
	} else {
		$(".form_search:eq(0)").attr("checked","checked");
	}

	if ($.cookie('prefs_email') !== null && $.cookie('prefs_email') != '') {
		$(".form_email:eq(" + $.cookie('prefs_email') + ")").attr("checked","checked");
	} else {
		$(".form_email:eq(0)").attr("checked","checked");
	}

	if ($.cookie('prefs_masthead') !== null && $.cookie('prefs_masthead') != '') {
		$(".form_masthead:eq(" + $.cookie('prefs_masthead') + ")").attr("checked","checked");
		updateMasthead($(".form_masthead:eq(" + $.cookie('prefs_masthead') + ")")[0].value);
	} else {
		$(".form_masthead:eq(flash)").attr("checked","checked");
	}

	if ($.cookie('prefs_photo') !== null && $.cookie('prefs_photo') != '') {
		$(".form_photo:eq(" + $.cookie('prefs_photo') + ")").attr("checked","checked");
		updatePhoto($(".form_photo:eq(" + $.cookie('prefs_photo') + ")")[0].value);
	} else {
		$(".form_photo:eq(flash)").attr("checked","checked");
	}

	if ($.cookie('prefs_background') !== null && $.cookie('prefs_background') != '') {
		$(".form_background:eq(" + $.cookie('prefs_background') + ")").attr("checked","checked");
		updateBackground($(".form_background:eq(" + $.cookie('prefs_background') + ")")[0].value);
	} else {
		$(".form_background:eq(0)").attr("checked","checked");
	}
}

function updateBackground(id)
{
	var imageNumber = 0;	

		if(id == 0){
			imageNumber = 1;
		}else if(id == 1){
			imageNumber = 3;
		}else if(id == 2){
			imageNumber = 4;
		}else if(id == 3){
			imageNumber = 5;
		}else if(id == 4){
			imageNumber = 11;
		}else if(id == 5){
			imageNumber = 7;
		}else if(id == 6){
			imageNumber = 8;
		}
		
	$("#page").css('background-image','url(http://virginia.edu/virginia/images/bg/page-'+imageNumber+'.png)');
}

function updateMasthead(id)
{
	if(id == 0) {
		$(".mast").removeClass("alt");
	} else {
		$(".mast").addClass("alt");
	}
	
}

function updatePhoto(id)
{
	var imgSrc = "http://virginia.edu/virginia/images/photos/";
	var showFlash = false;

		if(id == 0){
			// add flash
			showFlash = true;
		}else if(id == 1) {
			imgSrc += "01.jpg";
		}else if(id == 2){
			imgSrc += "nbm2.jpg";
		}else if(id == 3){
			imgSrc += "snowa.jpg";
		}else if(id == 4){
			imgSrc += "health-building.jpg";
		}else if(id == 5){
			imgSrc += "athletics-flags.jpg";
		}else if(id == 6){
			imgSrc += "lawn-fall.jpg";
		}else if(id == 7){
			imgSrc += "cville-icepark.jpg";
		}else if(id == 8){
			imgSrc += "athletics-flag.jpg";
		}else if(id == 9){
			imgSrc += "scenic-books.jpg";
		}
		
	var main = $("#main-photo");
	var selected = $("#selected-photo");

	if(showFlash)	{
		main.show();
		selected.hide();
	} else {
	
		// check for img
		var selectedImage = $("#selected-photo img");
		if(selectedImage.length == 0) {
			// add img tag
			selected.html('<img src="'+imgSrc+'"/>');
		} else {
			selectedImage[0].src = imgSrc;
		}
	
		main.hide();
		selected.show();
	}
}

function closePrefsPanel() { 
	loadPreferences();
	$(".prefs").removeClass("active");
}
