var ie7set = false;
jQuery(function( $ ){
	$('#slider').slider({
		openWidth: '420px'              
	});         

	//Scrolling content

	$('#content_pane').attr({scrollTop:0,scrollLeft:0});
	
	$('#mainnav .first A').addClass('active');


	$.localScroll.hash({
		target: '#content_pane', //could be a selector or a jQuery object too.
		axis:'xy',//the default is 'y'
		queue:true,
		duration:1500
	});
	
	var $last = $([]);//save the last link
	
	
	
/* 	var scrollVariables = null; */
	
	$.localScroll( {
		target: '#content_pane', //could be a selector or a jQuery object too.
		axis:'xy', //the default is 'y'
		queue:true,
		lazy:true,
		duration:2000,
		onBefore:function(e,elem,c){
/* 			console.log(c); */
			var href = $(this).attr('href');
			var hashPosition = href.search('#')+1;
			var pageName = href.substring(hashPosition);
			
			if($(this).parents('#landing_content').length > 0) {
				if(!ie7set) {
					adjustNav(href);
				}
			}
			$('#play_latest_link').fadeOut();
			$('#menu, #footer_wrapper, #way_bottom_shadow, #bottom_footer, #campus').fadeIn(3000);
			
			level = $('#'+pageName).attr('class').substring(10);
			if(($('#'+pageName+'_subpages').length == 0)&&(level < 5)){
				$('#loading').fadeIn('medium');
				$.get(pageName+'/ajax/'+level,function(data){
					$('#'+pageName).append(data);
					$('#loading').fadeOut('medium');
					loadGreyBox();
					loadDepartment();
					//makeClickable(pageName);
				});
			} else {
				$('#loading').fadeOut('medium');
			}
		},
		onAfter:function(){
			
		},
		hash:false
	});


	function adjustNav(href) {
		$('#campus').css('background','url(../img/campus_name_sprite.png) top center no-repeat');
		var location = "";
		switch(href) {
			case "#amarillo-home":
				location = "#amarillo-";
				$('#campus').css('background-position','center -120px');
				$('#mainnav-amarillo').show();
				$('#footer_menu-amarillo').show();
				break;
			case "#canyon-home":
				location = "#canyon-";
				$('#campus').css('background-position','center -80px');
				$('#CalendarCategoryId').hide();
				$('#mainnav-canyon').show();
				$('#footer_menu-canyon').show();
				break;
			case "#dalhart-home":
				location = "#dalhart-";
				$('#campus').css('background-position','center -40px');
				$('#mainnav-dalhart').show();
				$('#footer_menu-dalhart').show();
				break;
			case "#kingston-home":
				location = "#kingston-";
				$('#mainnav-kingston').show();
				$('#footer_menu-kingston').show();
				break;
			default:
				location = "#unknown-";
				break;
		}
		
		/*
$('#home_nav a').attr('href', href);
		$('#about_nav a').attr('href',location+'about');
		$('#ministries_nav a').attr('href',location+'ministries');
		$('#media_nav a').attr('href',location+'media');
		$('#contact_nav a').attr('href',location+'contact');
		$('#firsttimeguest_btn a').attr('href',location+'first-time-guests');
		$('#getinvolved_btn a').attr('href',location+'get-involved-landing');
		$('#onlinegiving_btn a').attr('href',location+'online-giving');
*/

		
		if($.browser.msie && $.browser.version == "7.0"){
			/*
ie7set = true;
			$('#mainnav').localScroll(scrollVariables);
*/
		} else {

		
		}
		
	}


	function loadGreyBox() {
		$('a[rel*=greybox]').click(function(){
			$('.media_roller').hide();
			$.get($(this).attr("href"), function(data){
		  		$('#greybox_content').html(data);
		  		$('#greybox').fadeIn();
		  		calendarLink();
			});
			return false;
		});
		
		$('a[rel*=greylocal]').click(function(){
			$('.media_roller').hide();
			var href = $(this).attr('href');
			var hashPosition = href.search('#');
			var pageName = href.substring(hashPosition);
	  		$('#greybox_content').html($(pageName).html());
	  		$('#greybox').fadeIn();
/* 	  		loadStaff(); */
/* 	  		loadDepartment(); */
			return false;
		});
		
		/*
$('.podcast_embed').click(function(){
			$('.media_roller').hide();
			$('#mini_greybox_content').html('<embed src="'+$(this).attr("href")+'" type="application/x-shockwave-flash" width="960" height="558" allowscriptaccess="always" allowfullscreen="true"></embed>');
	  		$('#mini_greybox').fadeIn();
			return false;
		});
*/
		
		$('.podcast_embed, #play_latest_link a, #series_btn a').click(function(){
			$('.media_roller').hide();
			$.get($(this).attr('href'),function(data) {
				$('#mini_greybox_content').html("");
				$('#mini_greybox_content').flash({
					src: "swf/player.swf",
					width: "720px",
					height: "435px",
					wmode: "transparent",
					allowfullscreen: "true",
					allowscriptaccess: "always",
					allownetworking: "all",
					quality: "high",
					flashvars: {
						skin: "swf/dangdang.swf",
						file: data,
						bufferlength: "7",
						autostart: "true",
						type:"rtmp",
						"rtmp.loadbalance":"true"
					}
				});				
			});
			
			
			if($(this).hasClass("podcast_embed")){
				$('#mini_greybox_caption').hide();
			} else {
				$('#mini_greybox_caption').show();
			}
			
	  		$('#mini_greybox').fadeIn();
			return false;
		});
		
		$('a[rel*=greyiframe]').click(function(){
			$('.media_roller').hide();
			$('#greybox_content').html('<iframe src="'+$(this).attr("href")+'" width="935" height="540" scrolling="no" frameborder="0"></iframe>');
	  		$('#greybox').fadeIn();
			return false;
		});
		
		$('#greybox_close a').click(function(){
			$('#greybox_content').html("");
			$('#greybox').fadeOut();
			$('.media_roller').show();
		});
		
		$('#mini_greybox_close a').click(function(){
			$('#mini_greybox_content').html("");
			$('#mini_greybox').fadeOut();
			$('.media_roller').show();
		});
	}
	
	loadGreyBox();
	
	/*
$('#play_latest_link a, #series_btn a').click(function(){
		var player = '<embed src="http://blip.tv/play/hJw_Apq7Rg" type="application/x-shockwave-flash" width="725" height="435" allowscriptaccess="always" allowfullscreen="true"></embed>';
		$('.media_roller').hide();
  		$('#mini_greybox_content').html(player);
  		$('#mini_greybox').fadeIn();
		return false;
	});
*/
	
	//CALENDAR
	function calendarLink() {
		$('.calendar_nav_link').click(function(){
			calendarGet($(this).attr('href'));
			return false;
		});
	
		//$('.calendar_event a').calendarTooltip({speed: 150, delay: 300});
		$('.calendar_event a').click(function(){
			$('#calendar_details').fadeOut();
			//$('#calendar_details_content').html($('#loading').html());
			//$('#calendar_details').fadeIn();
			$.get($(this).attr('href'), function(data){
				$('#calendar_details_content').html(data);
				$('#calendar_details').fadeIn();
			});
			return false;
		});
		
		/*
$('.calendar_has_events').click(function(){
			$('#calendar_details_content').html($(this).html());
			$('#calendar_details').fadeIn();
			calendarLink();
			$('#calendar_details_content .calendar_date').append(' click event below for details');
			return false;
		});
*/
		
		$('#calendar_details_close a').click(function(){
			$('#calendar_details').fadeOut();
			$('#calendar_details_content').html("");
		});
		
		$('.calendar_category div select').change(function(){
			calendarGet('year:'+$('#CalendarCategoryYear').val()+'/month:'+$('#CalendarCategoryMonth').val()+'/category:'+$('.calendar_category div select option:selected').val());
		});
		
		switch($('#CalendarCategoryId').attr('rel')) {
			case "8":
				$('#CalendarCategoryId').remove();
				break;
			case "9":
				$('#CalendarCategoryId').remove();
				break;
			case "10":
				$('#CalendarCategoryId').remove();
				break;
			default:
				$('#CalendarCategoryId option[value="8"]').remove();
				$('#CalendarCategoryId option[value="9"]').remove();
				$('#CalendarCategoryId option[value="10"]').remove();
				break;
		}
	}
	
	function calendarGet(link) {
		timestamp = new Date().getTime();
		href = webroot+'plugin/calendar/calendar_events/calendar/' + link + '&' +timestamp;
		$('#calendar').fadeOut();
		$.get(href,function(data){
			$('#greybox_content').html(data);
			calendarLink();
			$('#calendar').fadeIn();
		});
	}
	
	calendarLink();

	function fixSmall() {
		if(window.innerHeight < 680) {
			$('#wrapper').css('margin-top','-318px');
		}
	}
	
	fixSmall();
	
	function loadStaff() {
		$('#staff_listing li a').click(function(){
			$('#staff_template_details div').fadeOut();
			$.get($(this).attr("href"), function(data){
		  		$('#staff_template_details').html(data);
		  		$('#staff_template_details div').fadeIn();
			});
			return false;
		});
	}
	
	function loadDepartment() {
		$('#staff_template_menu a').click(function(){
			$('#staff_template_listing div').fadeOut();
			var href = $(this).attr("href");
			var parentLI = $(this).parent();
			$.get(href, function(data){
		  		$('#staff_template_listing').html(data);
		  		$('#staff_template_listing div').fadeIn();
		  		$('#staff_template_menu LI').css('background','url(../img/white_transparent_bg.png)');
		  		parentLI.css('background','url(../img/blue_transparent_bg.png)');
			});
			return false;
		});
	}

});