ListyFilmow = new Object()

ListyFilmow.aktualnaLista = '';
ListyFilmow.mojProgram = new Array();
ListyFilmow.ogladnieteFilmy = new Array();
ListyFilmow.currentMovieID = null;
ListyFilmow.currentPlaylist = null;
ListyFilmow.listSaved = true;

styleScrollBar = { showArrows: true, scrollbarWidth: 12, scrollbarMargin: 0, animateTo: true, animateInterval: 25, animateStep: 5 };

ListyFilmow.bindPlayMovieButton = function(event) {
	ListyFilmow.addMovieEvent(event, true);
	return false;
}

ListyFilmow.bindAddMovieToMojProgramButton = function(event) {
	ListyFilmow.addMovieEvent(event, false);
	return false;
}

ListyFilmow.removeMovieFromProgram = function(event) {
    var callingObj = $(event.target);

    var movieIdentificator = callingObj.parents(".jq-movie").attr('id').split("-");

    var index = jQuery.inArray(movieIdentificator[0], ListyFilmow.mojProgram);
    if (index >= 0) {
    
        delete ListyFilmow.mojProgram[index];
        ListyFilmow.mojProgram.splice(index, 1);

        callingObj.parents(".jq-movie").remove();
    
        if(ListyFilmow.mojProgram.length > 0) {                
            ListyFilmow.listSaved = false;
            updateMyProgramInfo();
            $('#jq-blokM-container').jScrollPane(styleScrollBar);
        }
        else
            ListyFilmow.zapiszMojProgram(event);
    } 
    
    
    
    if($.cookie('session_mojprogram') != null) {
        var tmp_session = $.cookie('session_mojprogram').split('|');
        index = jQuery.inArray(movieIdentificator[0], tmp_session);
        
        if(index >= 0) {
            delete tmp_session[index];
            tmp_session.splice(index, 1);                        
        }        
        
        if(tmp_session.length > 0) {
            $.cookie('session_mojprogram', tmp_session.join("|"));
        }
        else
            $.cookie('session_mojprogram', null);
    }
     
    return false;
}

ListyFilmow.addMovieEvent = function(event, autoplay) {
    var callingObj = $(event.target);

    var movieID = callingObj.parents(".bottomItemNode").attr('id');
    if (movieID == undefined) { movieID = callingObj.parents(".bloczekLewy").attr('id'); }
    if (movieID == undefined) { movieID = callingObj.parents(".jq-search").attr('id'); }    
    if (movieID == undefined) { movieID = callingObj.attr("href").slice(1); }        

    ListyFilmow.addMovieToMojProgram(movieID, autoplay);
    return false;
}

ListyFilmow.addMovieToMojProgram = function(movieID, autoplay) {    
    if (jQuery.inArray(movieID, ListyFilmow.mojProgram) >= 0) return false;
    $('ul#jq-blokM.jq-ajax-tabs a[href="#mojprogram"]').click();        

    $.ajax({
        type: "POST",
        url: "ajax_getMovieHTML.asp",
        data: "id=" + movieID + "&rnd=" + Math.random(),
        success: function(movieHTML) {

            if (jQuery.inArray(movieID, ListyFilmow.mojProgram) >= 0) return false;
            
            movieHTMLObj = $(movieHTML).hoverClass();
            if (autoplay) {            
                $('div#jq-blokM-mojprogram-lista').prepend(movieHTMLObj);

                ListyFilmow.mojProgram.unshift(movieID);
                iHPlayer_loadThisMoviesAsPlayList(ListyFilmow.mojProgram, 'mojprogram', function() { 
                    iHPlayer_playThisIdFromLoadedPlaylist(movieID);
                });
            }
            else {
            
                if(movieHTMLObj.attr("premium") > 0){                    
                   $('div#lista-premium').append(movieHTMLObj);
                }
                else {
                   $('div#lista-open').append(movieHTMLObj);
                }
              
                ListyFilmow.mojProgram.push(movieID);
                if (ListyFilmow.aktualnaLista == 'mojprogram') { iHPlayer_addMovieByID(movieID); }
            }
            if ($.cookie('session_mojprogram') != null) {
                var tmpListaFilmow = $.cookie('session_mojprogram').split('|');
                
                if (jQuery.inArray(movieID, tmpListaFilmow) == -1) {
                    
                    if(autoplay) 
                        tmpListaFilmow.unshift(movieID);
                    else 
                        tmpListaFilmow.push(movieID);                                                                     
                    
                    $.cookie('session_mojprogram', tmpListaFilmow.join('|'));
                }                               
            }
            else 
                $.cookie('session_mojprogram', movieID);
            
            ListyFilmow.listSaved = false;

            updateMyProgramInfo();
            updateListHover();
                        
            $('#jq-blokM-container').jScrollPane(styleScrollBar);
        }
    });
    
    return false;
}


