var script_start = 1;

jQuery(document).ready(function()
{
    setTimeout(function()
    {
        jQuery('#msg').fadeOut(1000);
    }, 3000);
    setTimeout(function()
    {
        jQuery('#authMessage').fadeOut(1000);
    }, 3000);
       
    jQuery('#editlink').click(function() {
        jQuery('#edit').toggle(); // First click should toggle to 'show'
        jQuery('#st').toggle(); // First click should toggle to 'show'
    });
    jQuery('#menumaster').click(function() {
        jQuery('#menuchild').toggle(); // First click should toggle to 'show'
    });

    initLogin();
    /*
    if(jQuery.cookie('search') == "city"){
        toggleSearch('link_1','search_1');
    }else if(jQuery.cookie('search') == "zipcode"){
        toggleSearch('link_2','search_2');
    }else if(jQuery.cookie('search') == "mls"){
        toggleSearch('link_3','search_3');
    }else{
        toggleSearch('link_1','search_1');
    }
    */
});
function click(id)
{
    jQuery("#"+id).html('');
}
 
function close_div(id)
{
    //jQuery("table > #"+id).slideUp('slow');
    jQuery("#"+id).html('');
}
function toggle_details_link(id,id2,tab_id){
    jQuery("#"+id2).fadeOut('slow');
    jQuery("#"+id).fadeIn('slow');

    if(tab_id){
        jQuery("#"+tab_id).html('');
    }
}

function jqCheckAll( id, name,master)
{
   if (jQuery('#'+master).is(':checked'))
   {
      jQuery("form#" + id + " INPUT[@name=" + name + "][type='checkbox']").attr('checked', true);
   }
   else
   {
      jQuery("form#" + id + " INPUT[@name=" + name + "][type='checkbox']").attr('checked', false);
   }
}
function checkChangePasswordForm(){
	var errorMsg = "";
	if(jQuery("#UserOldPassword").val() == ""){
		errorMsg += "Old password must not be blank \n";
	}
	if(jQuery("#UserNewPassword").val() == ""){
		errorMsg += "New password must not be blank \n";
	}
	if(jQuery("#UserConfirmPassword").val() == ""){
		errorMsg += "Confirm password must not be blank and should match with new password. \n";
	}
	
	if(errorMsg != ""){
		alert(errorMsg);
		return false;
	}
	return true;
}

function updateLoginHeader(strData){
    var arrResponce = strData.split("~~");
    strData = arrResponce[2];
    jQuery('#header_signup').html(strData);
    initLogin();
    jQuery("#signin_menu").toggle();
    jQuery("#signin").toggleClass("menu-open");
    if(jQuery.trim(arrResponce[0]) == 'failer'){
        jQuery("input[id='UserPassword']").val('');
        jQuery("#signin_menu #msg").html(arrResponce[1]);

    }else{
        jQuery("#aLogin").html("My Account");
        jQuery("#aLogin").attr("href","/users");
    }
}

function initLogin(){
    //twitter login start
    jQuery("#signin").click(function(e) {
        e.preventDefault();
        jQuery("#signin_menu").toggle();
        jQuery("#signin").toggleClass("menu-open");
    });

    jQuery("#signin_menu").mouseup(function() {
        return false;
    });
    jQuery(document).mouseup(function(e) {
        if(jQuery(e.target).parent("a.signin").length==0) {
            jQuery("#signin").removeClass("menu-open");
            jQuery("#signin_menu").hide();
        }
    });
//twitter login end
}

function toggleSearch(src,div){
    jQuery('#search_1').hide();
    jQuery('#search_2').hide();
    jQuery('#search_3').hide();
    jQuery('#'+div).show('slow');

    jQuery('#link_1').css('color','#FFF');
    jQuery('#link_1').css('cursor','pointer');
    jQuery('#link_2').css('color','#FFF');
    jQuery('#link_2').css('cursor','pointer');
    jQuery('#link_3').css('color','#FFF');
    jQuery('#link_3').css('cursor','pointer');

    jQuery('#'+src).css('color','#000');
    jQuery('#'+src).css('cursor','default');
    
    options = {path: '/', expires: 10};
    if(src == "link_1"){
      jQuery.cookie('search', 'city', options);
    }else if(src == "link_2"){
        jQuery.cookie('search', 'zipcode', options);
    }else if(src == "link_3"){
        jQuery.cookie('search', 'mls', options);
    }else{
        jQuery.cookie('search', 'city', options);
    }
}
function clearText(src,text){
    t = jQuery("#"+src).val();
    if(jQuery.trim(t) == text){
        jQuery("#"+src).val('');
    }
}
function addText(src,text){
    t = jQuery("#"+src).val();
    if(jQuery.trim(t) == ''){
        jQuery("#"+src).val(text);
    }
}

function changePropertyStyle(objElement){
    //alert(jQuery(objElement).attr('value'));
    divid = jQuery(objElement).attr('value');

    jQuery("#res").css('display', 'none');
    jQuery("#cn").css('display', 'none');
    jQuery("#tw").css('display', 'none');
    jQuery("#mul").css('display', 'none');
    jQuery("#lnd").css('display', 'none');
    jQuery("#com").css('display', 'none');

    jQuery("#"+divid).css('display', 'block');

}

function selectAllNone(strCheckedStatus)
{
    form =document.forms['CommunityTemplateAdminGeneratePagesForm'];
    for (i = 0; i < form.elements.length; i++) {
        if (form.elements[i].type == "checkbox"){
            form.elements[i].checked = strCheckedStatus;
        }
    }
}
function selectAllCategory(strCheckedStatus)
{
    form =document.forms['StaticPageAdminIndexForm'];
    for (i = 0; i < form.elements.length; i++) {
        if (form.elements[i].type == "checkbox"){
            form.elements[i].checked = strCheckedStatus;
        }
    }
}
function execute_script(){

    jQuery("#loading_status").show();
    jQuery("#ajax_execute").html("");
    jQuery.ajax({
        type : "POST",
        url:"/CommunitySearches/ajax_execute_script",
        success: function(data){
            jQuery("#ajax_execute").append("<i><br />" + data + "</i>");
        },
        complete:function(){
            script_start = 0;
            jQuery("#loading_status").html("<img src='/img/success.png'> Script Execution completed sucessfully.");
            jQuery("#loading_status").fadeOut(1000);
            getAjaxUpdate(1);
        }
    });

    if(script_start){
        getAjaxUpdate(0);
    }
}

function getAjaxUpdate(flag){
    var TimerID = setTimeout(function(){
        if(script_start || flag == 1){
        jQuery.ajax({
            type : "POST",
            url:"/getAjaxRequest.php",
            cache:false,
            success: function(data){
                jQuery("#ajax_execute").html("");
                jQuery("#ajax_execute").html(data);
            }
        });
        getAjaxUpdate(0);
        return;
        }else{
             clearTimeout(TimerID);
             return 0;
        }
    },30000);       // this will check the update after every 30 sec.
}
