/* begin config */
// Timers:
var hoverOptions = { 
    'show_delay_timer' : 1000,    // Delay after element "mouseenter" event start to show container ,ms
    'hide_delay_timer' : 200,     // Delay after element "mouseleave" event start to hide container ,ms
    'show_speed' : 0,             // Speed that content have been shown with ,ms
    'slide_speed' : 1000,         // Speed that container have been slide with ,ms
    // Positions
    'left_cap_check' : 550,       // Width of hover container (incl. paddings, margins) ,px
    'max_width' : 450,            // Width of hover content (incl. paddings, margins) ,px
    'left_pos_fix' : 115,
    'left_pos_fix_reverse' : 0,
    'left_pos_fix_reverse_val' : -109,
    // X position relating hovered element ,px
    'top_pos_fix' : 200          // Y position relating hovered element ,px
};

var counter1 = 0;
var counter2 = 0;
 /* end config */
 
function checkSelect(x,y)
{
    coords = getOffsetSum($("#items_per_page_select").get(0));
    sx = coords.left;
    sy = coords.top;
    if ((sx+60 > x && sx < x+450) && (sy > y && sy < y + 300))
    {
       $("#items_per_page_select").hide();
    }
    return true;
}

function fixSelect()
{
    if ($("#items_per_page_select").css('display') == 'none')
    {
        $("#items_per_page_select").show();
    }
}
function getResourceId (url) {
    var pos = 0;
    
    url += '';
    pos = url.indexOf( 'resourceId=' );
    if (pos == -1) {
        return false;
    } else{
        url = url.slice( pos );
        pos = url.indexOf('&');
        
        if(pos == -1)
        {
            url = url.slice(11);
            return url;
        }
        else
        {
            url = url.substring(11, pos);
            return url;
        }
    }
}

function getOffsetSum(elem) {
    var top=0, left=0
    while(elem) {
        top = top + parseInt(elem.offsetTop)
        left = left + parseInt(elem.offsetLeft)
        elem = elem.offsetParent        
    }
 
    return {top: top, left: left}
}

var showHoverDetail = function(el)
{
    var params = el.attr('params');
    var left = params.left;
    var top = params.top;
    var id = params.id;
    var licenced = params.licenced;
    var preview = params.preview;
    var reverse = params.reverse;
    var testingInfo = '';

    if(reverse)
    {
        var left_pos_fix_reverse = hoverOptions.left_pos_fix_reverse_val;
        if($('#hover_container').length)
        {
            $('#hover_container').attr({'id':'hover_container_reverse'});
        }
        var container_id = 'hover_container_reverse';
     } else {
        var left_pos_fix_reverse = hoverOptions.left_pos_fix_reverse;
        if($('#hover_container_reverse').length)
        {
            $('#hover_container_reverse').attr({'id' : 'hover_container'});
        }
        var container_id = 'hover_container';
     }
     checkSelect(left+hoverOptions.left_pos_fix+left_pos_fix_reverse, top-hoverOptions.top_pos_fix);
     if($('#'+container_id).length)
     {

            $('#hover_container_body').empty();
            $('#hover_container_end').empty();

        	$('#hover_container_body').append('<center><img class="hover_loading" src="/images/backgrounds/hoverLoading.gif"></img></center>');
        	$('#hover_container_end').append('<a id="closeBut" href="javascript:void(0)" title="CLOSE"></a>');

            $('#'+container_id).css({'top':top-hoverOptions.top_pos_fix,'left':left+hoverOptions.left_pos_fix+left_pos_fix_reverse});
            $('#'+container_id).show();
            $.get('/hover.php?resourceId='+id+'&licenced='+licenced+preview+testingInfo,function(data){
                wb = $('#'+container_id).attr('offsetWidth');

                $('#hover_container_body').empty();
                $('#hover_container_body').append(data);

                $('#hover_property').show();
                container =  $("#"+container_id).get(0);
                $(container).one("mouseleave", function(){
                    el.attr( 'params', function(){
                        clearTimeout(this.params.timer);
                        this.params.timer = setTimeout(function(){hideHoverDetail(container_id, el);}, hoverOptions.hide_delay_timer);
                    });
                });
                
            });


        
     }
     else
     {
        initHoverDetails(container_id);
        $('#'+container_id).css({'top':top-hoverOptions.top_pos_fix,'left':left+hoverOptions.left_pos_fix+left_pos_fix_reverse});

    	$('#hover_container_end').append('<a id="closeBut" href="javascript:void(0)" title="CLOSE"></a>');
    	$('#hover_container_body').append('<center><img class="hover_loading" src="/images/backgrounds/hoverLoading.gif"></img></center>');

        $('#'+container_id).show();
        $.get('/hover.php?resourceId='+id+'&licenced='+licenced+preview+testingInfo,function(data){
            wb = $('#'+container_id).attr('offsetWidth');

            $('#hover_container_body').empty();
            $('#hover_container_body').append(data);

            $('#hover_property').show();
            container =  $("#"+container_id).get(0);
                $(container).one("mouseleave", function(){
                    el.attr( 'params', function(){
                        clearTimeout(this.params.timer);
                        this.params.timer = setTimeout(function(){hideHoverDetail(container_id, el);}, hoverOptions.hide_delay_timer);
                    });
                });
        });



     }
    el.attr('params', function()
    {
        this.hoverInit = true;
    });
    $('#closeBut').bind("click",function(){hideHoverDetail(container_id, el);});
}
function hideHoverDetail(container_id, el)
{
        params = el.attr('params');
        $('#'+container_id).hide();
        fixSelect();
        $('#hover_container_body').empty();
        $('#hover_container_end').empty();
        el.attr('params', function()
        {
            this.hoverInit = false;
        });

}
function initHoverDetails(container_id)
{
	$(['<div id="'+container_id+'">',
        '<div id="hover_container_start">',
        '</div>',
        '<div id="hover_container_body">',
        '</div>',
        '<div id="hover_container_end">',
        '</div>',
        '</div>']
        .join('')).appendTo('body').hide();
}


