$(document).ready(function(){

	//jQuery.easing.def = "jSwing";

	//left menus
	 $('ul.jd_menu_vertical').jdMenu({showDelay:0, hideDelay:200, onAnimate: onAnimate, disableLinks:false});
	 function onAnimate(show) {
		//$(this).fadeIn('slow').show();
		if (show) {
			/*$(this)
				.css('visibility', 'hidden').show()
				.css('width', $(this).innerWidth())
				.hide().css('visibility', 'visible')
				.fadeIn('normal');
			*/
				$(this)
					.css('width','140px')
					.fadeIn('normal');
		} else {
			$(this).fadeOut('fast');
		}
	}
		
	//search dispaly and hide search
	$('#site_search input').focus(function(){
		if($(this).attr('value')=='search'){
			$(this).attr('value','');
		}
	}).blur(function(){
		if($(this).attr('value')===undefined){
			$(this).attr('value','search');
		}
	});
	
	//handle special links
	$("a[href^='http://']").attr("target","_blank");
	$("a[href^='mailto:']").addClass("email");
	$("a[href$='.pdf']").not('.no_icon').addClass("pdf").attr("target","_blank");
	//$("a[href$='.wav']").addClass("audio").attr("target","_blank");
	//$("a[href$='.mp3']").addClass("audio").attr("target","_blank");
	$("a[href$='.mpg']").addClass("video").attr("target","_blank");
	$("a[href$='.mpeg']").addClass("video").attr("target","_blank");
	$("a[href$='.mov']").addClass("video").attr("target","_blank");
	$("a[href$='.avi']").addClass("video").attr("target","_blank");
	$("a[href$='.wmv']").addClass("video").attr("target","_blank");
	$("a[href$='.rm']").addClass("video").attr("target","_blank");

	//rfb box bottom
	$(".right_floating_box").append('<div class="bottom"></div>');


	//hot happenings
	$('#hot_happenings .hh_content a.read_more').facebox();
	$('a[rel="facebox"]').facebox();
	$('#hot_happenings .hh_content td .holder').each(function(i) {
		$(this).cycle({
			delay:   -8000 + (4000 * (i+1)),
			timeout:  8000,
			pause:    1
		}); 
	});


	//FAQ
	$('dl.faq').find('dd').hide().end().find('dt').click(function() {
         var answer = $(this).next();
         if (answer.is(':visible')) {
             answer.slideUp();
         } else {
             answer.slideDown();
         }
     });

	 $('#expand_all').toggle(
		function(){
			$(this).text("collapse all");
			$('.expand_section').text('collapse section');
			$('dl.faq dd').slideDown();
		},
		function(){
			$(this).text("expand all");
			$('.expand_section').text('expand section');
			$('dl.faq dd').slideUp();
		}
	 );

	$('.expand_section').toggle(
		function(){
			$(this).text("collapse section");
			$('dl.faq dd.'+$(this).attr('rel')).slideDown();
		},
		function(){
			$(this).text("expand section");
			$('dl.faq dd.'+$(this).attr('rel')).slideUp();
		}
	);

	//FACILITIES AND TABLE STUFF
	$('.horiz_stripe tr:odd td').addClass('stripe');
	$('.round_top').nifty('top');
	$('.round_bottom').nifty('bottom');
	//$('.round_right').nifty('right');
	//$('.round_left').nifty('left,transparent');
	//$('.round').nifty();


	//SWF OBject?
	if($('#flash_slideshow').length){
		var so = new SWFObject("slideshow.swf", "slideshow", "480", "270", "8", "#ffffff");
			so.addParam("quality", "high");
			so.addParam("wmode", "transparent");
			so.addParam("salign", "t");
			so.write("flash_slideshow");
	}

	//NEWS HEADLINES?
	if($('#news').length){
		var t=setTimeout("loadRGMHeadline()",8000);
	}


	//force tab structure to grand ideas
	//loop through the h2
	$('#grand_idea_tabs').each(function(){
		//start the tab ul
		var $tabs = $('<ul id="tabs"></ul>');

		//add a class so we can stop at the headings
		$('h2', $(this)).addClass('tab_heading');
		//loop through all the h2s
		$('h2', $(this)).each(function(){
			//name of this chunk
			var name = $(this).text();
			var id = name.replace(/ /g,"_").toLowerCase();
			var content = [];

			//loop through all the siblings of this heading
			var sibs = $(this).nextAll();
			for(i=0; i<sibs.length; i++){
				//if its one of the headings stop
				//otherwise add it to the content variable
				if($(sibs[i]).attr('class') == "tab_heading")
					break;
				else
					content.push(sibs[i]);
			}
			//wrap all of the content into a div
			$(content).wrapAll('<div class="tab_content" id="tab_content_'+id+'"></div>');

			//create a list item for this content and add to the tabs
			$tab = $('<li>'+name+'</li>');
			$tab.attr('id',id);
			$tab.click(function(){
				$('#grand_idea_tabs .tab_content').hide();
				$('#grand_idea_tabs #tab_content_'+id).show();
				$('#tabs li.current').removeClass('current');
				$(this).addClass('current');
			});
			$tabs.append($tab);

			//hide the heading
			$(this).hide();

		});
		//call the click on the first tab to hide the rest
		$('li:first', $tabs).click();

		//add the tabs to the top of the page
		$(this).prepend($tabs);
		//add a clearer after the tabs
		$('#tabs').after('<br class="clearer" />');

		//special hooks for tab trigger links
		$('a.tab_trigger').click(function(){
			$('#tabs li#'+$(this).attr('rel')).click();
		});
	});
	


	//hotel package collapse
	$('.hotel_package_listing.collapse').each(function(){
		$('h3.listing_header', $(this)).siblings('div.holder').hide().end().click(function(){
			$(this).toggleClass('open').siblings('div.holder').slideToggle();
		});
	});

	//hotel package enter limit textarea
	$('textarea.limited').maxlength({
		'feedback' : '.chars_left',
		'useInput' : true,
		'words' : true
	});

	/*VISITORS GUIDE COUNTRY SWITCHING SHIPPING SELECT*/
	$('input[name="vr_country"]').change(function(){
		$('select[name^="shipping_"]').hide();
		$('select[name="shipping_'+$(this).val().toLowerCase()+'"]').show();
	})


	/*TOURING TUNES JS->FLASH INTERFACE*/
	if($('#media_player_container').length){
		var so = new SWFObject("media_player.swf", "media_player", "555", "36", "8", "#ffffff");
			so.addParam("quality", "high");
			so.addParam("wmode", "transparent");
			so.addParam("salign", "t");
			so.write("media_player_container");
	}
	$('.item_bar div, ul.latest_episodes li').click(function(e){
		e.stopPropagation();
		var song = $('p.flash_vars', $(this)).text();
		song = eval('(' + song + ')');
		//alert(song.title);
		mov = thisMovie("media_player");		
		mov.loadSong(song);
		return false;
	});
	/* MOVED OUTSIDE SINCE IT WILL BE CALLED BY FLASH NOW TO AVOID THIS LOADING QUICKER THAN FLASH
	$('#podcast_autostart').each(function(){
		var song = $('p.flash_vars', $(this)).text();
		song = eval('(' + song + ')');
		var mov = thisMovie("media_player");		
		mov.loadSong(song);
		return false;
	});
	*/

	//AJAX TWITTER MORE
	$('#more_tweets a').bind('click',function(){
		//add the loading animation
		var $loader = $('<div class="loading"><img src="layout_imgs/ajax_loader.gif" alt="loading" /></div>');
		$('#more_tweets').before($loader);
		//ditch the dotted rectangle
		$(this).blur();

		var max_id = $('#max_id').text();
		console.log($(this).attr('href'));
		$.getJSON(
			$(this).attr('href'),
			{'ajax':1, 'max_id':max_id},
			function(data){
				if(data.last_id>0){
					$('.loading').replaceWith(data.content);
					//change the current page
					$('#max_id').text(data.last_id);
				} else {
					$('.loading').remove();
					$('#more_tweets a').text('no previous tweets found').removeAttr('href').unbind('click');
				}
			}
		);

		return false;
	});

	//TAB MENU/TWITTER MENU THUMBNAIL PREVIEWS
	// Notice the use of the each method to gain access to each element individually
	$('#tab_menu a').each(function() {
		var content = new Array();
		content['Podcast'] = 'Audio podcasts with summer trip ideas';
		content['Twitter'] = 'Find Grand Rapids people to follow and see tweets without having an account';
		content['Twitter Feed'] = content['Twitter'];
		content['Facebook'] = 'Become a Fan of Grand Rapids CVB';
		content['You Tube'] = 'See the latest Grand Rapids videos here';
		content['Flickr'] = 'View photos or add your own here';


		// Setup the tooltip with the content
		$(this).qtip({
			content: content[$(this).attr('title')],
			style: {
				tip: true, // Give it a speech bubble tip with automatic corner detection
				name: 'blue',
				border: { radius:5 },
				width: {max:202}
			}
		});
	});

	//VIDEOS
	 //create the embed player
	if($('#video_player a.direct_video').length){
		var so = new SWFObject($('#video_player a.direct_video').attr('href')+'&amp;autoplay=1', "video", "560", "337", "8", "#ffffff");
			so.addParam("quality", "high");
			so.addParam("wmode", "transparent");
			so.addParam("salign", "t");
			so.write("embed");
	}
	//thumbnail click
	$('.video_thumb').click(function(){
		//set some variables
		var player = $(this).find('span.player').html();
		var title = $(this).find('span.title').html();
		var desc = $(this).find('span.desc').html();
		var link = $(this).find('span.link').html();
		
		//flash the embed div with the new player
		$('#embed').html('<img src="layout_imgs/ajax-loader.gif" alt="loading" />');
		var so = new SWFObject(player+'&amp;autoplay=1', "video", "560", "337", "8", "#ffffff");
			so.addParam("quality", "high");
			so.addParam("wmode", "transparent");
			so.addParam("salign", "t");
			so.write("embed");

		//update the video_info div
		$('#video_info h3').html(title);
		$('#video_info .desc').html(desc+'<p>'+link+'</p>');
		$('#video_jump').click();
		$.scrollTo($('#video_player'), {duration:1000});
	});

	//FLICKR
	$('#gallery a.facebox').click(function(){
		var next = $(this).next();
		var prev = $(this).prev();
		var img_src = $(this).attr('href');
		var title = $(this).attr('title');
		
		//load the image
		$.facebox({image: img_src, 'next':next, 'prev':prev, 'title':title});

		return false;
	});

});//end document ready

