var DOMUtilities = {
	targetBlank: function(locality){
		// XHTML 1.0 Strict work around for external links
		$(locality+' a[rel*="external"]').attr("target","_blank");
	},
	inputAutoClear: function(locality){
		$(locality+' input.clearField').focus(function(){
			if(this.defaultValue == this.value) this.value='';
		}).blur(function(){
			if(this.value == '') this.value = this.defaultValue;
		});
	},
	imgRollover: function(locality){
		// Image roll-over setup
		$(locality+' img.rollOver, '+locality+' input[type="image"].rollOver')
			.mouseover(function(){
				if (this.src.indexOf("_i.") != -1) {
					this.src = this.src.replace("_i.", "_o.");
				}
			}).mouseout(function(){
				if (this.src.indexOf("_o.") != -1) {
					this.src = this.src.replace("_o.", "_i.");
				}
				if(this.src.indexOf("_a.")) {
					this.src = this.src.replace("_a.","_i.");
				}
			}).filter("input").mousedown(function(){
				this.src = this.src.replace("_o.","_a.");
			}).mouseup(function(){
				this.src = this.src.replace("_a.","_i.");
			});
	},
	init: function(locality){
		if(locality == null) {
			locality = "body";
		}
		this.targetBlank(locality);
		this.inputAutoClear(locality);
		this.imgRollover(locality);
	}
}

// Speaker Action script for handling goto's and prev and next
var speakerAction = {
    speakerSlideCount: 0,
    prevSlide: function(theIndex){
        speakerAction.goToSlide(theIndex - 1);
    },
    nextSlide: function(theIndex){
        speakerAction.goToSlide(theIndex + 1);
    },
    goToSlide: function(theIndex){
        var speakerName = $('.speakerDeck dt:eq('+ ( theIndex + 1 ) +')').html();
        //change speaker name on next link        
        $('#next_speaker a[href=#next]').html(speakerName);
        
        $('#speaker_nav li.sNav a').removeClass('active');
        $('#speaker_nav li.sNav a:eq('+ theIndex +')').addClass('active');
        speakerDeck.goTo(theIndex+1);
        if((theIndex + 1) == speakerAction.speakerSlideCount){
            // disable the next button
            $('#next_speaker a[href=#next]').addClass('disabled');
            $('#prev_speaker a[href=#prev]').removeClass('disabled');
        }else if((theIndex + 1) == 1){
            // disable the previous button
            $('#prev_speaker a[href=#prev]').addClass('disabled');
            $('#next_speaker a[href=#next]').addClass('disabled');
        }else{
            // enable both next/previous buttons
            $('#next_speaker a[href=#next],#prev_speaker a[href=#prev]').removeClass('disabled');
        }
    },
    init: function(){
        if($('#speaker_nav').length){
            speakerAction.speakerSlideCount = $('.speakerDeck dd').length;
            $('#speaker_nav li.sNav a').click(function(e){
                e.preventDefault();
                var theIndex = $('#speaker_nav li.sNav a').index($(this));
                speakerAction.goToSlide(theIndex);
                return false;
            });
            $('.speakerPreviewList li.speakerPreview a').click(function(e){
                e.preventDefault();
                var theIndex = $('.speakerPreviewList li.speakerPreview a').index($(this));
                speakerAction.goToSlide(theIndex + 1);
                return false;
            });
            $('#prev_speaker a[href=#prev]').click(function(e){
                e.preventDefault();
                if($(this).hasClass('disabled')){
                    return false;
                }else{
                    var theIndex = $('#speaker_nav li.sNav a').index($('#speaker_nav li.sNav a.active'));
                    speakerAction.prevSlide(theIndex);
                }                    
                return false;
            });
            $('#next_speaker a[href=#next]').click(function(e){
                e.preventDefault();
                if($(this).hasClass('disabled')){
                    return false;
                }
                else{
                    var theIndex = $('#speaker_nav li.sNav a').index($('#speaker_nav li.sNav a.active'));
                    speakerAction.nextSlide(theIndex);
                }
                return false;
            });                
        } 
 
    }
};




