var hash = window.location.hash;

$(document).ready(function(){

    //_hideLoading();

    // Shortlist on model detail page
    iniDetailShortList();
    iniIndex();
    
});


iniIndex = function(){
    $('#model-list-content li').hover(function(){
        var $node = $(this);

        if($node.hasClass('shortlisted')){
            $(this).find('.remove-from-shortlist').show();
        } else {
            $(this).find('.add-to-shortlist').show();
        }
        
        
    }, function(){
        var $node = $(this);
        if($node.hasClass('shortlisted')){
            $(this).find('.remove-from-shortlist').hide();
        } else {
            $(this).find('.add-to-shortlist').hide();
        }
        
    });
};



iniDetailShortList = function(){

    $('.add-to-shortlist')
        .bind('click' , function(){

        var $button = $(this);
        var $client_id = $button.attr('rel');

        if(null == $client_id){
            $client_id = $('#model-id').text();
        }

        if(null == $client_id){
            console.log("CLIENT_ID COULD NOT BE FOUND");
            return false;
        }
        //console.log($client_id);

        // update the session with this model id
        $.ajax({
            type: "GET",
            url: "/clients/shortlist_add/client_id:"+$client_id ,
            beforeSend: function(){_showLoading();},
            complete: function(){_hideLoading();},
            success: function(data){
             if(1==data){

                    // update the shortlistcount
                    count = $('#shortlisted').text(); /* get the count , this element is always here regardless... */
                    count++;
                    _updateCount( count );

                    $button.addClass('foo');

                    $('.add-to-shortlist').hide();
                    //$('.remove-from-shortlist').show();              /* show the element */


                }
                if( 0 == data){
                    //console.log('Client already in shortlist');
                }

                if(!$button.parent().hasClass('shortlisted')){
                    $button.parent().addClass('shortlisted');
                }
            }
        });

        return false; /* Disable the link */
    });

    $('.remove-from-shortlist')
        .bind('click' , function(){

        var $button = $(this);
        var $client_id = $button.attr('rel');

        // update the session with this model id
        $.ajax({
            type: "GET",
            url: "/clients/shortlist_remove/client_id:"+$client_id ,
            beforeSend: function(){_showLoading();},
            complete: function(){_hideLoading();},
            success: function(data){
                if(1==data)
                {
                    //console.log('Removed Client from shortlist');
                    // update the shortlistcount
                    count = $('#shortlisted').text();
                    count = (count-1);
                    _updateCount( count );

                    $('.remove-from-shortlist').hide();
                    //$('.add-to-shortlist').show();

                }
                if( 0 == data){
                    //console.log('Client not in shortlist');
                }

                if($button.parent().hasClass('shortlisted')){
                    if(HERE == '/shortlist'){
                        $button.parent().hide();
                    } else {
                        $button.parent().removeClass('shortlisted');
                    }
                }

            }
        });

        return false;
    });

}

_hideLoading = function(){
    if($('#loading:visible').length == 1){
        $('#loading').hide();
    }
}
_showLoading = function(){
    if($('#loading:visible').length == 0){
        $('#loading').show();
    }
}

_updateCount =function( count ){
    
    if(count < 0){count = 0;}
    $('#shortlisted').text(count);
    Cufon.refresh();
}

iniIndexShortList = function(){

    // hover actions...
    $('.model')
        .bind("mouseenter",function()
        {
            // if this moddel is shortlisted...
            if($(this).hasClass('shortlisted') == true)
            {
                $(this).find('.remove-from-shortlist')
                    .css('left' , 0+'px') /* move back into viewport */
                    .show();
            }
            else
            {
                $(this).find('.add-to-shortlist')
                    .css('left' , 0+'px') /* move back into viewport */
                    .show();
            }
        })
        .bind("mouseleave",function()
        {
            // when we leave...cleanup....
            $(this).find('.remove-from-shortlist:visible').hide();
            $(this).find('.add-to-shortlist:visible').hide();
        });

    //make title click
    /*$('span.default').bind('click' , function(){
        alert('clicked');
    });*/

    // add
    $('.add-to-shortlist')
        .bind('click' , function()
        {

        var $button = $(this);
        var $client_id = $button.find('a').attr('rel');

        $.ajax({
            type: "GET",
            url: "/clients/shortlist_add/client_id:"+$client_id ,
            beforeSend: function(){_showLoading();},
            complete: function(){_hideLoading();},
            success: function(data){
               if(1==data)
               {
                    // update the shortlistcount
                    count = $('#shortlisted-subtext').text();
                    count++;
                    _updateCount( count );

                    // add the classname .shortlisted to some areas..
                    $button.parent().parent().addClass('shortlisted');// the .model div

                    // updated button...
                    $button.parent().parent().find('span.default a').addClass('shortlisted')
                    // update sIFR button
                    _updateSIFRCss( $button.parent().parent().find('.default object').attr('id') , 'a{font-size: 16pt; color: #8d0035; text-decoration: none;} , a:hover{font-size: 16pt; color: #8d0035;}' );

                    // toggle
                    $button.parent().parent().find('.add-to-shortlist').hide();
                    //$button.parent().parent().find('.remove-from-shortlist').show();

                }
                if( 0 == data){
                    //console.log('Client already in shortlist');
                }
            }
        });
        // this this model to being shortlisted


        return false;
    });

    // remove
    $('.remove-from-shortlist')
        .bind('click' , function(){

        var $button = $(this);
        var $client_id = $button.find('a').attr('rel');

        // update the session with this model id
        $.ajax({
            type: "GET",
            url: "/clients/shortlist_remove/client_id:"+$client_id ,
            beforeSend: function(){_showLoading();},
            complete: function(){_hideLoading();},
            success: function(data){
              if(1==data)
              {
                    //console.log('Removed Client from shortlist');

                    // update the shortlistcount
                    count = $('#shortlisted-subtext').text();
                    count = (count-1);
                    _updateCount( count )

                    if($button.parent().parent().hasClass('shortlisted') == true )
                    {
                        // remove the classname .shortlisted from the .model div
                        $button.parent().parent().removeClass("shortlisted");

                        //
                        $button.parent().parent().find('span.default a').removeClass('shortlisted')
                        // update sIFR
                        _updateSIFRCss( $button.parent().parent().find('.default object').attr('id') , 'a{font-size: 16pt; color: #FFFFFF; text-decoration: none;} , a:hover{font-size: 16pt; color: #8d0035;}' );

                        // toggle
                        $button.parent().parent().find('.remove-from-shortlist').hide();
                        //$button.parent().parent().find('.add-to-shortlist').show();
                    }
                }
                if( 0 == data){
                    //console.log('Client not in shortlist');
                }
            }
        });

        return false;
    });
}



function popUp(URL)
{
    day = new Date();
    id = day.getTime();
    eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=750,height=615,left = 370,top=100');");
}

jQuery.extend(
  jQuery.expr[ ":" ],
  {reallyvisible : "(jQuery(a).is(':visible') && jQuery(a).parents(':hidden').length == 0)"}
);
