function initLinks() {
	$(".hoverable").hover(
		function () {$(this).addClass('hover');}, 
		function () {$(this).removeClass('hover');}
	);

	$("a[rel='external']").attr('target', '_blank');

}
// function initContactForm() {
// 	$("form#contact_form input, form#contact_form textarea").focus(function() {
// 		if (!$(this).hasClass('focused')) {
// 			$(this).val('');
// 			$(this).addClass('focused');
// 		}
// 	});
// }

function thisMovie(movieName) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
	} else {
		return document[movieName];
	}
}

var FormFields = {
	INPUTS : "form#contact_form input, form#contact_form textarea",
	init: function() {
		$(FormFields.INPUTS).focus(function() {
			if (!$(this).hasClass('focused')) {
				$(this).attr('title', $(this).val());
				$(this).val('');
			}
		});	
		$(FormFields.INPUTS).blur(function() {
			if ($(this).val().replace(/\s*/, '') == '') {
				$(this).val($(this).attr('title'));
			}
			if ($(this).val() != $(this).attr('title')) {
				$(this).addClass('focused');
			}
			
		});	
	}
}

var Calendar = {
	HOLDER : "div#calendar",
	CALENDAR_TABLE : "div#calendar table",
	NEXT_MONTH : "a#next_month_link",
	PREV_MONTH : "a#prev_month_link",
	init: function() {
		$(Calendar.NEXT_MONTH + ',' + Calendar.PREV_MONTH).click(function() {
			var link = this;
			$(Calendar.CALENDAR_TABLE).fadeOut('normal', function() {
				$(Calendar.HOLDER).load($(link).attr('href'), function() {
					Calendar.init();
				});
			});
			return false;
		});	
	}
}
var Searchable = {
	DEFAULT_VALUE : 'Search',
	SEARCH_FIELD : "input#search_input_field",
	init: function() {
		if ($(Searchable.SEARCH_FIELD).val() == '') {
			$(Searchable.SEARCH_FIELD).val(Searchable.DEFAULT_VALUE);
		}
		$(Searchable.SEARCH_FIELD).focus(function() {
			if ($(this).val() == Searchable.DEFAULT_VALUE) {
				$(this).val('');
			}
		});	
		$(Searchable.SEARCH_FIELD).blur(function() {
			if ($(this).val().replace(/\s*/, '') == '') {
				$(this).val(Searchable.DEFAULT_VALUE);
			}
		});	
	}
}
	
function initVideo() {
	if ($("div#video_holder").size() > 0) {
		var first;
		var player_id = "web_tv_player";
		$('ul#videos_list').empty();
		$.getJSON(script_path+"/videos/webtv", {}, function(data){
			var params = {
				allowScriptAccess : "always",
				allowFullScreen : "true"
			};
			var attributes = {};
			var flashvars = {};
			attributes.id = player_id;
			
		 	var list = $('ul#videos_list');
		 	for(var i = 0; i < data.length; i++) {
		 		
		 		var v = data[i];
		 		if (v.Video.image == null) {
		 			v.Video.image = {};
		 			v.Video.image.path = root_path + '/img/common/web_tv_default.png';
		 		}
		 		if (i==0) {
		 			flashvars.image = data[0].Video.image.path;
					flashvars.video = root_dir + data[0].Video.file.path;
		 		}
		 		list.append("<li><span></span><a href=\"#\" onclick=\"loadMovie('" +v.Video.image.path +"','"+root_dir+v.Video.file.path+"'); return false;\">" + v.Video.title+ "</a></li>");
			}
			swfobject.embedSWF(root_path + "/swf/video_component.swf", "video_player", 224, 135, "9.0.0", false, flashvars, params, attributes);
			CSBfleXcroll('video_list_holder');
		});
		
	};	
}

function loadMovie(image, video) {
	var player_id = "web_tv_player";
	
	
	thisMovie(player_id).swapVideo(image, video);
	return false;
}


function loadWebTV(link, url) {
	var video_url = url;
	var image_url = $(link).parent().find('img').attr('src');
	var player_id = "web_tv_player";
	
	thisMovie(player_id).swapVideo(image_url,  video_url);
	return false;
}





function initHeaders() {
	Cufon.replace('.box h2 span', {
		fontWeight : 'bold',
		textTransform : 'uppercase'
	});
	Cufon.replace('#view_program h1, #about_content h1, #search_result h1', {
		fontWeight : 'bold',
		textTransform : 'uppercase'
	});
}

function init() {
//	$('ul#program_list').jScrollPane();
	$('div.box, ul.mini_box_list li, ul#menu').ifixpng()
	initLinks();

	if(navigator.appName != "Microsoft Internet Explorer" && parseInt(navigator.appVersion) < 7) {

	}
	initHeaders();

	
	// initContactForm();
	initVideo();
	Searchable.init();
	FormFields.init();
	Calendar.init();
	//initScroll();
	// initRoundedCorners();
}


$(document).ready(init);