// connect Action script for handling goto's and prev and next
var connectAction = {
    connectSlideCount: 0,
    prevSlide: function(theIndex){
        connectAction.goToSlide(theIndex - 1);
    },
    nextSlide: function(theIndex){
        connectAction.goToSlide(theIndex + 1);
    },
    goToSlide: function(theIndex){
        var connectName = $('.connectDeck dt:eq('+ ( theIndex + 1 ) +')').html();
        //change speaker name on next link        
        $('#next_connect a[href=#next]').html(connectName);
        
        $('#connect_nav li.sNav a').removeClass('active');
        $('#connect_nav li.sNav a:eq('+ theIndex +')').addClass('active');
        connectDeck.goTo(theIndex+1);
        if((theIndex + 1) == connectAction.connectSlideCount){
            // disable the next button
            $('#next_connect a[href=#next]').addClass('disabled');
            $('#prev_connect a[href=#prev]').removeClass('disabled');
        }else if((theIndex + 1) == 1){
            // disable the previous button
            $('#prev_connect a[href=#prev]').addClass('disabled');
            $('#next_connect a[href=#next]').addClass('disabled');
        }else{
            // enable both next/previous buttons
            $('#next_connect a[href=#next],#prev_connect a[href=#prev]').removeClass('disabled');
        }
    },
    init: function(){
        if($('#connect_nav').length){
            connectAction.connectSlideCount = $('.connectDeck dd').length;
            $('#connect_nav li.sNav a').click(function(e){
                e.preventDefault();
                var theIndex = $('#connect_nav li.sNav a').index($(this));
                connectAction.goToSlide(theIndex);
                return false;
            });
            $('.connectPreviewList li.connectPreview a').click(function(e){
                e.preventDefault();
                var theIndex = $('.connectPreviewList li.connectPreview a').index($(this));
                connectAction.goToSlide(theIndex + 1);
                return false;
            });
            $('#prev_connect a[href=#prev]').click(function(e){
                e.preventDefault();
                if($(this).hasClass('disabled')){
                    return false;
                }else{
                    var theIndex = $('#connect_nav li.sNav a').index($('#connect_nav li.sNav a.active'));
                    connectAction.prevSlide(theIndex);
                }                    
                return false;
            });
            $('#next_connect a[href=#next]').click(function(e){
                e.preventDefault();
                if($(this).hasClass('disabled')){
                    return false;
                }
                else{
                    var theIndex = $('#connect_nav li.sNav a').index($('#connect_nav li.sNav a.active'));
                    connectAction.nextSlide(theIndex);
                }
                return false;
            });                
        } 
 
    }
};


// collections Action script for handling goto's and prev and next
var collectionsAction = {
    collectionsSlideCount: 0,
    prevSlide: function(theIndex){
        collectionsAction.goToSlide(theIndex - 1);
    },
    nextSlide: function(theIndex){
        collectionsAction.goToSlide(theIndex + 1);
    },
    goToSlide: function(theIndex){
        var collectionsName = $('.collectionsDeck dt:eq('+ ( theIndex + 1 ) +')').html();
        //change speaker name on next link        
        $('#next_collections a[href=#next]').html(collectionsName);
        
        $('#collections_nav li.sNav a').removeClass('active');
        $('#collections_nav li.sNav a:eq('+ theIndex +')').addClass('active');
        collectionsDeck.goTo(theIndex+1);
        if((theIndex + 1) == collectionsAction.collectionsSlideCount){
            // disable the next button
            $('#next_collections a[href=#next]').addClass('disabled');
            $('#prev_collections a[href=#prev]').removeClass('disabled');
        }else if((theIndex + 1) == 1){
            // disable the previous button
            $('#prev_collections a[href=#prev]').addClass('disabled');
            $('#next_collections a[href=#next]').addClass('disabled');
        }else{
            // enable both next/previous buttons
            $('#next_collections a[href=#next],#prev_collections a[href=#prev]').removeClass('disabled');
        }
    },
    init: function(){
        if($('#collections_nav').length){
            collectionsAction.collectionsSlideCount = $('.collectionsDeck dd ').length;
            $('#collections_nav li.sNav a').click(function(e){
                e.preventDefault();
                var theIndex = $('#collections_nav li.sNav a').index($(this));
                collectionsAction.goToSlide(theIndex);
                return false;
            });
            $('.collectionsPreviewList li.collectionsPreview a').click(function(e){
                e.preventDefault();
                var theIndex = $('.collectionsPreviewList li.collectionsPreview a').index($(this));
                collectionsAction.goToSlide(theIndex + 1);
                return false;
            });
            $('#prev_collections a[href=#prev]').click(function(e){
                e.preventDefault();
                if($(this).hasClass('disabled')){
                    return false;
                }else{
                    var theIndex = $('#collections_nav li.sNav a').index($('#collections_nav li.sNav a.active'));
                    collectionsAction.prevSlide(theIndex);
                }                    
                return false;
            });
            $('#next_collections a[href=#next]').click(function(e){
                e.preventDefault();
                if($(this).hasClass('disabled')){
                    return false;
                }
                else{
                    var theIndex = $('#collections_nav li.sNav a').index($('#collections_nav li.sNav a.active'));
                    collectionsAction.nextSlide(theIndex);
                }
                return false;
            });                
        } 
 
    }
};