ListyFilmow.playThisMovieFromPlaylist = function(event) {
    var callingObj = $(event.target);

    if (!callingObj.hasClass('jq-movie')) callingObj = callingObj.parents('.jq-movie').eq(0);
    var movieIdentificator = callingObj.attr('id').split("-");   
 
    if (movieIdentificator[1] == ListyFilmow.aktualnaLista && ListyFilmow.currentPlaylist == ListyFilmow.aktualnaLista) {           
        iHPlayer_playThisIdFromLoadedPlaylist(movieIdentificator[0]);
        showInfoMovie(movieIdentificator[0], movieIdentificator[1]);               
    }
    else {      
        if (movieIdentificator[1] == 'mojprogram') {
            iHPlayer_loadThisMoviesAsPlayList(ListyFilmow.mojProgram, 'mojprogram', function() {
                iHPlayer_playThisIdFromLoadedPlaylist(movieIdentificator[0]);
            });
        }
        else {        
            iHPlayer_loadProgramOIdentyfikatorze(movieIdentificator[1], function() {
                iHPlayer_playThisIdFromLoadedPlaylist(movieIdentificator[0]);
            });
        }

        showInfoMovie(movieIdentificator[0], movieIdentificator[1]);
        ListyFilmow.currentPlaylist = movieIdentificator[1];

        //setTimeout('iHPlayer_playThisIdFromLoadedPlaylist("' + movieIdentificator[0] + '")', 500);
    }
    
    return false;
}



ListyFilmow.oznaczJakoOgladniety = function(movieID) {
	ListyFilmow.ogladnieteFilmy.push(movieID);	
 	$.cookie('ogladnietefilmy', ListyFilmow.ogladnieteFilmy.join('|'), { expires: 20 });
}


ListyFilmow.bindSwitchListGTabButton = function(event) {
    setHDToMinHD();
    
    var callingObj = $(event.target);
    var menuTabow = callingObj.parents('.jq-ajax-tabs');

    menuTabow.find('a').removeClass('active');
    callingObj.addClass('active');

    var listaIdent = callingObj.attr('href').slice(1);

    if (listaIdent.length > 0) {
        $('#' + menuTabow.attr('id') + '-' + listaIdent).show().siblings().hide();

        if (listaIdent == 'mojprogram') {
            ListyFilmow.aktualnaLista = "mojprogram";
            updateMyProgramInfo();            
        }
    }

    $('#jq-blokM-container').jScrollPane(styleScrollBar);
    $('#jq-blokM-container')[0].scrollTo(0);
   
    return false;
}


ListyFilmow.bindSwitchListDTabButton = function(event) {
    var callingObj = $(event.target);
    var menuTabow = callingObj.parents('.jq-ajax-tabs');
    var kontenerTabow = $('#' + menuTabow.attr('id') + '-container');

    menuTabow.find('a').removeClass('active');
    callingObj.addClass('active');

    var akcjaIdent = callingObj.attr('href').slice(1);
    if (akcjaIdent.length > 0) {        
        var kontenerTejListy = $('#' + menuTabow.attr('id') + '-' + akcjaIdent);        
        if (!callingObj.hasClass('jq-forcereload') && kontenerTejListy.size() == 1) {
            kontenerTejListy.show().siblings().hide();
            return false;
        }        

        kontenerTabow.showAjaxIndicator();
        
        $.ajax({
            type: "POST",
            url: "ajax_getBottomContent.asp",
            data: "akcjaIdent=" + akcjaIdent + "&typ=1&currentMovieID=" + ListyFilmow.currentMovieID + "&rnd=" + Math.random(),
            success: function(html) {
                
                HTMLObj = $(html).hoverClass();

                if (kontenerTejListy.size() == 1) {
                    kontenerTejListy.empty().append(HTMLObj);
                }
                else {                    
                    kontenerTejListy = $('<div id="' + menuTabow.attr('id') + '-' + akcjaIdent + '"></div>');
                    kontenerTejListy.append(HTMLObj);

                    kontenerTabow.append(kontenerTejListy);
                }

                kontenerTejListy.show().siblings().hide();
                kontenerTabow.hideAjaxIndicator();
            },
            error: function() {
                kontenerTabow.hideAjaxIndicator();
            }
        });
    }

    return false;
}


