$(document).ready(function () {
	
//fungsi menampilkan tulisan pesan pada comment textarea, sebelum ditulis
var charcounter_active;
function comment_textarea(selector_textarea, selector_counter, value_form, comment_length){
	if($(selector_textarea).attr("value")==""){
		$(selector_textarea).attr("value", value_form);
	}
	
	charcounter_active = false;
	var max_char_msg = comment_length + " characters left";
	$(selector_counter).html(max_char_msg);
	
	$(selector_textarea).focus(function(){
		if(!charcounter_active && $(selector_textarea).attr("value")==value_form){
			$(selector_textarea).attr("value", "");			
			$(selector_textarea).charCounter(comment_length, {
				container: selector_counter,
				format: "%1 characters left"
			});
			charcounter_active = true;
		}
	});
	$(selector_textarea).blur(function(){
		if($(selector_textarea).attr("value")==""){
			$(selector_textarea).attr("value", value_form);
			$(selector_textarea).charCounter(comment_length, {
				container: selector_counter,
				format: max_char_msg
			});
			charcounter_active = false;
		}
	}); 
}
//fungsi untuk mendapatkan info aktifnya charCounter, 
//jika charCounter tidak aktif maka yang sedang aktif adalah tulisan value_form yang di pass oleh user
//dengan kata lain di dalam text area tidak ada tulisan/user belum memasukan tulisan sama sekali
function getCharCounter(){
	return charcounter_active;
}
	
// ???????????????????????????????????????????????????? watch-page javascript?????????????????????????????????????????? //
//perform countdown on text area
comment_textarea(".watch-page #form-give-comment #comment", ".watch-page .form-comment .counter_info", "New Comment . . .", 500);

$('.watch-page .just-visit').hover(function(){
	$(this).find('li').animate({left:'120px'},{queue:false,duration:300});
	}, function(){
	$(this).find('li').animate({left:'0px'},{queue:false,duration:300});
}); 
$('.watch-page .just-visit2').hover(function(){
	$(this).find('li').animate({right:'120px'},{queue:false,duration:300});
	}, function(){
	$(this).find('li').animate({right:'0px'},{queue:false,duration:300});
}); 	

//load comment video at first init
var video_id = $(".watch-page .comment #video_id").attr('vid_id');
load_comment(1, video_id);

//give comment form ajax action
$(".watch-page #form-give-comment").submit(function(event) {
	var vid_id = $(".watch-page .form-comment form .videoId").attr('value');
	var username = $(".watch-page .form-comment form #username").attr('value');
	var comment = $(".watch-page .form-comment form #comment").attr('value');
	if (checkLengthComment(comment.length, 500)){					
		// Send the data using post and put the results in a div
		$.post( "/video_comment_block.php", { "page": 1, "video_id": vid_id, "action": "add_comment", "comment": comment, "username": username },
			function( content ) {
				$( "#vid-comment" ).html( content );
			}
		);
		$(".watch-page .form-comment form #comment").attr('value', '');
		load_comment(1, vid_id);
		$('.watch-page .form-comment .error_length').hide();
		$('.watch-page .form-comment .error_empty').hide();
	}else{
		//add comment suspend
		event.preventDefault(); 
	}
	return false;
});

//ajax video function(s)
//form add favourite action post ajax
$(".watch-page #form-add-fav").submit(function(event) {
	event.preventDefault(); 
	var username = $(".watch-page .form-comment form #username").attr('value');
	var vid_id = $(".watch-page .form-comment form .videoId").attr('value');
	// Send the data using post and put the results in a div
	$.post( "/watch_video_action.php", { "action": "add-fav", "video_id": vid_id, "username": username },
		function( content ) {
			$( ".watch-page #watch-content-inner .info-msg" ).html( content );
		}
	);
});

//form flag video action post ajax
$(".watch-page #form-flag-vid").submit(function(event) {
	event.preventDefault(); 
	var vid_id = $(".watch-page .form-comment form .videoId").attr('value');
	var username = $(".watch-page .form-comment form #username").attr('value');
	// Send the data using post and put the results in a div, make a form
	$.post( "/watch_video_action.php", { "action": "flag-vid", "sub-action": "get-reason"},
		function( content ) {
			$( ".watch-page #watch-content-inner .info-msg" ).html( content );
			$( ".watch-page #form-flag-reason" ).dialog({
				height: 140,
				width: 350,
				modal: true,
				buttons: {
					"Flag this video": function() {
						reason = $(".watch-page #form-flag-reason #reason").attr('value');
						// Send the data using post and put the results in a div
						$.post( "/watch_video_action.php", { "action": "flag-vid", "sub-action": "set-flag", "video_id": vid_id, "username": username, "reason": reason},
							function( content ) {
								$( ".watch-page #watch-content-inner .info-msg" ).html( content );
							}
						);
						$( this ).dialog( "close" );
					},
					Cancel: function() {
						$( this ).dialog( "close" );
					}
				},
				close: function() {
				}
			});
		}
	);
});

//form tell friend action post ajax
var counter = 0;
$(".watch-page #form-tell-friend").submit(function(event) {
	counter++;
	event.preventDefault(); 
	vid_id = $(".watch-page .form-comment form .videoId").attr('value');
	username = $(".watch-page .form-comment form #username").attr('value');
	
	form = "#form-send-message_"+counter;
	
	// Send the data using post and put the results in a div, make a form
	$.post( "/watch_video_action.php", { "action": "tell-friend", "sub-action": "get-form", "form-id" : counter},
		function( content ) {
			$( ".watch-page #watch-content-inner .info-msg" ).html( content );
			$(".watch-page "+ form).dialog({
				height: 240,
				width: 450,
				modal: true,
				buttons: {
					"Confirm": function() {
						friend = $(form + " #message_receiver").attr('value');
						message = $(form + " #message_content").attr('value');
						$( this ).dialog( "close" );
						$.post( "/watch_video_action.php", { "action": "tell-friend", "sub-action": "send-message", 
						"video_id": vid_id, "username": username, "friend": friend, "message": message},
							function( content ) {
								$( ".watch-page #watch-content-inner .info-msg" ).html( content );
								$( ".watch-page #result-send-message" ).dialog({
									modal: true
								});
							}
						);
					},
					Cancel: function() {
						$( this ).dialog( "close" );
					}
				},
				close: function() {
				}
			});
		}
	);
});

//form vote video action post ajax
$(".watch-page #form-vote-vid").submit(function(event) {
	event.preventDefault();
	vid_id = $(".watch-page .form-comment form .videoId").attr('value');
	username = $(".watch-page .form-comment form #username").attr('value');
	// Send the data using post and put the results in a div
	$.post( "/watch_video_action.php", { "action": "vote-vid", "video_id": vid_id, "username": username },
		function( content ) {
			$( ".watch-page #watch-content-inner .info-msg" ).html( content );
		}
	);
});
//vote hover action
var button_vote_value;
$(".watch-page #form-vote-vid .button").live("mouseover mouseout", function(event) {
	if ( event.type == "mouseover" ) {
		button_vote_value = $(this).attr("value");
		$(this).attr("value", "Vote Here");
	} else if ( event.type == "mouseout" ) {
		$(this).attr("value", button_vote_value);
	}
});

$(".watch-page .load_comment").live("click", function(){
	var video_id = $(this).attr("vid-id");
	var page = $(this).attr("page");
	load_comment(page, video_id);
	return false;
});

//load comment ajax
function load_comment(page, vid_id){
	// Send the data using post and put the results in a div
	$.post( "/video_comment_block.php", { "page" : page, "video_id": vid_id},
		function( content ) {
			$( ".watch-page #vid-comment" ).html( content );
		}
	);
}

function checkLengthComment( len, max ) {
	if ( len == 0 || getCharCounter()==false) {
		$('.watch-page .form-comment .error_length').hide();
		$('.watch-page .form-comment .error_empty').show();
		return false;
	}
	else if ( len > max ) {
		$('.watch-page .form-comment .error_empty').hide();
		$('.watch-page .form-comment .error_length').show();
		return false;
	}
	
	else {
		return true;
	}
}


// ???????????????????????????????????????????????????? user-message-list-page javascript?????????????????????????????????????????? //
$(".user-message-list-page .outbox_sAll").click(function(){
	$('.user-message-list-page .outbox_cbox').attr('checked', 'checked');
	return false;
});

$(".user-message-list-page .outbox_desAll").click(function(){
	$('.user-message-list-page .outbox_cbox').removeAttr('checked');
	return false;
});

$(".user-message-list-page .inbox_desAllInbox").click(function(){
	$('.user-message-list-page .inbox_cbox').removeAttr('checked');
	return false;
});

$(".user-message-list-page .inbox_sAllInbox").click(function(){
	input = $(this).attr('read');
	if (input=='unread'){
		//select all read messages
		$('.user-message-list-page .unread_message .inbox_cbox').attr('checked', 'checked');
	}else{
		//select all messages
		$('.user-message-list-page .inbox_cbox').attr('checked', 'checked');
	}
	return false;
});

$(".user-message-list-page .nav_postFormOutbox").click(function(){
	param = $(this).attr('href');
	$('.user-message-list-page .search_outbox_form form').attr('action', param);
	$('.user-message-list-page .search_outbox_form form').submit();
	return false;
});

$(".user-message-list-page .postFormInbox").click(function(){
	param = $(this).attr('href');
	$('.user-message-list-page .search_inbox_form form').attr('action', param);
	$('.user-message-list-page .search_inbox_form form').submit();
	return false;
});

search_bar("#user-message-content-inner #search .field-inbox", "search message inbox");
search_bar("#user-message-content-inner #search .field-outbox", "search message outbox");

// ???????????????????????????????????????????????????? user-token-page javascript?????????????????????????????????????????? //
var first_load_form = $(".user-token-page").attr("arg");
show_form_token_page(first_load_form);

$(".user-token-page .show_token_form").change(function(){
	show_form_token_page();
});

$(".user-token-page .converting_token").change(function(){
	var target = $(this).attr("conv-target");
	var conv = $(this).attr("conv-val-tok");
	var source_val = $(this).val();
	converting_token_token_page(source_val,target,conv);
});

function show_form_token_page(first_load_form){
	var value = $('.user-token-page #select_token').val();
	
	if (first_load_form != null){
		value = first_load_form;
	}
	
	if (value == "Bank Payment"){
		//bank payment menu chosed
		$(".user-token-page #select-token :selected").removeAttr("selected");
		$('.user-token-page #select-token').val('Bank Payment');
		$(".user-token-page #t-cash-form").hide();
		$(".user-token-page #bank-payment-form").show();
	}else{
		//t-cash payment menu chosed
		$(".user-token-page #select-token :selected").removeAttr("selected");
		$('.user-token-page #select-token').val('ectronic Transaction - T-Cash');
		$(".user-token-page #bank-payment-form").hide();
		$(".user-token-page #t-cash-form").show();
	}
}

function converting_token_token_page(source_val,target,conv){
	var new_value = source_val*conv;
	$(target).attr({value:new_value});
}


// ???????????????????????????????????????????????????? user-message-view-page javascript?????????????????????????????????????????? //
$(".user-message-view-page .showReply").click(function(){
	$(".user-message-view-page .show_reply_button").hide('slow');
	$(".user-message-view-page .form-message").show('slow', triggerEqualHeight);
	return false;
});

$(".user-message-view-page .form-message .reset").click(function(){
	$(".user-message-view-page .form-message").hide('slow');
	$(".user-message-view-page .show_reply_button").show('slow', triggerEqualHeight);
	return false;
});


// ???????????????????????????????????????????????????? user-invoice-page javascript?????????????????????????????????????????? //
//first init form
var first_load_form = $(".user-invoice-page").attr("first_load");
var invc_num = $(".user-invoice-page").attr("invc_num");
var invc_amt = $(".user-invoice-page").attr("invc_amt");
var init = $(".user-invoice-page").attr("init");
if(init==1){
	init_form(first_load_form, invc_num, invc_amt);
}

$(".user-invoice-page .show_payment_form").click(function(){
	invc_num = $(this).attr("invc_num");
	invc_amt = $(this).attr("invc_amt");
	show_payment_form(invc_num, invc_amt);
	return false;
});

$(".user-invoice-page .hide_payment_form").click(function(){
	hide_payment_form();
});

$(".user-invoice-page #select-token").change(function(){
	show_token_form();
	return false;
});

function init_form(first_load_form, invc_num, invc_amt){
	show_payment_form(invc_num, invc_amt);
	show_token_form(first_load_form);
}

function show_token_form(first_load_form){
	var value = $('.user-invoice-page #select-token').attr('value');
	
	if (first_load_form != null){
		value = first_load_form;
	}
	
	if (value == "Bank Payment"){
		//bank payment menu chosed
		$(".user-invoice-page #select-token :selected").removeAttr("selected");
		$('.user-invoice-page #select-token').val('Bank Payment');
		$(".user-invoice-page #t-cash-form").hide();
		$(".user-invoice-page #bank-payment-form").show();
	}else{
		//t-cash payment menu chosed
		$(".user-invoice-page #select-token :selected").removeAttr("selected");
		$('.user-invoice-page #select-token').val('ectronic Transaction - T-Cash');
		$(".user-invoice-page #bank-payment-form").hide();
		$(".user-invoice-page #t-cash-form").show();
	}
}

function show_payment_form(invc_num, invc_amt){
	$(".user-invoice-page #invoice-info").hide();
	$(".user-invoice-page #table-form").show();
	show_token_form("ectronic Transaction - T-Cash");
	$('.user-invoice-page .invc_num').attr({value : invc_num});
	$('.user-invoice-page .invc_amt').attr({value : invc_amt});
}

function hide_payment_form(){
	$(".user-invoice-page #table-form").hide();
	$(".user-invoice-page #invoice-info").show();
}


// ???????????????????????????????????????????????????? user-manage-video-page javascript?????????????????????????????????????????? //
var global_cbox = 0;

//submit button validation, if none of check boxes is checked
$(".user-manage-video-page #video-container").submit(function(event) {
	if(global_cbox==0){
		event.preventDefault();
		$('.user-manage-video-page .alert_video_cbox').show();
	}else{
		$('.user-manage-video-page .alert_video_cbox').hide();
	}
});

$(".user-manage-video-page #video-container .delete").click(function() {
	if(global_cbox==0){
		$('.user-manage-video-page .alert_video_cbox').show();
	}else{
		$('.user-manage-video-page .alert_video_cbox').hide();
		
		//show confirmation dialog
		$(".user-manage-video-page .delete_confirmation").dialog({
			resizable: false,
			modal: true,
			buttons: {
				"Yes": function() {
					$( this ).dialog( "close" );
					$(".user-manage-video-page #video-container .action_type").val("delete");
					$(".user-manage-video-page #video-container").submit();
				},
				"No": function() {
					$( this ).dialog( "close" );
				}
			}
		});
	}
	return false;
});

$(".user-manage-video-page .post_form").click(function() {
	goto_page = $(this).attr("page");
	var form = 'video-container';
	var new_action = $('#'+form).attr('action') + "/page/" + goto_page;
	$('.user-manage-video-page #'+form).attr({action : new_action});
	document.forms[form].submit();
	return false;
});

$(".user-manage-video-page .cbox_desAll").click(function() {
	global_cbox = 0;
	$('.user-manage-video-page .cbox').removeAttr('checked');
	$('.user-manage-video-page .content-vid-menu').hide('fast');
});

$(".user-manage-video-page .cbox_sAll").click(function() {
	global_cbox = 4;
	$('.user-manage-video-page .cbox').attr('checked', 'checked');
	$('.user-manage-video-page .content-vid-menu').show('fast');
});	

$(".user-manage-video-page .show_calendar_diary").click(function() {
	var video_id = $(this).attr("vid_id");
	var calendar_id = ".user-manage-video-page #calendar_" + video_id;
	var diary_id = ".user-manage-video-page #diary_link_" + video_id;
	$(calendar_id).show("fast");
	$(diary_id).hide("fast");
	return false;
});	

$(".user-manage-video-page .clear_calendar_diary").click(function() {
	var video_id = $(this).attr("vid_id");
	var diarybox_class = ".user-manage-video-page .diarybox_" + video_id;
	var calendar_id = ".user-manage-video-page #datepicker_" + video_id;
	$(diarybox_class).attr({value: ''});
	$(calendar_id).attr({value : ''});
	return false;
});

$(".user-manage-video-page .cbox_showVideoMenu").change(function() {
	var cbox_attr = $(this).attr('checked');
	var cbox_val = $(this).val();
	var target = ".user-manage-video-page #content-vid-menu_" + cbox_val;
	if(cbox_attr == 'checked'){
		//show
		global_cbox++;
		$(target).show('fast');
	}else{
		//hide
		global_cbox--;
		$(target).hide('fast');
	}
});

$(".user-manage-video-page .update_checklist_calendar_diary").change(function() {
	var video_id = $(this).attr("vid_id");
	var diarybox_class = ".user-manage-video-page .diarybox_" + video_id;
	var calendar_id = ".user-manage-video-page #datepicker_" + video_id;
	var date = $(calendar_id).attr('value');
	$(diarybox_class).attr({value: date});
});


// ???????????????????????????????????????????????????? profile-page javascript (beoscopers-profile-page & user-profile-page) ?????????????????????????????????????????? //
//function check length status
function checkLengthStatus( len, max ) {
	if ( len == 0 || getCharCounter()==false) {
		$('.profil-page .comment-form .error_length').hide();
		$('.profil-page .comment-form .error_empty').show();
		return false;
	}
	else if ( len > max ) {
		$('.profil-page .comment-form .error_empty').hide();
		$('.profil-page .comment-form .error_length').show();
		return false;
	}
	else {
		return true;
	}
}

//perform countdown on text area
comment_textarea(".profil-page .comment-form #comstat-detail", ".profil-page .comment-form .counter_info", "New Comment . . .", 500);

//perform check status comment validation
$(".profil-page .comment-form #add-comstat-form").submit(function(event) {
	comment = $(".profil-page .comment-form #comstat-detail").attr('value');
	if (checkLengthStatus(comment.length, 500)){					
		// submit form
		return true;
	}else{
		//add comment suspend
		event.preventDefault(); 
		return false;
	}
});

//get profile page informatino
var current_page = $(".profil-page").attr("page");

//beoscopers-profile-page onlye
if(current_page=="beoscopers-profile-page"){
	equalHeight($('.equal_height'));
}

var val_stat = $('.profil-page .status-val .fill').text();
$('.profil-page .status-val input').live('click', function(){
	$('.profil-page .status-val').hide();
	$('.profil-page .formstat').removeAttr('style');
	$('.profil-page .formstat textarea').val('');
	$('.profil-page .formstat textarea').focus();

});

$(".profil-page .formstat textarea").focus(function()
{
	if($(this).val()==val_stat)
	{
		$(this).val("");
	}
});
$(".profil-page .formstat textarea").blur(function()
{
	if($(this).val()=="")
	{
		$('.profil-page .status-val').show();
		$('.profil-page .formstat').hide();
		
	}
}); 

function equalHeight(group) {
	var tallest = 0;
	group.each(function() {
		var thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	group.height(tallest);
}

function loading_show(selector, document_action_type, document_target, animation_speed){
	if(document_action_type == "append"){
		if(!animation_speed){
			$(selector).append(document_target).fadeIn(animation_speed);
		}else{
			$(selector).append(document_target);
		}
	}else if(document_action_type == "prepend"){
		if(!animation_speed){
			$(selector).prepend(document_target).fadeIn(animation_speed);
		}else{
			$(selector).prepend(document_target);
		}
	}else if(document_action_type == "html"){
		if(!animation_speed){
			$(selector).html(document_target).fadeIn(animation_speed);
		}else{
			$(selector).html(document_target);
		}
	}
}

function viewAll(stat){
	loading_show(".profil-page .comment-inner .total-comstat a", "html", "<img src='/tpl/flash/ajax-loader2.gif#' />", "slow");
	
	//beoscopers-profile-page only
	if(current_page=="beoscopers-profile-page"){
		var username = $('.beoscopers-profile-page #lastn').attr("usrnm");
		$.ajax
		({	
			type: "GET",
			url: "/beoscopers-profile-page.php",
			data: "act="+stat+"&username="+username,
			success: function(msg)
			{
				$(".profil-page .comment-inner").ajaxComplete(function(event, request, settings)
				{
					$(".beoscopers-profile-page .comment-inner").html(msg);
					$(".beoscopers-profile-page .equal_height").removeAttr("style");
					equalHeight($('.beoscopers-profile-page .equal_height'));
				});
			}
		});
	}else{
		$.ajax({	
			type: "GET",
			url: "/user-profile-page.php",		
			data: "act="+stat,
			success: function(msg)
			{
				$(".user-profil-page .comment-inner").ajaxComplete(function(event, request, settings)
				{
					$(".user-profil-page .comment-inner").html(msg);
					$(".user-profil-page .equal_height").removeAttr("style");
					equalHeight($(".user-profil-page .equal_height"));
				});
			}
		});
	}
}

function loadData(page){
	var loadHeight = $(".profil-page .my-video .list").height();
	var htmlvid = "<div id='load' class='loading'><img src='/tpl/flash/load.gif#' /></div>";
	loading_show(".profil-page .my-video .list", "append", htmlvid);
	$("#load").height(loadHeight);
	
	if(current_page=="beoscopers-profile-page"){
		var username = $('.beoscopers-profile-page #lastn').attr("usrnm");
		$.ajax
		({	
			type: "GET",
			url: "/beoscopers-profile-page.php",	
			data: "page="+page+"&username="+username,
			success: function(msg)
			{
				$(".beoscopers-profile-page .my-video table").ajaxComplete(function(event, request, settings)
				{
					$(".beoscopers-profile-page .my-video table").html(msg);
					$(".beoscopers-profile-page .my-video .list ").removeAttr("style");
					$('.beoscopers-profile-page .my-video .list #load').remove();
				});
			}
		});
	}else{
		
		$.ajax({	
			type: "GET",
			url: "/user-profile-page.php",		
			data: "page="+page,
			success: function(msg2)
			{
				$(".user-profil-page .my-video table").ajaxComplete(function(event, request, settings)
				{
					$(".user-profil-page .my-video .user-vid").html(msg2);
					$('.user-profil-page .my-video .list').removeAttr('style');
					$('.user-profil-page .my-video .list #load').remove();
					
				});
			}
		});
	}
}

function viewVideo(vid){
	loading_show(".profil-page .my-video #watch-content-inner", "html", "<div id='load2' class='loading'><img src='/tpl/flash/load.gif#' /></div>", "slow");
	
	if(current_page=="beoscopers-profile-page"){
		var username = $('.beoscopers-profile-page #lastn').attr("usrnm");
		$.ajax({	
			type: "GET",
			url: "/watch-page.php",
			data: "vid_id="+vid+"&username="+username,
			success: function(msg){
				$(".beoscopers-profile-page .my-video #watch-content-inner").ajaxComplete(function(event, request, settings){
					$(".beoscopers-profile-page .my-video #watch-content-inner").html(msg);
					$(".beoscopers-profile-page .equal_height").removeAttr("style");
					equalHeight($(".beoscopers-profile-page .equal_height"));
				});
			}
		});
	}else{
		$.ajax({	
			type: "GET",
			url: "/watch-page.php",		
			data: "vid_id="+vid,
			success: function(msg){
				$(".profil-page .my-video #watch-content-inner").ajaxComplete(function(event, request, settings){
					$(".profil-page .my-video #watch-content-inner").html(msg);
					$(".profil-page .equal_height").removeAttr("style");
					equalHeight($(".profil-page .equal_height"));
				});
			}
		});
	}
}

$('.profil-page .comment-inner .total-comstat').live('click', function(){
	var stat = $(this).attr('title');
	viewAll(stat);
		
});	

$('.profil-page .my-video .pic a').live('click', function(){
	var vid = $(this).attr('vid');
	viewVideo(vid);	
	
});


var noprev = parseInt($(".profil-page .prev-page .prev a").attr("p"))+ 1;
if(noprev = 1){
	$('.profil-page .prev-page .prev').attr("class", "dissmiss");
	$('.profil-page .prev-page .dissmiss').removeAttr("style");
}

$('.profil-page #paging #link .next').live('click', function(){
	var page = $(this).attr('p');
	loadData(page);
	
	var floorvid = parseInt($(".profil-page #lastn").text());
	var nonext = parseInt(page);
	var avaprev = $('.profil-page .prev-page .dissmiss').attr('class');
	if(floorvid==nonext){
		$('.profil-page .next-page .next').attr("class", "dissmiss");
		$('.profil-page .next-page .dissmiss').removeAttr("style");
		$('.profil-page .next-page .dissmiss').attr("p", parseInt(page)+1);
	}
	if(avaprev == 'dissmiss')
	{
		$('.profil-page .prev-page .dissmiss').attr("class", "prev");
		$('.profil-page .prev-page .prev').attr("style", "cursor:pointer");
	}
	
	var counter1 = parseInt($(".profil-page #paging #link .prev").attr("p"))+ 1;
	var counter2 = parseInt($(".profil-page #paging #link .next").attr("p"))+ 1;
	$('.profil-page #paging #link .prev').attr("p", counter1);
	$('.profil-page #paging #link .next').attr("p", counter2);
}); 

$('.profil-page #paging #link .prev').live('click', function(){
	var page = $(this).attr('p');
	loadData(page);
	
	var floorvid = parseInt($(".profil-page #lastn").text());
	var avanext = $('.profil-page .next-page .dissmiss').attr('class');
	var noprev = parseInt(page);
	if(noprev == 1)
	{
		$('.profil-page .prev-page .prev').attr("class", "dissmiss");
		$('.profil-page .prev-page .dissmiss').removeAttr("style");
		$('.profil-page .prev-page .dissmiss').attr("p", "0");
		$('.profil-page .next-page .next').attr("p", "3");
	}
	if(avanext == 'dissmiss')
	{
		$('.profil-page .next-page .dissmiss').attr("class", "next");
		$('.profil-page .next-page .next').attr("style", "cursor:pointer");
	}
	
	var counter1 = parseInt($(".profil-page #paging span .prev").attr("p"))- 1;
	var counter2 = parseInt($(".profil-page #paging span .next").attr("p"))- 1;
	$('.profil-page #paging #link .prev').attr("p", counter1);
	$('.profil-page #paging #link .next').attr("p", counter2);
});
equalHeight($(".profil-page .equal_height"));
var def = $('.profil-page .my-video #lastn').attr('def');
viewVideo(def);

//beoscopers-profile-page
if(current_page=="beoscopers-profile-page"){
	//catch and display alert from ajax add friends
	function response_alert_ajax(){
		var alert_msg = $(".beoscopers-profile-page #page-title .relation_info .alert").html();
		if(alert_msg!=""){
			$(".beoscopers-profile-page #content-inner #user-profil-inner .draw").html(alert_msg);
			$(".beoscopers-profile-page #content-inner #user-profil-inner .draw").show("fast");
		}else{
			$(".beoscopers-profile-page #content-inner #user-profil-inner .draw").hide("fast");
		}
	}
	
	//friend button hover action
	var lock_button = false;
	$(".status_ok").live("mouseover mouseout mousedown", function(event) {
		if(!lock_button){					
			if ( event.type == "mouseover" ) {
				$(this).html("remove friend");
			} else if ( event.type == "mouseout" ) {
				$(this).html("already friend");
			} else if ( event.type == "mousedown" ) {
				lock_button = true;
			}
		}
	});
	
	$(".requestConfirmation").live("click", function(){
		var username = $(this).attr("user");
		var friendship_id = $(this).attr("frdid");
		showRequestConfirmationDialog(username, friendship_id);
		return false;
	});
	
	$(".friendButton").live("click", function(){
		var username = $(this).attr("user");
		var friendship_id = $(this).attr("frdid");
		var action = $(this).attr("act");
		
		if(action == "remove"){
			$( "#dialog-confirmation-remove" ).dialog({
				close: function(){
					//set button name to default
					$(".status_ok").html("already friend");
					lock_button = false;
				},
				resizable: false,
				height:200,
				modal: true,
				buttons: {
					"Remove From Friend": function() {
						friendButtonAction(username, friendship_id, action);
						$( this ).dialog( "close" );
					},
					"Cancel": function() {
						$( this ).dialog( "close" );
					}
				}
			});
		}else{
			friendButtonAction(username, friendship_id, action);
		}
		return false;
	});

	//invite friend ajax
	function friendButtonAction(username, friendship_id, action){
		// Send the data using post and put the results in a div
		$.post( "/beoscopers-profile-page.php", { "add-friend" : true, "username": username, "friendship_id": friendship_id, "action": action},
			function( content ) {
				$( ".add-friend" ).html( content );
				response_alert_ajax();
			}
		);
	}
	
	function showRequestConfirmationDialog(username, friendship_id){
		$( "#dialog-confirmation" ).dialog({
			resizable: false,
			height:200,
			modal: true,
			buttons: {
				"Approve Request": function() {
					friendButtonAction(username, friendship_id, "request_approve");
					$( this ).dialog( "close" );
				},
				"Reject Request": function() {
					friendButtonAction(username, friendship_id, "request_reject");
					$( this ).dialog( "close" );
				}
			}
		});
	}
}


// ???????????????????????????????????????????????????? help-page javascript ?????????????????????????????????????????? //
function displayAjax(arg, ind){
	// Send the data using post and put the results in a div
	$.ajax
	({
		type: "POST",
		url: "/help-page.php",		
		data: "request="+arg+"&indeks="+ind,
		beforeSend: function(){
			loading_show(".help-page #page-help-content", "prepend", 
			"<div id='loading-bar'><span class='loading-bar-ajax'>"+
			"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+
			"</span></div>");
			$(".help-page #display-help").hide('slow');
		},
		success: function(msg)
		{	
			$('.help-page #page-help-content #loading-bar').remove();
			$(".help-page #display-help").html(msg).show('slow');	
			$(".help-page #display-help").promise().done(function(){
				triggerEqualHeight()
			});
		}

	});	
}			
function oneDisplayHelp(arg) {
	//one display help function, call ajax help-content
	//without indexing previous and next.
	$.ajax
	({
		type: "POST",
		url: "/help-page.php",
		data: "request="+arg+"&indeks=-1",
		beforeSend: function(){
			$('#help-modal').dialog({
				show:'puff',  
				hide: 'blind',
				resizable: false,
				draggable: false,
				modal: true				
			});
		},
		success: function(msg)
		{
			$('#help-modal span').remove();
			$('#help-modal').append(msg);
			//remove help content navigation
			$('.help-content .help-menu').remove();
		}
	});
}
function prev(ind){
	var layout_count = $('.help-page #layout_count').attr('class');
	var prev_ind = ind-1;
	if (prev_ind < 0){
		prev_ind = layout_count-1;
	}
	var prev_arg = $('.help-page #'+prev_ind).attr("title");
	displayAjax(prev_arg, prev_ind);
}			
function next(ind){
	var layout_count = $('.help-page #layout_count').attr('class');
	var next_ind = ind + 1;
	if (next_ind == layout_count){
		next_ind = 0;
	}
	var  next_arg= $('.help-page #'+next_ind).attr("title");
	displayAjax(next_arg, next_ind);
}
$('.help-page #list-help td a').live('click', function(){		
	var prm1 = $(this).attr('title');
	var prm2 = $(this).attr('id');
	$(".equal_height").css("height", "");
	displayAjax(prm1, prm2);
	return false;
});
$('.help-page .help-content .nextHelp').live('click', function(){
	var prm = parseInt($(this).attr('id'));
	next(prm);
	return false;
});
$('.help-page .help-content .prevHelp').live('click', function(){
	var prm = parseInt($(this).attr('id'));
	prev(prm);
	return false;
});
$('.help-page .help-content .hideHelp').live('click', function(){
	$('.help-page .help-content').hide('slow').html('');
	return false;
});
$('#help-icon a').live('click', function(){
	var prm1 = $(this).attr('page_name');
	var cond = $('#help-modal').text();
	if (cond != 'loading'){
		$('#help-modal').dialog({
			show:'puff',  
			hide: 'blind',
			resizable: false,
			draggable: false,
			modal: true				
		});
	}
	else{
		//request one display help
		oneDisplayHelp(prm1);
	}
	return false;
});

/*hover function*/
$('.help-page .help-content .prevHelp').live({
mouseover: function(){
	$('.help-page .help-content .prevHelp img').attr("src", "/tpl/css/images/prev-arrow-hover.png#");
},
mouseout: function(){
	$('.help-page .help-content .prevHelp img').attr("src", "/tpl/css/images/prev-arrow.png#");
}
});

$('.help-page .help-content .hideHelp').live({
mouseover: function(){
	$('.help-page .help-content .hideHelp img').attr("src", "/tpl/css/images/minimize-hover.png#");
},
mouseout: function(){
	$('.help-page .help-content .hideHelp img').attr("src", "/tpl/css/images/minimize.png#");
}
});

$('.help-page .help-content .nextHelp').live({
mouseover: function(){
	$('.help-page .help-content .nextHelp img').attr("src", "/tpl/css/images/next-arrow-hover.png#");
},
mouseout: function(){
	$('.help-page .help-content .nextHelp img').attr("src", "/tpl/css/images/next-arrow.png#");
}
});
	
// ???????????????????????????????????????????????????? commercial-watch-page javascript ?????????????????????????????????????????? //
//perform countdown on text area
comment_textarea(".commercial-watch-page .commercial-form-comment #comment", ".commercial-watch-page .commercial-form-comment .counter_info", "New Comment . . .", 500);

//load comment video at first init
var commercial_video_id = $(".commercial-watch-page .commercial-form-comment form #video_id").attr('value');
load_commercial_comment(1, commercial_video_id);

//give comment form ajax action
$(".commercial-watch-page #form-give-comment-commercial").submit(function(event) {
	vid_id = $(".commercial-watch-page .commercial-form-comment form #video_id").attr('value');
	username = $(".commercial-watch-page .commercial-form-comment form #username").attr('value');
	comment = $(".commercial-watch-page .commercial-form-comment form #comment").attr('value');
	if (checkLengthCommercialComment(comment.length, 500)){					
		// Send the data using post and put the results in a div
		$.post( "/video_comment_block.php", { "page": 1, "video_id": vid_id, "action": "add_comment", "comment": comment, "username": username },
			function( content ) {
				$( "#vid-comment" ).html( content );
			}
		);
		$(".commercial-watch-page .commercial-form-comment form #comment").attr('value', '');
		load_commercial_comment(1, vid_id);
		$('.commercial-watch-page .commercial-form-comment .error_length').hide();
		$('.commercial-watch-page .commercial-form-comment .error_empty').hide();
	}else{
		//add comment suspend
		event.preventDefault(); 
	}
	return false;
});

$(".commercial-watch-page .load_comment").live("click", function(){
	var video_id = $(this).attr("vid-id");
	var page = $(this).attr("page");
	load_commercial_comment(page, video_id);
	return false;
});

//load comment ajax
function load_commercial_comment(page, vid_id){
	// Send the data using post and put the results in a div
	$.post( "/video_comment_block.php", { "page" : page, "video_id": vid_id},
		function( content ) {
			$( ".commercial-watch-page #vid-comment" ).html( content );
		}
	);
}

function checkLengthCommercialComment( len, max ) {
	if ( len == 0  || getCharCounter()==false ) {
		$('.commercial-watch-page .commercial-form-comment .error_length').hide();
		$('.commercial-watch-page .commercial-form-comment .error_empty').show();
		return false;
	}
	else if ( len > max ) {
		$('.commercial-watch-page .commercial-form-comment .error_empty').hide();
		$('.commercial-watch-page .commercial-form-comment .error_length').show();
		return false;
	}
	else {
		return true;
	}
}

// ???????????????????????????????????????????????????? help span div append javascript ?????????????????????????????????????????? //
//common help modal style
var content= "<div id='help-modal' style='display:none;'><span class='loading-bar-ajax'>loading</span></div>";
$("body").append(content);

});