// other Action script for handling goto's and prev and next
var otherAction = {
    otherSlideCount: 0,
    prevSlide: function(theIndex){
        otherAction.goToSlide(theIndex - 1);
    },
    nextSlide: function(theIndex){
        otherAction.goToSlide(theIndex + 1);
    },
    goToSlide: function(theIndex){
        var otherName = $('.otherDeck dt:eq('+ ( theIndex + 1 ) +')').html();
        //change speaker name on next link        
        $('#next_other a[href=#next]').html(collectionsName);
        
        $('#other_nav li.sNav a').removeClass('active');
        $('#other_nav li.sNav a:eq('+ theIndex +')').addClass('active');
        otherDeck.goTo(theIndex+1);
        if((theIndex + 1) == otherAction.otherSlideCount){
            // disable the next button
            $('#next_other a[href=#next]').addClass('disabled');
            $('#prev_other a[href=#prev]').removeClass('disabled');
        }else if((theIndex + 1) == 1){
            // disable the previous button
            $('#prev_other a[href=#prev]').addClass('disabled');
            $('#next_other a[href=#next]').addClass('disabled');
        }else{
            // enable both next/previous buttons
            $('#next_other a[href=#next],#prev_other a[href=#prev]').removeClass('disabled');
        }
    },
    init: function(){
        if($('#other_nav').length){
            otherAction.otherSlideCount = $('.otherDeck dd ').length;
            $('#other_nav li.sNav a').click(function(e){
                e.preventDefault();
                var theIndex = $('#other_nav li.sNav a').index($(this));
                otherAction.goToSlide(theIndex);
                return false;
            });
            $('.otherPreviewList li.otherPreview a').click(function(e){
                e.preventDefault();
                var theIndex = $('.otherPreviewList li.otherPreview a').index($(this));
                otherAction.goToSlide(theIndex + 1);
                return false;
            });
            $('#prev_other a[href=#prev]').click(function(e){
                e.preventDefault();
                if($(this).hasClass('disabled')){
                    return false;
                }else{
                    var theIndex = $('#other_nav li.sNav a').index($('#other_nav li.sNav a.active'));
                    otherAction.prevSlide(theIndex);
                }                    
                return false;
            });
            $('#next_other a[href=#next]').click(function(e){
                e.preventDefault();
                if($(this).hasClass('disabled')){
                    return false;
                }
                else{
                    var theIndex = $('#other_nav li.sNav a').index($('#other_nav li.sNav a.active'));
                    otherAction.nextSlide(theIndex);
                }
                return false;
            });                
        } 
 
    }
};



$(document).ready(function(){
    window.validateForms = {};
    DOMUtilities.init();
	FancyBox.init();
    startForms.init();
    Speakers.init();
    connect.init ();
    collections.init ();
    other.init ();
});
