$(document).ready(function(){
	
	Site.init();
	
	
	Shadowbox.init({
	    overlayOpacity: 0.8,
	   	players:	["html"],
	   	flv: ['flv','mp4']
	}, setupShadowbox);
	
	
	// Init the map if the maps tab is visible
	if($('.map-tab').is(':visible')) {
		initMap();
	}
	
	// Init the map if we click on the tab to display it
	$("#show-map").click(function() {
		if($('.map-tab').is(':visible')) {
			initMap();
		}    
    });

});


function setupShadowbox() {

    Shadowbox.setup("a.gallery-one", {
        gallery:        "Gallery",
        continuous:     true,
        counterType:    "skip"
    });
    
    Shadowbox.setup('a[title="vimeo-video-1"]', {
        height:     360,
        width:      640,
        flashVars:  {
            clip_id:    "1893986",
            autoplay:   "1"
        }
    });
}


var Site = {
	
	init:function() {
		
		//HANDLE EXTERNAL LINKS
		Site.externalLinks();
			
		//TEXT RESIZZLE
		if($('#usability').length != 0) {
			$('#primary').textResizzle();	
		}
		
		//PRINT
		if($('#usability-controls').length != 0) {
			Site.print();
		}
		
		//TABS
		if($('#tabs').length != 0) {
			$('#tabs').tabs();
		}
		
		//FORM VALIDATION
		if($('.val-form').length != 0){
			$('#second-level-get-invited').validate();
		}
		
		//TERTIARY QUOTES
		if($('#tertiary-quotes').length != 0){
			Site.tertiaryQuotes();
		}
		
		//SECOND LEVEL GET INVITED INLINE FORM LABELS
		if($('#second-level-get-invited').length != 0) {
			Site.getInvited();
		}
		
		//HOMEPAGE GET INVITED INLINE FORM LABELS
		if($('#home-get-invited').length != 0) {
			Site.homeGetInvited();
			
			$('#home-get-invited').validate();
		}
		
		
		//HOME SPONSORS
		if($('#sponsors-inner').length != 0){
			Site.homeSponsors();
		}
		if($('#home-quotes').length != 0){
			Site.homeQuotes();
		}
		if($('#home-feature').length != 0){
			Site.homeFeature();
		}
	},
	
	print:function() {
		
		$('#print').click(function(){
			window.print();
		});
		
	},
	
	externalLinks:function() {
	
		var extLinks = $('a.external');
			if ( extLinks.length ) {
				extLinks.each(function() { 
					$(this).attr('target', '_blank');
			});
		}
	
	},
	
	combobox:function() {
		$(".combobox" ).combobox();
	},
	
	getInvited:function() {
		
		$('#second-level-get-invited input').focus(function() {
			$(this).css({'background-position':'-280px -28px'});			
			$(this).prev('label').css({'left':'-9999px'});
		});
		
		$('#second-level-get-invited input').blur(function() {
			$(this).css({'background-position':'-280px 0'});
			if ($(this).val() == '')
				$(this).prev('label').css({'left':'5px'});
		});
		
	},
	
	homeGetInvited:function() {
		
		$('#home-get-invited input').focus(function() {
			$(this).css({'background-position':'-280px -28px'});			
			$(this).prev('label').css({'left':'-9999px'});
		});
		
		$('#home-get-invited input').blur(function() {
			$(this).css({'background-position':'-280px 0'});
			if ($(this).val() == '')
				$(this).prev('label').css({'left':'5px'});
		});
		
	},
		
	cycleFeature:function() {
	
		$('ul#home-events').cycle({
				fx: 'fade', 
				width: '100%',
				fit: true,
				pager: '#feature-nav', 
				timeout: 2000,
				pause:   1
			});
		
		$('ul#home-events').width('100%');

		
		$(window).resize(function() {
					
			var height = $('ul#home-events > li').height();
						
			$('ul#home-events').height(height);
			
		});
		
		
	},
	
	homeSponsors:function() {
		
		$('#sponsor-list-inner').carouFredSel({
			scroll: {
				items: 1
			},
			items: {
				visible: 4
			},
			prev: '#prev',
			next: '#next',
			auto: true
		});

	
	},
	
	homeFeature:function() {
		
		$('#home-event-overview > ul').cycle({
			fx: 'fade', 
			timeout: 50000,
			pause:   1,
			pager:  '#feature-pagination',
        	pagerAnchorBuilder: function(idx, slide) {
            	// return sel string for existing anchor
            	return '#feature-pagination li:eq(' + (idx) + ')';
       		}

		}).cycle('pause');
		
		//HANDLE :NTH CHILD ON PROFILE LISTING
		$('.speakers li:nth-child(4n+4)').addClass('fourth-child');
		
	},
	
	homeQuotes:function() {
	
		$('#home-quote-cycle').cycle({
			fx: 'fade', 
			pager: '#quote-pagination',
			timeout: 5000,
			pause:   1,
			cleartypeNoBg: true,
			cleartype: true
		});
	},

	tertiaryQuotes:function() {
		
		$('#tertiary-quotes ul').cycle({
				fx: 'fade', 
				fit: true,
				pager: '#quote-pagination', 
				timeout: 5000,
				pause:   1
			});
	
	}
};