jQuery(document).ready(function($)
{
	 $('.hoverdetails').live('hover',
        function(){
            var el = $(this);
            if (this.hoverInit)
            {
                clearTimeout(this.params.timer);
                this.params.timer = 0;
                return false;
            }
            else
            {
                $('.hoverdetails').each(function(i,e)
                {
                    if (this.params)
                    {
                        clearTimeout(this.params.timer);
                    }
                    this.hoverInit = false;
                });
                $('#hover_container_reverse').unbind();
                $('#hover_container').unbind();
                
                var bodyW = document.documentElement.clientWidth;
                var coords = getOffsetSum(this);
                this.params = {
                    'top' : coords.top,
                    'left' : coords.left,
                    'id' : getResourceId(this.href),
                    'reverse' : false,
                    'preview' : '',
                    'licenced' : true
                }


                if (el.hasClass('preview'))
                {
                    this.params.preview = '&preview=true';
                }
                if ((bodyW - this.params.left) < hoverOptions.left_cap_check)
                {
                    this.params.left = this.params.left - hoverOptions.max_width;
                    this.params.reverse = true;
                }
                if(this.rel)
                {
                    this.params.id = this.rel;
                    this.params.licenced = false;
                }
                if(this.nodeName == "IMG")
                {
                    this.params.id = this.id;
                    if (this.className.indexOf('unlicensed') + 1)
                    {
                        this.params.licenced = false;
                    }
                }
                if(this.params.id && this.params.id != 1499)
                {
                    clearTimeout(this.params.timer);
                    this.params.timer = setTimeout(function(){showHoverDetail(el);}, hoverOptions.show_delay_timer);
                }
            }
            
        },
        function(){
            var el = $(this);
            if($('#hover_container').length)
            {
                el.attr( 'params', function(){
                    clearTimeout(this.params.timer);
                    this.params.timer = setTimeout(function(){hideHoverDetail('hover_container', el);}, hoverOptions.hide_delay_timer);
            
                });

                $('#hover_container').one("mouseover", function(){
                    el.attr( 'params', function(){
                        clearTimeout(this.params.timer);
                        this.params.timer = 0;
                
                    });
                    $(this).one("mouseleave", function(){
                        el.attr( 'params', function(){
                            clearTimeout(this.params.timer);
                            this.params.timer = setTimeout(function(){hideHoverDetail('hover_container', el);}, hoverOptions.hide_delay_timer);
                    
                        });
                    });
                });

            }
            else if($('#hover_container_reverse').length)
            {
                el.attr( 'params', function(){
                    clearTimeout(this.params.timer);
                    this.params.timer = setTimeout(function(){hideHoverDetail('hover_container_reverse', el);}, hoverOptions.hide_delay_timer);
            
                });

                $('#hover_container_reverse').one("mouseover", function(){
                    el.attr( 'params', function(){
                        clearTimeout(this.params.timer);
                        this.params.timer = 0;
                
                    });
                    $(this).one("mouseleave", function(){
                        el.attr( 'params', function(){
                            clearTimeout(this.params.timer);
                            this.params.timer = setTimeout(function(){hideHoverDetail('hover_container_reverse', el);}, hoverOptions.hide_delay_timer);
                    
                        });
                    });
                });

            }
        }
    );
});
