jQuery(window).load(function() {
    jQuery('a.delete').click(function(ev) {
        var msg = 'Are you sure you want to delete this?';
        if (jQuery(this).attr('message') != undefined)
            msg = jQuery(this).attr('message');
        // generic delete confirmation is here because this function would override the return of any confirmation placed in events elsewhere
        if (confirm(msg) == true) {
            //ev.preventDefault();
            jQuery(this).parent().parent().fadeOut(1000, function() {
                //Do some server side stuff to delete record
                jQuery(this).remove();
            });
            return true;
        }
        return false;
    });
    if (jQuery("ul.clubs").length) new ClubSwitcher(0);
    new GalleryViewer();

//    jQuery('#ShowAddComment').click(function(e) {
//        jQuery('#AddCommentBox').toggle();
//        e.preventDefault();
//    });
});

function ClubSwitcher(openIndex){

    this.init = function(){
        var self = this;
        this.tabButtons = new Array();
        this.tabPanes = new Array();
        this.tabs = jQuery('ul.clubs').children('li').children('a');
        this.panes = jQuery('.club');
	this.openIndex = openIndex;
        this.tabs.each(function(i){
            self.tabButtons[i] = jQuery(this);
        });
        this.panes.each(function(i){
            self.tabPanes[i] = jQuery(this);
            self.tabPanes[i].hide();
        });

        if(this.openIndex != ''){
            this.tabPanes[this.openIndex].show();
            jQuery(self.tabButtons).each(function(i){
                if(i==self.openIndex)
                    self.tabButtons[self.openIndex].addClass('current');
                else
                    self.tabButtons[i].removeClass('current');
            });
        }else{
            this.tabPanes[0].show();
        }

        this.switchTabs();
    }

    this.switchTabs = function(){
        var self = this;
        this.tabs.each(function(i){
            jQuery(this).click(function(e){
                e.preventDefault();
                for(j=0;j<self.tabPanes.length;j++){
                    if (j != i) {
                        if(self.tabButtons[j].hasClass('current'))
                            self.tabButtons[j].removeClass('current')
                        self.tabPanes[j].fadeOut(500);
                    }else {
                        self.tabButtons[i].addClass('current');
                        self.tabPanes[i].fadeIn(500);
                    }
                }
            });
        });
    }

    this.init();
}

function BenefitSwitcher(openIndex) {

    this.init = function() {
        var self = this;
        this.tabButtons = new Array();
        this.tabPanes = new Array();
        this.tabs = jQuery('ul.benefits').children('li').children('a');
        this.panes = jQuery('.benefit');
        this.openIndex = openIndex;
        this.tabs.each(function(i) {
            self.tabButtons[i] = jQuery(this);
        });
        this.panes.each(function(i) {
            self.tabPanes[i] = jQuery(this);
            self.tabPanes[i].hide();
        });

        if (this.openIndex != '') {
            this.tabPanes[this.openIndex].show();
            jQuery(self.tabButtons).each(function(i) {
                if (i == self.openIndex)
                    self.tabButtons[self.openIndex].addClass('current');
                else
                    self.tabButtons[i].removeClass('current');
            });
        } else {
            this.tabPanes[0].show();
        }

        this.switchTabs();
    }

    this.switchTabs = function() {
        var self = this;
        this.tabs.each(function(i) {
            jQuery(this).click(function(e) {
                e.preventDefault();
                for (j = 0; j < self.tabPanes.length; j++) {
                    if (j != i) {
                        if (self.tabButtons[j].hasClass('current'))
                            self.tabButtons[j].removeClass('current')
                        self.tabPanes[j].fadeOut(500);
                    } else {
                        self.tabButtons[i].addClass('current');
                        self.tabPanes[i].fadeIn(500);
                    }
                }
            });
        });
    }

    this.init();
}

function GalleryViewer(){

    this.init = function(){
        var self = this;
        this.thumbs = new Array();
        this.thumbLinks = jQuery('div.thumbHolder').children('a');
        this.imagesViewer = jQuery('div.imgHolder');
        this.thumbLinks.each(function(i){
            self.thumbs[i] = jQuery(this);
        });

        this.getImage();
    }

    this.getImage = function(){
        var self = this;
        this.thumbLinks.each(function(i){
            jQuery(this).click(function(e){
                e.preventDefault();
                var url = jQuery(this).attr('href');
                var alt = jQuery(this).attr('title');
                self.imagesViewer.addClass('loading');
                if(self.imagesViewer.children().length){
                    self.imagesViewer.children().each(function(){
                        jQuery(this).fadeOut(1000);
                    });
                }
                jQuery.get(url, function(){
                     var img = jQuery('<img/>').attr({'src':url,'alt':alt}).css('opacity',0);
                     self.imagesViewer.append(img);
                     img.animate({'opacity':1},1000,function(){
                         self.imagesViewer.removeClass('loading');
                     });
                });
            });
        });
    }

    this.init();
}