ListyFilmow.bindSwitchListATabButton = function(event) {
    var callingObj = $(event.target);
    var menuTabow = callingObj.parents('.jq-ajax-buttons');

    var kontenerProgramow = $('div#jq-blokM-container');

    var actionIdent = callingObj.attr('href').slice(1);
    if (actionIdent.length > 0) {
        kontenerProgramow.showAjaxIndicator();

        $.ajax({
            type: "POST",
            url: "ajax_getPlaylistHTML.asp",
            data: "listaIdent=" + actionIdent + "&rnd=" + Math.random(),
            success: function(html) {

                HTMLObj = $(html).hoverClass();

                $('#jq-blokM-program-lista', kontenerProgramow).empty().append(HTMLObj);
                $('ul#jq-blokM.jq-ajax-tabs a[href="#program"]').click().text(callingObj.text());

                iHPlayer_loadProgramOIdentyfikatorze(actionIdent);
                kontenerProgramow.hideAjaxIndicator();

                updateListHover();
            },
            error: function() {
                kontenerProgramow.hideAjaxIndicator();
            }
        });
    }
    
    return false;
}


function pokarzInformacjeDlaFilmu(id) {
    $.ajax({
        type: "POST",
        url: "ajax_getMovieInfo.asp",
        data: "movieID=" + id  + "&rnd=" + Math.random(),
        success: function(html) {                    
            $("#jq-movie-info").empty().append(html).toggle(true);
            pageTracker._trackPageview('AJAX?ID=' + id);
        }
    });    
}

function updateMyProgramInfo() {
    $('#mojprogram-info').toggle(ListyFilmow.mojProgram.length == 0);
    
    $('#jq-mojprogram-btns-top').toggle(ListyFilmow.mojProgram.length > 0);    
    $("#jq-mojprogram-btns-end").toggle(ListyFilmow.mojProgram.length > 0);    
    
    $('#jq-mojprogram-save-top').toggle(!ListyFilmow.listSaved);        
    $('#jq-mojprogram-save-end').toggle(!ListyFilmow.listSaved);
    
    $('#jq-mojprogram-delete-end').toggle(ListyFilmow.listSaved);    
    $('#jq-mojprogram-delete-top').toggle(ListyFilmow.listSaved);    
       
    $('div#jq-blokM-mojprogram-lista .prem_bar').toggle($("#lista-premium").children().size() > 0);
}

function showInfoMovie(movieID, playlist) {

    if (ListyFilmow.currentMovieID == movieID && playlist == ListyFilmow.aktualnaLista) return false;

    if (playlist != ListyFilmow.currentPlaylist && ListyFilmow.currentPlaylist != null ) {
        $("#" + ListyFilmow.currentMovieID + "-" + ListyFilmow.currentPlaylist).removeClass("active").addClass("visited");        
        $("#" + ListyFilmow.currentMovieID + "-" + ListyFilmow.currentPlaylist).find(".buttonControl[type='minus']").removeClass("buttonControl-active").addClass("buttonControl-disable");
        $("#" + ListyFilmow.currentMovieID + "-" + ListyFilmow.currentPlaylist).find(".buttonControl[type='play'], .buttonControl[type='day'], .buttonControl[type='promo']").removeClass("buttonControl-enable").addClass("buttonControl-disable");
    }

    var scrollPos = $("#" + movieID + "-" + playlist).position().top - (2 * $("#" + movieID + "-" + playlist).height());

    if (ListyFilmow.currentMovieID == null) {
        $("#" + movieID + "-" + playlist).removeClass("normal").addClass("active");
        $("#" + movieID + "-" + playlist).find(".buttonControl[type='minus']").addClass("buttonControl-active");
        $("#" + movieID + "-" + playlist).find(".buttonControl[type='play'], .buttonControl[type='day'], .buttonControl[type='promo']").addClass("buttonControl-enable");
        
        pokarzInformacjeDlaFilmu(movieID);

        $('#jq-blokM-container').jScrollPane(styleScrollBar);
        $('#jq-blokM-container')[0].scrollTo(scrollPos);
    }
    else {
        ListyFilmow.oznaczJakoOgladniety(ListyFilmow.currentMovieID);

        $("#" + movieID + "-" + playlist).removeClass("normal").addClass("active");
        $("#" + movieID + "-" + playlist).find(".buttonControl[type='play'], .buttonControl[type='day'], .buttonControl[type='promo']").addClass("buttonControl-enable");

        $("#" + ListyFilmow.currentMovieID + "-" + playlist).removeClass("active").addClass("visited");
        $("#" + ListyFilmow.currentMovieID + "-" + playlist).find(".buttonControl[type='minus']").removeClass("buttonControl-active").addClass("buttonControl-disable");
        $("#" + ListyFilmow.currentMovieID + "-" + playlist).find(".buttonControl[type='play'], .buttonControl[type='day'], .buttonControl[type='promo']").removeClass("buttonControl-enable").addClass("buttonControl-disable");
        
        pokarzInformacjeDlaFilmu(movieID);
        $('#jq-blokM-container')[0].scrollTo(scrollPos);        
    }

    ListyFilmow.currentMovieID = movieID;
    $('ul#jq-blokD.jq-ajax-tabs li a[href="#komentarze"]').click();
}

