$(document).ready(
function () {
	/*
	var browser = $.browser;
	if ( !browser.mozilla ){
		window.location = "/error-browser-page";
	}
	*/
	
	$("#menu-top ul li a").each(function() {
		if(this.href == window.location || this.href == document.location.protocol + "//" + window.location.hostname + window.location.pathname)
			$(this).parent().addClass("active");
		});
	$("#login-reg ul li a").each(function() {
		if(this.href == window.location || this.href == document.location.protocol + "//" + window.location.hostname + window.location.pathname)
			$(this).parent().addClass("active");
		});
	
	//index.html, add height info 
	var height = equalHeight($('#funcel-v .content'))+31;
	$("#funcel-v").css("height", height);
		
	$("#funcel-v").tabs({fx:{opacity: "toggle"}}).tabs("rotate", 3000, true);   
	$("#funcel-v").hover(function() {  
		$("#funcel-v").tabs("rotate",0,true);  
		return false;
	},  
	function() {  
		$("#funcel-v").tabs("rotate",3000,true);  
		return false;
	});  
		
	$(".menu-tabify").tabs();
	var repalce = $(".tabify-page .menu .active a").text();
	$('.download-page #page-title .sub-title').text(repalce);
	$('.menu-tabify li a').click(function(){
		$('.download-page #page-title .sub-title').text($(this).text());
		
		});
	
	$(".home #column-inner .category:first-child").addClass("first");
	$(".home #column-inner .category:last-child").addClass("last");
	$("#latest-com .content .vid:first-child").addClass("first");
	$("#latest-com .content .vid:last-child").addClass("last");
	$(".sidebar .right:first-child").addClass("first");
	$(".sidebar .right:last-child").addClass("last");
	$("#paging ul li:first-child").addClass("first");
	$("#paging ul li:last-child").addClass("last");
	$("table tr:first-child").addClass("first");
	$("table tr:last-child").addClass("last");
	$("table td:first-child").addClass("first");
	$("table td:last-child").addClass("last");
	
	//search bar pada header
	search_bar("#header #search .field", "search video");
	
	var page_name = $("body").attr("class");
	if (page_name=="home"){
		//only in page index
		$('#mycarousel').jcarousel();
		var total_sum = $("#partner-inner").attr("class");
		if (total_sum < 7 ){
			$('.jcarousel-next').attr("style", "display:none");
			$('.jcarousel-prev').attr("style", "display:none");
		}
	}
	else{
		//other page
		$('.user-manage-video-page .calendar_diary input').filter('.datepicker').datepicker({
			changeMonth : true,
			changeYear : true,
			yearRange : '1945:2015',
			dateFormat : 'yy/mm/dd',
			showOn: "button",
			buttonImage: "/tpl/img/calendar.gif#",
			buttonImageOnly: true
			});
		
		
		$("#datepicker").datepicker({
			changeMonth : true,
			changeYear : true,
			yearRange : '1945:2015',
			dateFormat : 'yy/mm/dd',
			showOn: "button",
			buttonImage: "/tpl/img/calendar.gif#",
			buttonImageOnly: true
			});
	}
	
	$("#notification-registration-success-dialog").dialog({  
		title:'Registration Success',
		show:'puff',  
		hide: 'blind',
		width: 350,
		resizable: false,
		draggable: false,
		modal: true
	});	
	
	$(window).bind("load", function(){
		equalHeight($(".equal_height"));
	});
	
	//add tabify function
	$(".tabify-page").tabs({
		fx:{
			opacity: "toggle"
		}, 
		show: function(event, ui){
			triggerEqualHeight();
		}
	});
	$(".tabify-page #tab1 a").click(function(){
		$(".tabify-page #tab2").removeClass("active");
		$(".tabify-page #tab1").addClass("active");
	});
	$(".tabify-page #tab2 a").click(function(){
		$(".tabify-page #tab1").removeClass("active");
		$(".tabify-page #tab2").addClass("active");
	});
	
	//first load tab, is free-dl tab (second tab)
	var first_tab = $(".tabify-page").attr("first-tab");
	if(first_tab == "free-dl"){
		$(".tabify-page").tabs("option", "selected", 1);
		$(".tabify-page #tab1").removeClass("active");
		$(".tabify-page #tab2").addClass("active");
	}
});

//if link with no redirect action
$('a.nolink').live("click", function(){
	return false;
});

function triggerEqualHeight(){
	$(".equal_height").css("height", "");
	equalHeight($(".equal_height"));
}
function triggerRemoveHeight(){
	$(".equal_height").css("height", "");
}

function equalHeight(group) {
	var tallest = 0;
	var tallestId = "";
	group.each(function() {
		var thisId = $(this).attr("id");
		var thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
			tallestId = thisId;
		}
	});
	if(tallestId != "" && tallestId !== "content-inner"){
		//jika tallestId bukan "#content-innner" dan tidak null maka lakukan equal height
		group.height(tallest);
	}
	return parseInt(tallest);
}

function showFormConfirmPassword(vid,ext,url){
	$("#form-confirm-password-dialog #vid").attr("value", vid);
	$("#form-confirm-password-dialog #ext").attr("value", ext);
	$("#form-confirm-password-dialog #url").attr("value", url);
	
	var password = $( "#form-confirm-password-dialog #password" ), allFields = $( [] ).add( password ), tips = $( ".validateTips" );
	function updateTips( t ) {
		tips.text( t ).addClass( "error" );
		/*setTimeout(function() {
			tips.removeClass( "ui-state-highlight", 1500 );
			}, 500 );*/
	}
	function checkLength( o, n, min ) {
		if ( o.val().length == 0 ) {
			o.addClass( "alert" );
			updateTips( n + " must be filled.");
			return false;
		}
		else if ( o.val().length < min ) {
			o.addClass( "ui-state-error" );
			updateTips( n + " minimmum 6 characters.");
			return false;
		}
		
		else {
			return true;
		}
	}
	
	$("#form-confirm-password-dialog").dialog({
		show:'puff',  
		hide: 'blind',
		width: 350,
		resizable: false,
		draggable: false,
		modal: true,  
		buttons : {  
			"Submit" : function(){ 
				var bValid = true;
				allFields.removeClass( "alert" );
				bValid = bValid && checkLength( password, "Beos Password", 6 );
				
				if ( bValid ) {
					$(this).dialog('close');
					$("#form-confirm-password").submit();
				}
				},  
			"Cancel" : function(){  
				$(this).dialog('close');
				allFields.val( "" ).removeClass( "alert" );
				tips.text( "" ).removeClass( "error" );
			}
			}  
		});
}

function show_menu(){
	var value = "#block_" + document.getElementById('category').value;
	if(value != "#block_"){
		$(".sub_cat").html( $(value).html() );
		}else{
		$(".sub_cat").html( "" );
	}
}

//fungsi menampilkan tulisan pesan pada search bar, sebelum ditulis
function search_bar(selector, value_form){
	if($(selector).attr("value")=="")
	{
		$(selector).attr("value", value_form);
	}
	$(selector).focus(function()
	{
		if($(selector).attr("value")==value_form)
		{
			$(selector).attr("value", "");
		}
	});
	$(selector).blur(function()
	{
		if($(selector).attr("value")=="")
		{
			$(selector).attr("value", value_form);
		}
	}); 
}
