var last_inv_id=0;

$(function(){
    // if(!uid)     // when non logged in users
    // $("a.signup").addClass('thickbox').attr('href',"#TB_inline?inlineId=register&width=300&height=80");

    $('a[rel*=facebox]').facebox();
    fontResizer('10px','11px','12px');



      
    if(uid){
// $.jGrowl("Hello world!");
}
});


getChatInvitations= function (){
    $.ajax({
        type: "POST",
        url: baseurl+'/chat/getInvitations/'+last_inv_id+'/'+Math.random(),
        dataType:'json',
        success: function(msg){
                
               $.each(msg,function(i,item) {
                   last_inv_id=i;
                   jQuery.noticeAdd({
                            text: '<b>'+msg[i]+'</b> Want to Chat with you <br/> <center><a class="button century-22" onclick=\'acceptIt("'+msg[i]+'",'+i+')\' href="#"> Accept</a> <a onclick=\'rejectIt('+i+')\'  class="button century-22" href="#"> Reject</a> </center>',
                            stay: true
                       });
               });
                 

         

          }
        
    });

}


function acceptIt(username,c_id){
    $.ajax({
        type: "POST",
        url: baseurl+'/chat/acceptInvitation/'+c_id+'/'+Math.random(),
        success: function(msg){
            jQuery.noticeRemove($('.notice-item-wrapper'), 400);
            opendialog('/chat/index/'+username, 375,550);
        }
    });   
    
  
}

function rejectIt(c_id){
    $.ajax({
        type: "POST",
        url: baseurl+'/chat/AddToIgnor/'+c_id+'/'+Math.random(),
        success: function(msg){
            jQuery.noticeRemove($('.notice-item-wrapper'), 400);
        }
    });
}


//// to validate search box at top ////
function searchit(f){

    if(f.lookfor.value.length<3 || f.lookfor.value==f.lookfor.title){
        show_msg('Please enter atleast 3 Characters to search',1);
        return false;
    }
    return true;
}
/////////////////////////////////////////
function gotourl(url){

    window.location.href=url;
}

function uploadpic(){

    window.open(''+baseurl+'/members/uploadimg/','_blank','width=450,height=100,scrollbars=no,resizeble=no');

}

function uploadpiclist(name)
{

    var ht='<img src="'+baseurl+'/img/profile_pics/'+name+'" />'+
    '<input type="radio" class="clearLeft" name="data[User][picture]" value="'+name+'" />';
    $('#newimg').html(ht);


}

///////////// notification msg ///////////////////////
function show_msg(str,flag,ajax){
    humanMsg.setup();
    humanMsg.displayMsg(str,flag,ajax);
    return false;
}

function hide_msg(str){
    humanMsg.hideMsg(str);
    return false;
}



////////////////////////////////////////////////////////////

function opendialog(url,width,height){


    window.open(baseurl+url,'_blank','width='+width+',height='+height+',scrollbars=no,resizeble=yes');


}

////////////////////////////////////////////////////////
function checkEmail (strng) {
    var error="";
    if (strng == "") {
        error = "You didn't enter an email address.\n";
    }

    var emailFilter=/^.+@.+\..{2,3}$/;
    if (!(emailFilter.test(strng))) {
        error = "Please enter a valid email address.\n";
    }
    else {
        //test email for illegal characters
        var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
        if (strng.match(illegalChars)) {
            error = "The email address contains illegal characters.\n";
        }
    }
    return error;
}


function hovertxt(obj,flag){

    if(!flag)
    {
        if(obj.value.length < 1 || obj.value==obj.title )
        {
            obj.value="";
            obj.className="black-txt left";
        /* if(obj.id=="UserPassword")
                {
                  obj.setAttribute("type","password");
                }*/

        }
    }else {
        if(obj.value.length<1 ||  obj.value=="")
        {
            obj.value=obj.title;
            obj.className="grey-txt left";
        /*if(obj.id=="UserPassword")
                    {
                        obj.setAttribute("type","text");
                    }*/

        }

    }
}

function fontResizer(smallFont,medFont,largeFont)
{
    function clearSelected() {
        $(".smallFont").removeClass("curFont"); $(".medFont").removeClass("curFont"); $(".largeFont").removeClass("curFont");
    }
    function saveState(curSize) {
        var date = new Date(); date.setTime(date.getTime()+(7*24*60*60*1000)); var expires = "; expires="+date.toGMTString(); document.cookie = "fontSizer"+"="+curSize+expires+"; path=/";
    }

    $(".smallFont").click(function(){
        $('html').css('font-size', smallFont); clearSelected(); $(".smallFont").addClass("curFont"); saveState(smallFont);
    });

    $(".medFont").click(function(){
        $('html').css('font-size', medFont); clearSelected(); $(".medFont").addClass("curFont"); saveState(medFont);
    });

    $(".largeFont").click(function(){
        $('html').css('font-size', largeFont); clearSelected(); $(".largeFont").addClass("curFont"); saveState(largeFont);
    });

    function getCookie(c_name) {
        if (document.cookie.length>0) {
            c_start=document.cookie.indexOf(c_name + "="); if (c_start!=-1) {
                c_start=c_start + c_name.length+1; c_end=document.cookie.indexOf(";",c_start); if (c_end==-1) c_end=document.cookie.length; return unescape(document.cookie.substring(c_start,c_end));
            }
        } return "";
    }

    var savedSize = getCookie('fontSizer');

    if (savedSize!="") {
        $('html').css('font-size', savedSize); switch (savedSize) {
            case smallFont: $(".smallFont").addClass("curFont"); break; case medFont: $(".medFont").addClass("curFont"); break; case largeFont: $(".largeFont").addClass("curFont"); break; default: $(".medFont").addClass("curFont");
        }
    }
    else {
        $('html').css('font-size', medFont); $(".medFont").addClass("curFont");
    }
}