ListyFilmow.zapiszMojProgram = function(event) {
    $.cookie('mojprogram', ListyFilmow.mojProgram.join('|'), { expires: 10 });

    ListyFilmow.listSaved = true;
    updateMyProgramInfo();

    return false;
}

ListyFilmow.wyczyscMojProgram = function(event) {

    ListyFilmow.mojProgram = new Array();
    $.cookie('mojprogram', null);
    $.cookie('session_mojprogram', null);
            
    $('#lista-open, #lista-premium').empty();
    updateMyProgramInfo();
    
    $('#jq-blokM-container').jScrollPane(styleScrollBar);
    return false;
}



ListyFilmow.wczytajMojProgram = function(movieIDzParametru) {    

    var tmpListaFilmow = new Array();
           
    if ($.cookie('mojprogram') != null) {    
        tmpListaFilmow = ListyFilmow.mojProgram.concat($.cookie('mojprogram').split('|'));
    }   
           
    if ($.cookie('session_mojprogram') != null) {    
        var tmp_session = $.cookie('session_mojprogram').split('|');
        
        for(var i=0; i<tmp_session.length; i++) {
            if(jQuery.inArray(tmp_session[i], tmpListaFilmow) == -1) {
                tmpListaFilmow.push(tmp_session[i]);
            }
        }                  
    }        
       
    if (movieIDzParametru.length > 1 && jQuery.inArray(movieIDzParametru, tmpListaFilmow) == -1) {
        tmpListaFilmow.unshift(movieIDzParametru); }
       
    if(tmpListaFilmow != null) {    
        for(var i=0; i<tmpListaFilmow.length; i++) {        
            ListyFilmow.addMovieToMojProgram(tmpListaFilmow[i], false);
        }
    }
    
}


function searchListByParam(word, category, page) {
    $.ajax({
        type: "POST",
        url: "ajax_getSearchList.asp",
        data: "count=15&word=" + word + "&category=" + category + "&page=" + page + "&rnd=" + Math.random(),
        success: function(html) {
            $('ul#jq-blokD.jq-ajax-tabs li a[href="#wyniki"]').click();

            $('div#jq-item-list').empty().append(html);            
            updateListHover();
            
            $(".jq-nextpage").click(function() { return searchListByParam(word, category, page + 1); });
            $(".jq-prevpage").click(function() { return searchListByParam(word, category, page - 1); });            
        },
        error: function() { }
    });

    return false;
}

function updateBottomItemHover() {
    $(".bottomItemNode .mouseOverLayer, .bottomItemNode .buttonLayer").live('mouseover', function() {
        var this_index = $(".bottomItemNode .mouseOverLayer").index(this);
        if (this_index == -1) { this_index = $(".bottomItemNode .buttonLayer").index(this); }

        $(".bottomItemNode p:eq(" + this_index + ")").stop().animate({
            marginTop: "0",
            height: "80px"           
        }, "normal");

        $(".bottomItemNode .buttonLayer:eq(" + this_index + ")").stop().animate({
            marginTop: "80px",
            height: "29px"
        }, "normal");

        $(".bottomItemNode .shadowLayer:eq(" + this_index + ")").css({
            'margin-top': "1px",
            'height': "108px"
        });

    }).live('mouseout', function() {
        var this_index = $(".bottomItemNode .mouseOverLayer").index(this);
        if (this_index == -1) { this_index = $(".bottomItemNode .buttonLayer").index(this); }

        $(".bottomItemNode p:eq(" + this_index + ")").stop().animate({
            marginTop: "66px",
            height: "25px"            
        }, "fast", function() {

            $(".bottomItemNode .shadowLayer:eq(" + this_index + ")").css({
                'margin-top': "76px",
                'height': "33px"
            });        
        });

        $(".bottomItemNode .buttonLayer:eq(" + this_index + ")").stop().animate({
            marginTop: "110px",
            height: "0"
        }, "fast");
    });
}

