
function loadWebPageAjax(sourceDiv, actionURL) {
    $.ajax ({     
    	type:   "get",
        url:        actionURL, 
        dataType:   "html",
        async: false,
        beforeSend: function() {
        },
        complete:   function() {
        },      
        success:  function(data) {   
            if (data != '') {
                $('#' + sourceDiv).html(data);
            }          
        }
    });    
}

function showFlash(postId) {
	$('#mask_' + postId).hide(); $('#po_' + postId).show();
}

function hideFlash(postId) {
	$('#mask_' + postId).show(); $('#po_' + postId).hide();
}


var searchtypeboxIsOpen                                 = false;

$(document).ready(function() {
	$('#searchItId').click(function(e){
		$('#searchItId').val('');
	});
	
	$('#record_group_title').click(function(e){
		if ($('#record_group_title').val() == "Name your recording") {
			$('#record_group_title').val('');
		}
	});

	$('#nameItId').click(function(e){
		$('#nameItId').val('');
	});
	
	$(document).click(function(){
		$("#fbNotificationsFlyout").hide();
		$("#showNotification").toggle(
			  function () {
				  	$("#fbNotificationsFlyout").show();
			  },
			  function () {
				  	$("#fbNotificationsFlyout").hide();
			  }
			);
	});
	
	$("#showNotification").toggle(
			  function () {
				  	$("#fbNotificationsFlyout").show();
			  },
			  function () {
				  	$("#fbNotificationsFlyout").hide();
			  }
			);
	
    $("#searchboxInput").click(function () {
        if ( !searchtypeboxIsOpen )
            $("#search_listbox").show();
        else
            $("#search_listbox").hide();

        searchtypeboxIsOpen                             = !searchtypeboxIsOpen;

        $("#searchboxInput").focus();
        var theText                                     = $(this).val();
        if ( theText == "Search" )
            $(this).val("");
    });
    
    $('.play_item').click(function(e) {
        $('.play_item').removeClass('active');
        $(this).addClass('active');    
    });

    $("#searchboxInput").keydown(function(e){
        if ( e.keyCode == 40 || e.keyCode == 38 ) {
            if ( $("#do-group-search").val() == 1 ) {
                $("#search-groups").removeClass("search_list_item_selected");
                $("#search-users").addClass("search_list_item_selected");

                $("#do-user-search").val("1");
                $("#do-group-search").val("0");

                return false;
            }
            else {
                $("#search-users").removeClass("search_list_item_selected");
                $("#search-groups").addClass("search_list_item_selected");

                $("#do-group-search").val("1");
                $("#do-user-search").val("0");

                return false;
            }
        }

        return true;
    });

    $("#large_search").keyup(function(e){
        if ( e.keyCode == 13 ) {
            if ( $('div.autocomplete').length > 0 ) {
                $('div.autocomplete').remove();

                return false;
            }
        }

        return true;
    });

    $("#search-groups").bind("click", function () {
       $("#search-users").removeClass("search_list_item_selected");
       $("#search-groups").addClass("search_list_item_selected");

       $("#do-group-search").val("1");
       $("#do-user-search").val("0");
    });

    $("#search-users").bind("click", function () {
        $("#search-groups").removeClass("search_list_item_selected");
        $("#search-users").addClass("search_list_item_selected");

        $("#do-user-search").val("1");
        $("#do-group-search").val("0");
    });
    
    $( "#dialogStatusMessageId" ).dialog({
		autoOpen: false,
		height: 150,
		width: 360,
		resizable: false,
		modal: true,
		buttons: {
			"Ok": function() {
				window.location.reload();
			}
		},
		close: function() {
			window.location.reload();
		}
	});
});

function followMe(userToFollow, doReload) {
    $.post("doAction.php", {
        action: 	"follow-me",
        data:	userToFollow}, function() {
            if ( doReload ) {
                window.location.reload();
            }
    });
}

function unFollowMe(userToFollow, doReload) {
    $.post("doAction.php", {
        action: 	"unfollow-me",
        data:	userToFollow}, function() {
            if ( doReload ) {
                window.location.reload();
            }
    });
}

function deletePost(postId) {
    if ( confirm("Are you sure you want to delete this post, THIS WILL DELETE ALL REPLIES TOO?") ) {
        $.post("doAction.php", {
            action: 	"delete-post",
            data:	postId}, function() {
                window.location.reload();
        });
    }
}

