$(".sign_in").live("click", function() {
    var color = 'white';
    var n = 0;
    flasher("logdiv",color,n);        
    if($("div.logdiv").css("display") == "none")
    {
        scroll(0,0);
        $("div.recoverdiv").hide();
        $("div.logdiv").show();

        $("div.register").html('<div class="welcomeheader">Need to <a href="'+baseUrl+'/index/register/">register</a> or <a href="javascript:showRecover();">recover your password</a>? </div>');
        $(".welcomeheader").css("width","550px");

    }
    //else
        //$("div.logdiv").fadeOut();

});


$(".read_more").live("click", function() {
    var color = 'white';
    var n = 0;
    flasher("logdiv",color,n);
    if($("div.logdiv").css("display") == "none")
    {    
        $("div.recoverdiv").hide();
        $("div.logdiv").show();
            
        $("div.register").html('<div class="welcomeheader">Need to <a href="'+baseUrl+'/index/register/">register</a> or <a href="javascript:showRecover();">recover your password</a>? </div>');
        $(".welcomeheader").css("width","550px");
    }
    var linkid = $(this).attr("rel");
    $("#linkid").attr("value",linkid);        
});

function showRecover() {
    $("div.logdiv").hide();
    $("div.recoverdiv").show();
    $("div.register").html("To access all features, please <a href='javascript:void(0);' class='sign_in' id='sign_in'>sign in</a> or <a href='"+baseUrl+"/index/register/'>register</a>")

}

function flasher(id,color, n) {
    
    if(color == 'orange') {
        color = 'white';
        $("."+id).css("border", "1px solid "+color);
    } else {
        color = 'orange';
        $("."+id).css("border", "1px solid "+color);
    }
    n++;
    if(n < 12)
        setTimeout("flasher('logdiv','"+color+"',"+n+")" , 1000);
    else
        n = 0;
}


$("#loguser").live("click", function(){
    $.ajax({
        url: baseUrl + '/index/ajax/',
        type: "post",
        data: $("#loginForm").serialize() + "&login=attempt",

        success: function (data) {
            if(data == "error")
                alert('Wrong email or password!');
            else {
                $("div.register").html('<div class="welcomeheader">Welcome '+data+'</div><div class="logoutheader"><a href="'+baseUrl+'/index/editprofile">your profile</a>&nbsp;|&nbsp;<a href="javascript:void(0);" id="logoutuser">logout</a></div><div class="your_profile" style="display:none"><a href="'+baseUrl+'/index/editprofile/">edit profile</a>&nbsp;|&nbsp;<a href="javascript:deleteProfile()">delete me</a></div>');
                $("div.logdiv").fadeOut();
                $("#logged").removeClass("hide");
                $("#logged").addClass("show");
                $("#notlogged").removeClass("show");
                $("#notlogged").addClass("hide");
                
                //alert("You are now logged-in and can access all content.");


                $.getJSON(baseUrl + '/index/ajax/getMyRates/true/',
                    function(data){
                      
                      $('div.starholder form.starrating input.yourstar').rating('readOnly', false);

                      $.each(data, function(i,item){
                          //console.log(c + " : " + i);
                          //console.log(item[0] + " : " + item[1]);
                          //$('div.starholder form.starrating input.userstar').removeAttr('disabled');
                          $('div.starholder form.starrating input.yourstar[name=yourstar'+item[0]+']').rating('select', item[1]);
                  });
                          
                          $(".agenciesLoggedOut").addClass("none");
                          $(".agenciesLoggedIn").removeClass("none");
                           // fitBoxes();
                });
             
                
                var sURL;
                var URL;
                var linkid = $("#linkid").attr("value");;
                
                if(linkid.match(/comm/))
                {    
                    id = linkid.split("_");
                    URL = $(".link_"+id[1]).attr("rel")+"/#addcomment";
                }
                else
                  URL = $(".link_"+linkid).attr("rel");
            
                if(typeof(URL) != "undefined")
                   sURL = URL;
                else
                    sURL = unescape(window.location.pathname);
                window.location.href = sURL;
            }
        },
        error: function (data) {
            alert('error');
        }
    });
});

$("#logoutuser").live("click", function(){
    $.ajax({
        url: baseUrl + '/index/ajax/',
        type: "post",
        data: "&logout=attempt",
        success: function (data) {
            $("div.register").html("To access all features, please <a href='javascript:void(0);' class='sign_in' id='sign_in'>sign in</a> or <a href='"+baseUrl+"/index/register/'>register</a>");
            $("#logged").removeClass("show");
            $("#logged").addClass("hide");
            $("#notlogged").removeClass("hide");
            $("#notlogged").addClass("show");

            $(".agenciesLoggedIn").addClass("none");
            $(".agenciesLoggedOut").removeClass("none");

            //$('div.starholder form.starrating input.userstar').attr('disabled', 'disabled');
            $('div.starholder form.starrating input.yourstar').rating('readOnly', true);
            //fitBoxes();

            var sURL = unescape(window.location.pathname);
            window.location.href = sURL;
        }
    });
});


$("#recoverpassword").live("click", function(){
    $.ajax({
        url: baseUrl + '/index/ajax/',
        type: "post",
        data: $("#recoverForm").serialize() + "&recover=attempt",
        success: function (data) {
            if(data == "empty")
                alert('Email is reqiured!');
            else if(data == "notregistered")
                alert('Email is not registered!');
            else if(data == "sent")
            {
                $("div.recoverdiv").hide();
                alert("Your password has been sent to your email address!");
            }    
        }
    });
});



function yourProfile() {
    div = $("div.your_profile");
    div.show();
    setInterval(function () { hideProfile(div); }, 10 * 1000);    
}

function hideProfile(div) {
    div.hide(); 
}


/* //$("#deleteProfile").live("click", function(){
function deleteProfile() {
    //var id = $("#deleteProfile").attr("rel");
    var answer = confirm ("Are you sure you want to proceed with this action? All your information will be deleted!");
    if (answer)
    {
        $.ajax({
            url: baseUrl + '/index/ajax/',
            type: "post",
            data: "&delete_profile=attempt",
            success: function (data) {
                alert('Your profile is deleted!');
            },
        });
    }        
}*/


function addAgencyRating (agency_id, rating) {

    $.ajax({
        url: baseUrl + '/index/ajax/rateAgency/true/',
        type: "post",
        data: "agency_id=" + agency_id + "&rating=" + rating,
        success: function (data) {
            //alert(data);
        }
    });

}


function fitBoxes() {
    
    //var container = $("#container").height();
    var layoutContent = $("#layout_content").height();
    var pageTitle = $(".pageTitle").height();
    var subTitle = $(".subTitle").height();

    var h = layoutContent+pageTitle+subTitle;
    $(".content").height(h);

    var max = 0;
    $(".fit").each(function(index){
        var trh = $(this).height(); 
        var pos = $(this).position();
        var endpos = trh + pos.top;
        if(max < endpos){
            max = endpos;
        }
    });
    $(".fit").each(function(index){
        var trh = $(this).height(); 
        var pos = $(this).position();
        var endpos = trh + pos.top;
        if(index == 1)
        {
            if(max > endpos)
                $(this).css("height", max-pos.top);
        }
    });
}