function updateListHover() {

    $(".jq-movie").hover(function() {
        var id = $(this).attr("id").split('-')[0];

        if (ListyFilmow.currentMovieID != id) {
            $(this).find(".buttonControl[type='play'], .buttonControl[type='plus']").addClass("buttonControl-enable");
            $(this).find(".buttonControl[type='minus'], .buttonControl[type='day'], .buttonControl[type='promo']").removeClass("buttonControl-disable").addClass("buttonControl-active");
        }

    }, function() {
        var id = $(this).attr("id").split('-')[0];

        if (ListyFilmow.currentMovieID != id) {
            if (jQuery.inArray(id, ListyFilmow.ogladnieteFilmy) >= 0) {
                $(this).find(".buttonControl[type='play'], .buttonControl[type='plus']").removeClass("buttonControl-enable").addClass("buttonControl-disable");
                $(this).find(".buttonControl[type='minus'], .buttonControl[type='day'], .buttonControl[type='promo']").removeClass("buttonControl-active").addClass("buttonControl-disable");
            }
            else {
                $(this).find(".buttonControl[type='play'], .buttonControl[type='plus']").removeClass("buttonControl-enable");
                $(this).find(".buttonControl[type='minus'], .buttonControl[type='day'], .buttonControl[type='promo']").removeClass("buttonControl-active");
            }
        }
    });   
}

function initPanelEvent() {
    $("#categoryPanel").mouseenter(function() { clearTimeout(timerCategory); })
        .mouseleave(function() { timerCategory = setTimeout("$('#categoryPanel').slideUp('fast');", 100); });        
    
    $("#kategoria").mouseenter( function() { clearTimeout(timerCategory); } )
        .mouseleave(function() { timerCategory = setTimeout("$('#categoryPanel').slideUp('fast');", 100); })
        .click(function() { $("#categoryPanel").slideDown("fast"); return false; } );
        
    $("#topgamePanel").mouseenter(function() { clearTimeout(timerTop); })
    .mouseleave(function() { timerTop = setTimeout("$('#topgamePanel').slideUp('fast');", 100); });
    
    $("#topgry").mouseenter( function() { clearTimeout(timerTop); } )
        .mouseleave(function() { timerTop = setTimeout("$('#topgamePanel').slideUp('fast');", 100); })
        .click(function() { $("#topgamePanel").slideDown("fast"); return false; });
                
    $("#topgamePanel .link").click(function() { 
        $("#topgamePanel").slideUp("fast");            
        
        var moviesIDs = $(this).attr("href").slice(1).split('|');                        
                                   
        var startPosition = $(this).offset();
        var endPosition = $("#jq-blokM-mojprogram-lista").offset();
        
        var elem = $("<div class='ui-effects-transfer'></div>")
            .appendTo("body")
            .css({
                top: startPosition.top,
                left: startPosition.left,
                'background-image' : 'url(' + $(this).children("img").attr("src") + ')'
            })
            .animate({
                top: endPosition.top,                    
                left: endPosition.left,
                height: '30px',
                width: '54px',
                opacity: 0.5
            }, "normal", "linear", function() { 
                elem.remove(); 
                for(var i=0; i<moviesIDs.length; i++) { ListyFilmow.addMovieToMojProgram(moviesIDs[i], false); }
            });            
        
        return false;
    });
    
    $("#categoryPanel .link").click(function() {  
        $("#categoryPanel").slideUp("fast"); 
          
        var item = $(this);           
        var startPosition = $(this).offset();
        var endPosition = $("ul#jq-blokD.jq-ajax-tabs li a[href=#wyniki]").offset();        
        
        var elem = $("<div class='ui-effects-transfer'></div>")
            .appendTo("body")
            .css({
                top: startPosition.top,
                left: startPosition.left,
                'background-image' : 'url(' + $(this).children("img").attr("src") + ')'
            })
            .animate({
                top: endPosition.top,                    
                left: endPosition.left,
                height: '30px',
                width: '54px',
                opacity: 0.5
            }, "normal", "linear", function() { 
                elem.remove(); 
        
                searchListByParam("", item.attr("href").slice(1), 0);                       
                $('ul#jq-blokD.jq-ajax-tabs a[href="#wyniki"]').click();        
            });
                            
        return false;                
    });
    
    $(".panelfloat .link").tooltip({track: true, delay: 150, showURL: false, showBody: " - ", bodyHandler: function() { return $(this).attr("name"); } })

}

