 jQuery=jQuery.noConflict();
;(function($) {


    var defaults = {

        pagerText: 'div#pager span',

        pagerNext: 'div#pager div#next',

        pagerPrev: 'div#pager div#prev',

        pages: 'div#pages',

        selectedRound: 10,
	
        selectedRoundText: '%s. runde',

        numRounds: 33

    };

 



    $.fn.round = function(settings) {

	$.extend(this, {



	    init: function() {
                		
                this.design();

                this.makePrevNextLinks();
                			
	    },

            design: function() {

                $this = this.children();

                $.each($this, function(index) {

                    $elem = $($this[index]);

                    if (index == 0) {$elem.attr('class', 'first');}

                    switch( $elem.find('ul').attr('class') ) {
                        case 'played':
							$elem.find('li:eq(9)').addClass('listitem livecast').find('a').empty();
							$elem.find('li:eq(5)').remove('.datetop')
							break;
                        case 'playing':
							$elem.find('li:eq(9)').addClass('listitem livecast').find('a').empty();
							$elem.find('li:eq(5)').remove('.datetop')
                            break;
                        case 'notplayed':
							$elem.find('li:eq(9)').addClass('listitem livecast').find('a').empty();
							$elem.find('li:eq(4)').remove('.awayscore')
							$elem.find('li:eq(3)').remove('.spacerbetweenscore')
							$elem.find('li:eq(2)').remove('.homescore')
                            break;
                    }

                });

                var $score = $this.find('li.homescore');
                $.each($score, function(index) {
                    round.setGoalImages( $($score[index]) );
                });

                var $score = $this.find('li.awayscore');
                $.each($score, function(index) {
                    round.setGoalImages( $($score[index]) );
                });

                this.designPager();              
            },

            setGoalImages: function(elm) {
                elm.addClass('s' + elm.html()).addClass('goalscore');
            },

            designPager: function() {
                var $_this = this;

                $(this.pagerText).empty().append(this.selectedRoundText.replace('%s', this.selectedRound));
                $pages = $(this.pages);

                if ($pages.find('ul').length == 0) {
                    $elem = $pages.clone();
                    $elem.append($('<ul />'));
                    for(var s=1;s<=this.numRounds;s++) {
                        var a = $('<a title="' + this.selectedRoundText.replace('%s', s) + '" href="#" class="p' + s + '">&nbsp;</a>').click(function() {
                            c = $(this).attr('class').split(' ');
                            $_this.getRound( parseInt(c[0].substr(1)) );
                            return false;
                        });
                        extraClass = '';
                        if (s == this.selectedRound) { extraClass = 'selected'; }

                        $elem.find('ul').append($('<li class="' + extraClass + '" />').append(a));
                    }
                    $pages.replaceWith($elem);
                } else {
                    $pages.find('ul').find('li').removeClass('selected');
                    $pages.find('ul').find('li').eq((this.selectedRound-1)).addClass('selected');
                }
            },

            makePrevNextLinks: function() {                        
                $(this.pagerNext).click(function() {			
					round.next();		
                });

                $(this.pagerPrev).click(function() {
                    round.prev();
                });
            },

            prev: function() {
                this.getRound( (this.selectedRound-1) );
            },

            next: function() {
                this.getRound( (this.selectedRound+1) );
            },

            setSelectedRound: function(index) {
                this.selectedRound = index;
                this.design();
            },

            getRound: function(index) {
                if (index < 1) {index = 1;}
                if (index > this.numRounds) {index = this.numRounds;}
                this.setSelectedRound(index);
                jQuery.ajax({
						ifModified: true,
						type: 'POST',
						//url:'index.php?id=3&type=201&tx_slscoreboardlive_pi2[uid]='+index,
						url:'livescoreCacheGetter.php?roundNumber='+index,
						dataType: 'html',
						beforeSend: function() {
							jQuery('#rounds').html('<div style="height:139px;padding-top:20px;padding-left:80px;margin-left:380px;"><img src="fileadmin/templates/page/images/ajax-loader(3).gif"/></div>');
						},
						success: function(data) {
							jQuery('#rounds').html(data);
							round.setSelectedRound(index);
						},
						error: function(httpReq, textStatus) {
							console.log(httpReq, textStatus);
						}
				});													
            }
	});



	$.extend(this, defaults, settings);

	var round = this;
	round.init();	

    }



})(jQuery);

function refreshround() {
	var tc1 = jQuery("#pages ul li.selected a").attr('class');
	if (typeof tc1!="undefined") {
	  var tc = tc1.split(' ');
	  var selectedround = parseInt(tc[0].substr(1));
	  if (currentround_db == selectedround) {
		jQuery("#pages ul li.selected a").click();
	  } 
	}
}

 setInterval('refreshround()',30000);