function deleteReply(replyId) {
    if ( confirm("Are you sure you want to delete this reply?") ) {
        $.post("doAction.php", {
            action: 	"delete-reply",
            data:	replyId}, function() {
                window.location.reload();
        });
    }
}

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


function updateTips(msgId,  t ) {
	$("#" + msgId)
		.html( t )
		.addClass( "ui-state-highlight" );
	setTimeout(function() {
		$("#" + msgId).removeClass( "ui-state-highlight", 1500 );
	}, 500 );
}

function checkLength(msgId, o, n, min, max ) {
	if ( o.val().length > max || o.val().length < min ) {
		o.addClass( "ui-state-error" );
		updateTips(msgId, "Length of " + n + " must be between " +
			min + " and " + max + "." );
		return false;
	} else {
		return true;
	}
}

function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-+\s]+")|([\w-+]+(?:\.[\w-+]+)*)|("[\w-+\s]+")([\w-+]+(?:\.[\w-+]+)*))(@((?:[\w-+]+\.)*\w[\w-+]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
};

/**
 * Provide a general dialog with flexible setting
 * @param selector : the id of html element be displayed in dialog
 * null: the dialog will generate it's own id(general-dialog) for the dialog
 * @param title: title of dialog
 * null: the title in selector element won't be override 
 * @param mess : message be displayed in dialog
 * null: the content of selector element won't be override
 * @param iconType: icon for message, the icon is displayed front the message
 * 'warn': warning icon
 * 'info': info icon
 * null: nothing
 * @param options : options for dialog
 * null: it will create dialog with default setting(the box with no button, model, and could be resized)
 * @param focusButton : the translate text of button be focused
 * ex: _('Yes') : the button have translate text of 'Yes' is focus
 * @return show dialog
 * @return
 */
function generalDialog(selector,title,mess,iconType,options,focusButton) {
	var b = jQuery(document.body);	
	var msgbox = '';
	var icon = '';
	
	if (selector == null || $.trim(selector) == '' || $('#' + selector).html() == null) {
		
		selector = "general-dialog";		
		$("#general-dialog").remove();
		msgbox = "<div id='general-dialog' title=''></div>";		
		b.append(msgbox);		
	}	
	
	if (iconType == 'warn') {		
		icon = "<span class='ui-icon  ui-icon-alert' style='float:left; margin:0 7px 20px 0;'></span>";
	} else if (iconType == 'info') {
		icon = "<span class='ui-icon ui-icon-info' style='float:left; margin:0 7px 20px 0;'></span>";
	}
	
	
	if (options == null || options =='') {			
		options = {
				autoOpen: true,			
				bgiframe: true,
				resizable: false,		
				modal: true,
				buttons: {}
		}
	}
	options['dialogClass'] = 'custom-dialog';
	var len = $('body').find('#' + selector).length;	
	if ($('div[role="dialog"] div[id="' + selector + '"]').html() != null && len == 1) {
		$('#' + selector).dialog( 'destroy' );
		if (title != null) {
			$('#' + selector).attr('title',title);
		}
		if (mess != null) {
			$('#' + selector).html("<p>" + icon + mess) + "</p>";
		}
		$('#' + selector).dialog(options);		
	} else if ($('div[role="dialog"] div[id="' + selector + '"]').html() != null && len > 1) {		
		$('div[role="dialog"] div[id="' + selector + '"]').remove();
		if (title != null) {
			$('#' + selector).attr('title',title);
		}
		if (mess != null) {
			$('#' + selector).html("<p>" + icon + mess) + "</p>";
		}
		$('#' + selector).dialog(options);
	} else {		
		if (title != null) {
			$('#' + selector).attr('title',title);
		}
		if (mess != null) {
			$('#' + selector).html("<p>" + icon + mess) + "</p>";
		}
		$('#' + selector).dialog(options);
	}

	if (focusButton != null && focusButton != '') {
		
		$("div[role='dialog'] div button").each(function (i) {
	        $(this).attr('class','ui-state-default ui-corner-all');
	      });
		$("div[role='dialog'] div button:contains('" + focusButton+ "')").attr('class',"ui-state-default ui-corner-all ui-state-focus");
		$("div[role='dialog'] div button:contains('" + focusButton+ "')").focus();
	}
	
}