jQuery.fn.extend( {
    showAjaxIndicator: function(emptyBefore) { 
    
        if (emptyBefore == true) { $(" > *", this).remove(); }
		else { $(" > *", this).hide(); }
		
		if ($('body').hasClass('szary'))
			this.css('background', 'url(images/ajax-loader-white.gif) no-repeat 50% 50%');
		else
			this.css('background', 'url(images/ajax-loader.gif) no-repeat 50% 50%');
    },
    hideAjaxIndicator: function() { 
        this.css('background-image', 'none');
    },
    hoverClass: function() { 
		this.bind('mouseover', function(){
					$(this).addClass('hover')
				}).bind('mouseout', function(){
					$(this).removeClass('hover')
				});
		return this;				
    }
});



/* jquery rating */

if(window.jQuery) (function($){
	
	if ($.browser.msie) try { document.execCommand("BackgroundImageCache", false, true)} catch(e) { };
	
	$.fn.rating = function(options){
		if(this.length==0) return this;
		
		if(typeof arguments[0]=='string') {
			if(this.length>1){
				var args = arguments;
				return this.each(function(){ $.fn.rating.apply($(this), args); });
			};
			
			$.fn.rating[arguments[0]].apply(this, $.makeArray(arguments).slice(1) || []);
			return this;
		};
		
		var options = $.extend( {}, $.fn.rating.options, options || {} );		
		$.fn.rating.calls++;
		
		this.not('.star-rating-applied').addClass('star-rating-applied').each(function(){
			
			var control, input = $(this);
			var eid = (this.name || 'unnamed-rating').replace(/\[|\]/g, '_').replace(/^\_+|\_+$/g,'');
			var context = $(this.form || document.body);
			
			var raters = context.data('rating');
			if(!raters || raters.call!=$.fn.rating.calls) raters = { count:0, call:$.fn.rating.calls };
			var rater = raters[eid];
			
			if(rater) control = rater.data('rating');
			
			if(rater && control)
				control.count++;				
			else{
				control = $.extend( {},
					options || {}, ($.metadata? input.metadata(): ($.meta?input.data():null)) || {},
					{ count:0, stars: [], inputs: [] }
				);
				
				control.serial = raters.count++;
				
				rater = $('<span class="star-rating-control"/>');
				input.before(rater);
				
				rater.addClass('rating-to-be-drawn');				
				if(input.attr('disabled')) control.readOnly = true;												
			};
			
			var star = $('<div class="star-rating rater-'+ control.serial +'"><a title="' + (this.title || this.value) + '">' + this.value + '</a></div>');
			rater.append(star);
			
			if(this.id) star.attr('id', this.id);
			if(this.className) star.addClass(this.className);
			
			if(control.half) control.split = 2;
			
			if(typeof control.split=='number' && control.split>0){
				var stw = ($.fn.width ? star.width() : 0) || control.starWidth;
				var spi = (control.count % control.split), spw = Math.floor(stw/control.split);
				star.width(spw).find('a').css({ 'margin-left':'-'+ (spi*spw) +'px' })
			};
			
			if(control.readOnly) {
				star.addClass('star-rating-readonly');
			}
			else {
				star.addClass('star-rating-live')			
					.mouseover(function(){
						$(this).rating('fill');
						$(this).rating('focus');
					})
					.click(function(){
						$(this).rating('select');
						control.is_rated = true;
						var input = $( control.current ? control.current.data('rating.input') : null );
						if(control.callback) control.callback.apply(input[0], [input.val(), control.is_rated]);
					})
					.parent().mouseleave(function() 
					{ 
					    star.rating('draw');
						star.rating('blur'); 						
					});
			};
			
			if(this.checked) control.current = star;
			
			input.hide();			
			input.change(function(){ $(this).rating('select'); });
			
			star.data('rating.input', input.data('rating.star', star));
			
			control.stars[control.stars.length] = star[0];
			control.inputs[control.inputs.length] = input[0];
			control.rater = raters[eid] = rater;
			control.context = context;
			
			input.data('rating', control);
			rater.data('rating', control);
			star.data('rating', control);
			context.data('rating', raters);
 
  });
		
		$('.rating-to-be-drawn').rating('draw').removeClass('rating-to-be-drawn');		
		return this;
	};
	

	$.extend($.fn.rating, {
		calls: 0,
		
		focus: function(){
			var control = this.data('rating'); if(!control) return this;
			if(!control.focus) return this;
			
			var input = $(this).data('rating.input') || $( this.tagName=='INPUT' ? this : null );
			if(control.focus) control.focus.apply(input[0], [input.val(), control.is_rated]);
		},
		
		blur: function(){
			var control = this.data('rating'); if(!control) return this;
			if(!control.blur) return this;
			
			var input = $(this).data('rating.input') || $( this.tagName=='INPUT' ? this : null );
			if(control.blur) control.blur.apply(input[0], [input.val(), control.is_rated]);
		},
		
		fill: function(){
			var control = this.data('rating'); if(!control) return this;		
			if(control.readOnly) return;
		
			this.rating('drain');
			this.prevAll().andSelf().filter('.rater-'+ control.serial).addClass('star-rating-hover');
		},
		
		drain: function() {
			var control = this.data('rating'); if(!control) return this;
			if(control.readOnly) return;
			
			control.rater.children().filter('.rater-'+ control.serial).removeClass('star-rating-on').removeClass('star-rating-hover');
		},
		
		draw: function(){
			var control = this.data('rating'); if(!control) return this;			
			this.rating('drain');
			
			if(control.current){
				control.current.data('rating.input').attr('checked','checked');
				control.current.prevAll().andSelf().filter('.rater-'+ control.serial).addClass('star-rating-on');
			}
			else $(control.inputs).removeAttr('checked');			
			
			this.siblings()[control.readOnly?'addClass':'removeClass']('star-rating-readonly');
		},
		
		select: function(value){
			var control = this.data('rating'); if(!control) return this;
			if(control.readOnly) return;
						
			control.current = null;
			if(typeof value!='undefined'){
				if(typeof value=='number') return $(control.stars[value]).rating('select');
 			 
				if(typeof value=='string')
					$.each(control.stars, function(){ if($(this).data('rating.input').val()==value) $(this).rating('select'); });
			}
			else
				control.current = this[0].tagName=='INPUT' ? this.data('rating.star') : (this.is('.rater-'+ control.serial) ? this : null);
		
			
			this.data('rating', control);
			this.rating('draw');
		},
		
		readOnly: function(toggle, disable){
			var control = this.data('rating'); if(!control) return this;			
			
			control.readOnly = toggle || toggle==undefined ? true : false;						
			
			if(disable) $(control.inputs).attr("disabled", "disabled");
			else  $(control.inputs).removeAttr("disabled");
			
			this.data('rating', control);
			this.rating('draw');
		},
		
		disable: function(){ this.rating('readOnly', true, true); },		
		enable: function(){ this.rating('readOnly', false, false); }		
 });
	
	$.fn.rating.options = { starWidth: 19, half: true };
		
})(jQuery);