$(document).bind('beforeReveal.facebox', function(){
	if($.facebox.prev && $.facebox.prev.length){
		$prev = $('<a class="prev" title="view previous image">prev</a>');
		$prev.click(function(){
			$.facebox.prev.click();
		});
		$('#facebox .content').append($prev);
	}
	if($.facebox.next && $.facebox.next.length){
		$next = $('<a class="next" title="view next image">next</a>');
		$next.click(function(){
			$.facebox.next.click();
		});
		$('#facebox .content').append($next);
	}
	if($.facebox.title){
		$('#facebox .caption').text($.facebox.title);
	}
});

function podcastAutoStart(){
	console.log('autostart called');
	$('#podcast_autostart').each(function(){
		var song = $('p.flash_vars', $(this)).text();
		song = eval('(' + song + ')');
		var mov = thisMovie("media_player");		
		mov.loadSong(song);
		return false;
	});
}

function loadRGMFeed(page_id){
	
	$.ajax({
		url: "/getRGMFeed.php",
		data: "page_id="+page_id,
		success: function(msg){
			$('#news_content').fadeTo("normal",.01, function(){
				$(this).html(msg).fadeTo("normal",1);
			});
		}
	});

}

function loadRGMHeadline(){
	//get the current category and key value
	parts = $('#news div').attr('id').split("_");
	
	key = Number(parts[2])+1;

	//alert(key);

	$.ajax({
		url: "getRGMHeadline.php?page_id="+parts[1]+"&key="+key,
		success: function(msg){
			$('#news').fadeTo("normal",.01, function(){
				$(this).html(msg).fadeTo("normal",1);
				setTimeout("loadRGMHeadline()",8000);
			});
		}
	});
}

function thisMovie(movieName) {
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return (isIE) ? window[movieName] : document[movieName];
}


//CATCH CONSOLE.LOG FOR NO-FIREBUG
if (typeof console == 'undefined' || typeof console.log == 'undefined') { console = { log : function (text) { return false; } } }
