jQuery(window).load(function() {

    if (jQuery('#Main h1 span').length) {
        Cufon.replace('#Main h1 span', {
            textShadow: '#517537 1px 1px, #517537 -1px -1px'
        });
    }

    if (jQuery('#SideBar ul a').length) {
        Cufon.replace('#SideBar ul a', {
            color: '-linear-gradient(#929190, #d6d4d4)',
            textShadow: '#1e1e1e 1px 1px, #1e1e1e -1px -1px',
            fontWeight: 'bold',
            textTransform: 'uppercase'
        });
    }

    //    if (jQuery('ul.mainNav ul a').length) {
    //        Cufon.replace('ul.mainNav ul a', {
    //            color: '-linear-gradient(#929190, #d6d4d4)',
    //            textShadow: '#1e1e1e 1px 1px, #1e1e1e -1px -1px',
    //            fontWeight: 'bold',
    //            textTransform: 'uppercase'
    //        });
    //    }

    if (jQuery('#SideInfo h1 span').length) {
        Cufon.replace('#SideInfo h1 span', {
            color: '-linear-gradient(#f5f5f5, #c9c8c7)',
            textShadow: '#1e1e1e 1px 1px, #1e1e1e -1px -1px',
            fontWeight: 'bold'
        });
    }

    // DB 16-8-09 commented out as bug with IE, text goes funny
    //    if(jQuery('ul.tabs li a').length){
    //        Cufon.replace('ul.tabs li a',{
    //            color: '-linear-gradient(#d6d4d4,#929190)',
    //            textShadow:'#1e1e1e 1px 1px, #1e1e1e -1px -1px',
    //            fontWeight:'bold'
    //        });
    //    }

    if (jQuery('#SideBar ul li.selected a').length) {
        Cufon.replace('#SideBar ul li.selected a', {
            color: '#fff',
            textShadow: '#517537 1px 1px, #517537 -1px -1px',
            fontWeight: 'bold',
            textTransform: 'uppercase'
        });
    }

    if (jQuery('.yourHandicap span').length) {
        Cufon.replace('.yourHandicap span', {
            color: '#fff',
            textShadow: '#58783e 1px 1px, #58783e -1px -1px',
            fontWeight: 'bold',
            textTransform: 'uppercase'
        });
    }

    if (jQuery('.projectedHandicap span').length) {
        Cufon.replace('.projectedHandicap span', {
            color: '#fff',
            textShadow: '#37757c 1px 1px, #37757c -1px -1px',
            fontWeight: 'bold',
            textTransform: 'uppercase'
        });
    }


    //The same thing can be achieved by :focus{ -moz-outline-style: none; }
    if (jQuery.browser.mozilla) {
        jQuery("a").each(function() {
            var element = jQuery(this);
            element.bind("focus", function() {
                element.css("-moz-outline-style", "none");
            });
        });
    }

    var startValue = jQuery('input.SearchField').val();

    jQuery('input.SearchField').focus(function() {
        if (jQuery(this).val() == startValue) {
            jQuery(this).val('');
        }
    });

    jQuery('input.SearchField').blur(function() {
        if (jQuery('input.SearchField').val() == '') {
            jQuery('input.SearchField').val(startValue);
        }
    });

    jQuery('input.calendar').each(function() {
        var icon = jQuery('<img/>').attr({
            'src': '/images/icon-calendar.png',
            'class': 'calendarIcon'
        }).css({
            'margin-left': '10px',
            'cursor': 'pointer'
        });
        jQuery(this).parent().after(icon);
        var that = jQuery(this);
        jQuery(this).DatePicker({
            format: 'd. m Y',
            date: that.val(),
            current: that.val(),
            onBeforeShow: function() {
                that.DatePickerSetDate(that.val(), true);
            },
            onChange: function(formated, dates) {
                that.val(formated);
                that.DatePickerHide();
            }
        });
    });
    jQuery('.calendarIcon').each(function() {
        var that = jQuery(this);
        jQuery(this).DatePicker({
            format: 'd. m Y',
            date: that.prev().children().val(),
            current: that.prev().children().val(),
            onBeforeShow: function() {
                that.DatePickerSetDate(that.prev().children().val(), true);
            },
            onChange: function(formated, dates) {
                that.prev().children().val(formated);
                that.DatePickerHide();
            }
        });
    });

    if (jQuery('ul.tabs').length) {
        if (jQuery('.tabContent').length) {
            new TabSwitcher(0);
        }
    }

    if (jQuery('.galleryItem').length) {
        jQuery(".galleryItem a").lightBox();
    }

    if (jQuery('.mediaLiner').length) {
        jQuery('.mediaLiner a[rel="video"]').videoBox();
        if (jQuery('.mediaLiner a[rel="image"]').length)
            jQuery('.mediaLiner a[rel="image"]').imageBox();
        jQuery('.mediaLiner div.audio a').videoBox({ audio: 1 });
    }


    //if (jQuery('.galleryLiner').length) {
    //    if (jQuery('a[rel="image"]').length)
    //        jQuery('a[rel="image"]').imageBox();
    //}

    jQuery('#MyGolf').each(function() {
        var that = jQuery(this);
        if (that.hasClass('login')) {
            jQuery(this).hover(function() {
                jQuery("#MyGolfLoginPopup").show();
                jQuery("#MyGolfLoginPopup input").eq(0).focus();
            }, function() {
                jQuery("#MyGolfLoginPopup").hide();
            });
            //jQuery(this).children('a').eq(0).click(function(event) {
            //    event.preventDefault();
            //});
        }
    });

    jQuery('a.addFriend').each(function() {
        jQuery(this).click(function(event) {
            return confirm('Do you want to add this person as a friend?');
        });
    });

});