/* jquery tooltip */

(function($) {
	
	var helper = {},
		current,
		title,
		tID,
		IE = $.browser.msie && /MSIE\s(5\.5|6\.)/.test(navigator.userAgent),
		track = false;
	
	$.tooltip = {
		blocked: false,
		defaults: {
			delay: 200,
			fade: false,
			showURL: true,
			extraClass: "",
			top: 15,
			left: 15,
			id: "tooltip"
		},
		block: function() { $.tooltip.blocked = !$.tooltip.blocked; }
	};
	
	$.fn.extend({
		tooltip: function(settings) {
			settings = $.extend({}, $.tooltip.defaults, settings);
			createHelper(settings);
			return this.each(function() {
					$.data(this, "tooltip", settings);
					this.tOpacity = helper.parent.css("opacity");
					this.tooltipText = this.title;
					$(this).removeAttr("title");
					this.alt = "";
				})
				.mouseover(save)
				.mouseout(hide)
				.click(hide);
		},
		fixPNG: IE ? function() {
			return this.each(function () {
				var image = $(this).css('backgroundImage');
				if (image.match(/^url\(["']?(.*\.png)["']?\)$/i)) {
					image = RegExp.$1;
					$(this).css({
						'backgroundImage': 'none',
						'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=crop, src='" + image + "')"
					}).each(function () {
						var position = $(this).css('position');
						if (position != 'absolute' && position != 'relative')
							$(this).css('position', 'relative');
					});
				}
			});
		} : function() { return this; },
		unfixPNG: IE ? function() {
			return this.each(function () {
				$(this).css({'filter': '', backgroundImage: ''});
			});
		} : function() { return this; },
		hideWhenEmpty: function() {
			return this.each(function() {
				$(this)[ $(this).html() ? "show" : "hide" ]();
			});
		},
		url: function() { return this.attr('href') || this.attr('src'); }
	});
	
	function createHelper(settings) {
		if( helper.parent ) return;
			
		helper.parent = $('<div id="' + settings.id + '"><h3></h3><div class="body"></div><div class="url"></div></div>')
			.appendTo(document.body).hide();
			
		if ( $.fn.bgiframe ) helper.parent.bgiframe();
		
		helper.title = $('h3', helper.parent);
		helper.body = $('div.body', helper.parent);
		helper.url = $('div.url', helper.parent);
	}
	
	function settings(element) { return $.data(element, "tooltip"); }
	
	function handle(event) {
		if( settings(this).delay )
			tID = setTimeout(show, settings(this).delay);
		else
			show();
		
		track = !!settings(this).track;
		$(document.body).bind('mousemove', update);
			
		update(event);
	}
	
	function save() {
		if ( $.tooltip.blocked || this == current || (!this.tooltipText && !settings(this).bodyHandler) )
			return;

		current = this;
		title = this.tooltipText;
		
		if ( settings(this).bodyHandler ) {
			helper.title.hide();
			var bodyContent = settings(this).bodyHandler.call(this);
			if (bodyContent.nodeType || bodyContent.jquery) {
				helper.body.empty().append(bodyContent)
			} else {
				helper.body.html( bodyContent );
			}
			helper.body.show();
		} else if ( settings(this).showBody ) {
			var parts = title.split(settings(this).showBody);
			helper.title.html(parts.shift()).show();
			helper.body.empty();
			for(var i = 0, part; (part = parts[i]); i++) {
				if(i > 0) helper.body.append("<br/>");
				helper.body.append(part);
			}
			helper.body.hideWhenEmpty();
		} else {
			helper.title.html(title).show();
			helper.body.hide();
		}
		
		if( settings(this).showURL && $(this).url() )
			helper.url.html( $(this).url().replace('http://', '') ).show();
		else 
			helper.url.hide();
		
		helper.parent.addClass(settings(this).extraClass);

		if (settings(this).fixPNG ) helper.parent.fixPNG();			
		handle.apply(this, arguments);
	}
	
	function show() {
		tID = null;
		if ((!IE || !$.fn.bgiframe) && settings(current).fade) {
			if (helper.parent.is(":animated"))
				helper.parent.stop().show().fadeTo(settings(current).fade, current.tOpacity);
			else
				helper.parent.is(':visible') ? helper.parent.fadeTo(settings(current).fade, current.tOpacity) : helper.parent.fadeIn(settings(current).fade);
		} else {
			helper.parent.show();
		}
		update();
	}
		
	function update(event)	{
		if($.tooltip.blocked) return;
		
		if (event && event.target.tagName == "OPTION") { return; }
		
		if ( !track && helper.parent.is(":visible")) { $(document.body).unbind('mousemove', update) }
		
		if( current == null ) {
			$(document.body).unbind('mousemove', update);
			return;	
		}
		
		helper.parent.removeClass("viewport-right").removeClass("viewport-bottom");
		
		var left = helper.parent[0].offsetLeft;
		var top = helper.parent[0].offsetTop;
		if (event) {
			left = event.pageX + settings(current).left;
			top = event.pageY + settings(current).top;
			var right='auto';
			if (settings(current).positionLeft) {
				right = $(window).width() - left;
				left = 'auto';
			}
			helper.parent.css({ left: left, right: right, top: top });
		}
		
		var v = viewport(),
			h = helper.parent[0];
			
		if (v.x + v.cx < h.offsetLeft + h.offsetWidth) {
			left -= h.offsetWidth + 20 + settings(current).left;
			helper.parent.css({left: left + 'px'}).addClass("viewport-right");
		}
		
		if (v.y + v.cy < h.offsetTop + h.offsetHeight) {
			top -= h.offsetHeight + 20 + settings(current).top;
			helper.parent.css({top: top + 'px'}).addClass("viewport-bottom");
		}
	}
	
	function viewport() {
		return { x: $(window).scrollLeft(), y: $(window).scrollTop(), cx: $(window).width(), cy: $(window).height() };
	}
	
	function hide(event) {
		if($.tooltip.blocked) return;
			
		if(tID) clearTimeout(tID);
		current = null;
		
		var tsettings = settings(this);
		function complete() { helper.parent.removeClass( tsettings.extraClass ).hide().css("opacity", ""); }
		if ((!IE || !$.fn.bgiframe) && tsettings.fade) {
			if (helper.parent.is(':animated'))
				helper.parent.stop().fadeTo(tsettings.fade, 0, complete);
			else
				helper.parent.stop().fadeOut(tsettings.fade, complete);
		} else
			complete();
		
		if( settings(this).fixPNG ) helper.parent.unfixPNG();
	}
	
})(jQuery);