function SlideNews(containerID, panelClass, leftArrow, rightArrow, displayNumber, hasPages) {
		
    this.init = function(){
        var that = this;
        this.containerID = containerID;
        this.container = jQuery("#"+containerID);
        this.leftArrow = jQuery("#"+leftArrow);
        this.rightArrow = jQuery("#"+rightArrow);
        this.displayNumber = displayNumber;
        this.panels = this.container.find("." + panelClass);
        this.panelsLength = this.panels.length;
        this.width = 0;
        this.hasPages = hasPages;
        this.panels.each(function(index){
            that.width += parseInt(jQuery(this).outerWidth());
            if (parseInt(jQuery(this).css("margin-right")) != 0 && jQuery(this).css("margin-right") != 'auto'){
                that.width += parseInt(jQuery(this).css("margin-right"));
            }
            if (parseInt(jQuery(this).css("margin-left")) != 0 && jQuery(this).css("margin-left") != 'auto'){
                that.width += parseInt(jQuery(this).css("margin-left"));
            }
            //console.log(that.width);
        });
        if (this.hasPages) {
            this.singleWidth = (this.width / this.panelsLength) * this.displayNumber;
            this.maxPosition = this.panelsLength / this.displayNumber-1;
        } else {
            this.singleWidth = this.width / this.panelsLength;
            this.maxPosition = this.panelsLength - this.displayNumber;
        }

        this.position = 0;
        this.numOfPages = this.panelsLength/this.displayNumber;
        this.setWidth();
        this.updateArrows();
        if(this.hasPages){
            this.updatePages();
        }
        this.setEventHandlers();
    }
	
    this.animateIt = function(left) {
        this.container.animate(
        {
            left: left
        },
        {
            queue: false,
            duration: 350
        }
        );
    }
	
    this.rightAnimate = function(setPosition) {
        if (this.position < this.maxPosition) {
            this.position++;
            this.updateArrows();
            if(setPosition || setPosition == 0)
                pos = setPosition;
            else
                pos = this.position;
			
            this.animateIt(pos * (-this.singleWidth));				
        }
    }
	
    this.leftAnimate = function(setPosition) {
        if (this.position > 0) {
            this.position--;
            this.updateArrows();
            var pos;
            if(setPosition || setPosition == 0)
                pos = setPosition;
            else
                pos = this.position;
			
            this.animateIt(pos * (-this.singleWidth));
        }
    }
	
    this.setWidth = function(){
        //if (this.container.css("width") != 'auto'){
        this.container.css("width", this.width + "px");
    //}
    }

    this.updateArrows = function() {
        (this.position > 0) ? this.arrowShow(this.leftArrow) : this.arrowHide(this.leftArrow);
        (this.position >= this.maxPosition) ? this.arrowHide(this.rightArrow) : this.arrowShow(this.rightArrow);
    }

    this.arrowShow = function(arrow) {
        arrow.children("img").fadeIn();
    }
	
    this.arrowHide = function(arrow) {
        arrow.children("img").fadeOut();
    }
	
    this.updatePages = function(){
        var that = this;
        for(i=0;i<this.numOfPages;i++){
            var page = jQuery('<span></span>');
            page.html("&nbsp;");
            if(i==0) page.attr('class','set');
            jQuery("#"+that.containerID+"-pages").append(page);
        }
    }
	
    this.setEventHandlers = function() {
        var that = this;
        this.rightArrow.click(function(event){
            event.preventDefault();
            that.rightAnimate();
            if(that.hasPages){
                that.setSelectedPage(that.position);
            }
        });
        this.leftArrow.click(function(event){
            event.preventDefault();
            that.leftAnimate();
            if(that.hasPages){
                that.setSelectedPage(that.position);
            }
        });
		
        if(this.hasPages){
            jQuery("#"+that.containerID+"-pages").children("span").each(function(i){
                jQuery(this).click(function(event){
                    event.preventDefault();
                    that.setSelectedPage(i);
                    that.animateIt(i*(-that.singleWidth));
                    that.position = i;
                    that.updateArrows();
                });
            });
        }
    }
	
    this.setSelectedPage = function(index){
        var that = this;
        var pages = new Array();
        jQuery("#"+that.containerID+"-pages").children("span").each(function(i){
            pages[i] = jQuery(this);
        });
        jQuery(pages).each(function(i){
            var count = pages.length;
            if(i==index){
                pages[index].attr('class', 'set');
            }else{
                pages[i].attr('class', '');
            }
        });
    }

    this.init();
	
}

function TabSwitcher(openIndex){

    this.init = function(){
        var self = this;
        this.tabButtons = new Array();
        this.tabPanes = new Array();
        this.tabs = jQuery('ul.tabs').children('li').children('a');
        this.panes = jQuery('.tabContent');
	this.openIndex = openIndex;
        this.tabs.each(function(i){
            self.tabButtons[i] = jQuery(this);
        });
        this.panes.each(function(i){
            self.tabPanes[i] = jQuery(this);
        });
		
        if(this.openIndex != ''){
            this.tabPanes[this.openIndex].show();
            jQuery(self.tabButtons).each(function(i){
                if(i==self.openIndex) 
                    self.tabButtons[self.openIndex].parent().addClass('selected');
                else
                    self.tabButtons[i].parent().removeClass('selected');
            });
        }else{
            this.tabPanes[0].show();
        }
        
        this.switchTabs();
        
        jQuery('div.club ul li a.arrow').each(function(i){
            jQuery(this).click(function(e){
               e.preventDefault();
               if (jQuery(this).parent().next().hasClass('hide')) 
                    jQuery(this).parent().next().slideDown(500, function(){
                        jQuery(this).removeClass('hide');
                    });
                else
                    jQuery(this).parent().next().slideUp(500, function(){
                        jQuery(this).addClass('hide');
                    });
            });
        });
        
        jQuery('#comp_events tr.item td a.infoLink').each(function(i){
            jQuery(this).click(function(e){
               e.preventDefault();
               if (jQuery(this).parent().parent().next().hasClass('hide')) 
                    //jQuery(this).parent().parent().next().slideDown(500, function(){
                        jQuery(this).parent().parent().next().removeClass('hide');
                    //});
                else
                    //jQuery(this).parent().parent().next().slideUp(500, function(){
                        jQuery(this).parent().parent().next().addClass('hide');
                    //});
            });
        });
        $('a.closeSummary').click(function(){
            $(this).parent().parent().parent().addClass('hide');
        });
        
    }
	
    this.switchTabs = function(){
        var self = this;
        this.tabs.each(function(i){
            jQuery(this).click(function(e){
                if(jQuery(this).attr('href')=='#')
                    e.preventDefault();
                for(j=0;j<self.tabPanes.length;j++){
                    if (j != i) {
                        if(self.tabButtons[j].parent().hasClass('selected'))
                            self.tabButtons[j].parent().removeClass('selected')
                        self.tabPanes[j].fadeOut(500);
                    }else {
                        self.tabButtons[i].parent().addClass('selected');
                        self.tabPanes[i].fadeIn(500);
                    }
                }
            });
        });
    }
	
    this.init();
}

function array_sum(array) {
    var key, sum = 0;

    if (typeof array !== 'object') {
        return null;
    }
    for (key in array) {
        sum += (array[key]*1);
    }
    if(/[0-9]/.test(sum))
        return sum;
    else
        return '*';
}

function inArray(array,value) {
    var i;
    for (i=0; i < array.length; i++) {
        if (array[i] === value) {
            return true;
        }
    }
    return false;
};


$.tablesorter.addParser({
    id: 'handicap',
    is: function(s) {
        return false;
    },
    format: function(s) {
        return s.replace('+', '-').replace('Pending', '99');
    },
    // set type, either numeric or text 
    type: 'numeric'
});
$.tablesorter.addParser({
    id: 'topar',
    is: function(s) {
        return false;
    },
    format: function(s) {
        if (s == '-') s = 'bottom';
        if (s == 'DQ') s = 'bottom';
        return s.replace('Ev', '0');
    },
    // set type, either numeric or text 
    type: 'numeric'
});

$.tablesorter.addParser({
    id: 'updown',
    is: function(s) {
        return false;
    },
    format: function(s) {
        return s.replace('<[^>]+>', '');
    },
    // set type, either numeric or text 
    type: 'numeric'
});

$(document).ready(function() {
    $('table.tablesorter').tablesorter();
    //$("table.tablesorter").tablesorter({ headers: { 2: { sorter: 'handicap'}} });
});
