//function to adjust layout of the homepage if the latest news post is too short.
function checkHomepageHeight(){
	//make sure we are on the homepage
	if($("#container").hasClass('homepage')){
		//store the content height of the news element
		var postHeight = $("#latestnews > .post").innerHeight();
		//replace the banners if the content is less than 610px - Changed to account for 4 banners TJR 11/21/2009
		if(postHeight < 760){
			$("#banners").addClass('short');
			$('#banners.short').cycle({ 
    		fx:      'fade',
    		speed:   500,
    		timeout: 10000
			});
		}
		
		//add some height if the content is too short for even 1 banner to show
		if(postHeight < 280){
			$("#latestnews > .post").css('height',230);
		}
		
	}
}

//function to init the retweet counters to check for their updated counts periodically
function retweetCounts(){
	clearInterval('rt_interval');
	var counter_ids = new Array();
	$(".rt_count").each(function(i) {
		//counter_ids.push($(this).attr('id'));
		getRetweetCount($(this).attr('id'));
		var rt_interval = setInterval("getRetweetCount('"+$(this).attr('id')+"')",10000);
	});
	
	
}

function getRetweetCount(elem_id){
	var pars = 'elem_id='+elem_id;
	
	$.ajax({
	type: "POST",
	url: "ajax.getRetweetCount.php",
	data: pars,
	success: function(msg){
		$("#"+elem_id).html(msg);
	}
	});
}

//init the homepage news section to advance to the second news item
function initHomeNews(){
	//make sure we are on the homepage
	if($("#container").hasClass('homepage')){
		scrollNews('down');
	}
}

//functions to scroll the tour right column to make the current tour item centered in the container
function initTourHolder(){
	if($("#last15_container").hasClass('show_holder')){
		centerTourItem();
	}
}

function centerTourItem(){
	
	var total_shows = $("#last15 > li").siblings().length;
	var current_location = window.location.toString();
	var item_index; //will hold the location in the list of the current tour item
	
	$("#last15 > li > a").each(function(i) {	
		var this_loc = $(this).attr('href');
		//grab the matching substring from the current location
		var index = current_location.length - this_loc.length;
		var curr_loc = current_location.substring(index);
		
		if(this_loc == curr_loc){ $(this).parent().addClass('current');item_index = i;}
		
	});
	
	//we will center the container if we can, but we cant do that for the first 8 items
	if(item_index > 7 && item_index < (total_shows-8)){
		var offset = -1 * ((item_index-7) *21) + 4;
		$("#last15").css('top',offset+'px');
		
	}else if(item_index >= (total_shows-8)){
		var offset = -1 * ($("#last15").outerHeight() - (18 * 21));
		$("#last15").css('top',offset+'px');
	}
	
	//make the nav active, in need-be
	if(item_index > 7){
		$("#scroll_last_up").removeClass('off');
		$("#scroll_last_up").addClass('on');
	}
	if(item_index >= (total_shows-8)){
		$("#scroll_last_down").removeClass('on');
		$("#scroll_last_down").addClass('off');
	}
	
}

function formEvents(){
	//get all forms on page
	var allforms = document.getElementsByTagName('form');
	
	//loop through forms
	for(i=0;i<allforms.length;i++){
		
		//get all input tags in current form
		var thisform = allforms[i].getElementsByTagName('input');
		
		//loop through those inputs
		for(j=0;j<thisform.length;j++){
			//qualify that the input is a text input, and has a title, and isn't being excluded by using the 'noevents' class
			if(thisform[j].title && thisform[j].title != '' && thisform[j].type != 'image' && thisform[j].type != 'button' && thisform[j].type != 'submit' && thisform[i].className != 'noevents'){
				//assign the title to the value
				thisform[j].value = thisform[j].title;
				//events that will remove/replace default values into fields
				thisform[j].onfocus = function(){if(this.title == this.value){this.value = '';}}
				thisform[j].onblur = function(){if(this.value == ''){this.value = this.title;}}
			}
		}
	}
}

		function nav_overs(id,h,spd){
			$('#'+id+' li').hover(function() {
        	$(this).children('.link a').animate({ marginTop: -h }, spd);
      		}, function() {
       		$(this).children('.link a').animate({ marginTop: -0 }, spd);          
      		});	
		}
		
		function list_overs(id,spd){
			$('#'+id+' a').hover(function() {
        	$(this).animate({ paddingLeft: '10px' }, spd);
     		}, function() {
        	$(this).animate({ paddingLeft: '0px' }, spd);    
     		});
      	}
		
		function globalnav_overs(id){
        	$('#'+id+' li').hover(
			function(){$(this).addClass('over');},
			function(){$(this).removeClass('over');}
			);
		}
		
		function showOffer(){
			//$("#musicplayer").html('Music player is here. <a onclick="launchPlayer();">Pop out the player</a> <a onclick="closePlayer();">Close player</a>');
			$("#musicplayer").html('<div id="player"><script type="text/javascript">swfobject.embedSWF("jm_player.swf", "player", "960", "25", "10.0.0", "expressInstall.swf", flashvars, params, attributes);</script></div>');
			$("#musicplayer").animate({ marginTop: '0px' }, '500');
		}
		
		function timedFadeOut(id,cls){
			$("#"+id).fadeTo('fast',0.0);
			$("#"+id).html('');
			$("#"+id).removeClass(cls);
			
		}
		
		function launchPlayer(){
      		window.open('player.html','player','width=960,height=150,scrollbars=0,menubar=0,status=0,resizable=0,location=0');
      		$("#musicplayer").css("margin-top","-25px");
      		$("#musicplayer").html('');
      		$("#launch").html('Launch <a id="launch" onclick="showOffer();">Music Player</a>');
      		return false;	
   		}
   		
   		function closePlayer(){
      		$("#musicplayer").animate({ marginTop: '-25px' }, '300');
      		$("#musicplayer").html('');
      		$("#launch").html('Launch <a id="launch" onclick="showOffer();">Music Player</a>');
      		return false;	
   		}
			
   		function tourOvers(){
   			$("div.touritem").hover(
			function(){$(this).addClass('over');},function(){$(this).removeClass('over');
			});
		}
		
   		function launchTweet(src,link){
      		window.open('tweet.php?source='+src+'&guid='+link+'','connect','width=800,height=390,scrollbars=0,menubar=0,status=0,resizable=0,location=0');
      		return false;	
   		}
   		
   		function viewTweets(user){
   			window.open('http://www.twitter.com/'+user+'','Twitter');
   			window.close();
   		}
   		
   		var scrolling;
   		
   		function start_scrollTweets(dir){
   			scrolling = setInterval("scrollTweets('"+dir+"')",25);
   		}
   		
   		function stop_scrollTweets(){
   			clearInterval(scrolling);
   		}
   		
   		function scrollTweets(dir){
   			var curr_top = sansPX($("#tweets").css('top'));
   			var min_top = -1 *($("#tweets").outerHeight());
   			var offset = sansPX($("#tweets_container").css('height'));
   			var max_top = 0;
   			
   			
   			if(dir == 'down'){
   				if(curr_top > (parseInt(min_top) + parseInt(offset))){
   					var new_top = parseInt(curr_top) - 5;
   				}
   			}else{
   				if(curr_top < max_top){
   					var new_top = parseInt(sansPX(curr_top)) + 5;
   				}
   			}
   			
   			if(curr_top <= (parseInt(min_top) + parseInt(offset))){
   				$("#scroll_tweet_down").removeClass('on');
   				$("#scroll_tweet_down").addClass('off');
   			}else{
   				$("#scroll_tweet_down").removeClass('off');
   				$("#scroll_tweet_down").addClass('on');
   			}
   			
   			if(curr_top >= max_top){
   				$("#scroll_tweet_up").removeClass('on');
   				$("#scroll_tweet_up").addClass('off');
   			}else{
   				$("#scroll_tweet_up").removeClass('off');
   				$("#scroll_tweet_up").addClass('on');
   			}
   			
   			
   			$("#tweets").css('top',new_top);
   		}
   		
   		function start_scrollLast(dir){
   			scrolling = setInterval("scrollLast('"+dir+"')",25);
   		}
   		
   		function stop_scrollLast(){
   			clearInterval(scrolling);
   		}
   		
   		function scrollLast(dir){
   			var curr_top = sansPX($("#last15").css('top'));
   			var min_top = -1 *($("#last15").outerHeight());
   			var offset = sansPX($("#last15_container").css('height'));
   			var max_top = 0;
   			
   			if(dir == 'down'){
   				if(curr_top > (parseInt(min_top) + parseInt(offset))){
   					var new_top = parseInt(curr_top) - 5;
   				}
   			}else{
   				if(curr_top < max_top){
   					var new_top = parseInt(sansPX(curr_top)) + 5;
   				}
   			}
   			
   			if(curr_top <= (parseInt(min_top) + parseInt(offset))){
   				$("#scroll_last_down").removeClass('on');
   				$("#scroll_last_down").addClass('off');
   			}else{
   				$("#scroll_last_down").removeClass('off');
   				$("#scroll_last_down").addClass('on');
   			}
   			
   			if(curr_top >= max_top){
   				$("#scroll_last_up").removeClass('on');
   				$("#scroll_last_up").addClass('off');
   			}else{
   				$("#scroll_last_up").removeClass('off');
   				$("#scroll_last_up").addClass('on');
   			}
   			
   			
   			$("#last15").css('top',new_top);
   		}
   		
   		function scrollNews(dir){
   			
   			var vis = $(".home_news_item.vis").length;
   			var all = $(".home_news_item").length;
   			
   			if(dir == 'down'){
   				if(vis > 1){
   				$(".home_news_item.vis:last").fadeOut("slow");
   				$(".home_news_item.vis:last").removeClass('vis');
   				}
   			}else{
   				if(vis < all){
   				$(".home_news_item:not(.vis):first").fadeIn("slow");
   				$(".home_news_item:not(.vis):first").addClass('vis');
   				}
   			}
   			
   			//reset after the update
   			var vis = $(".home_news_item.vis").length;
   			
   			//set the nav
   			if(vis <= 1){
   				$("#scroll_news_down").removeClass('on');
   				$("#scroll_news_down").addClass('off');
   			}else{
   				$("#scroll_news_down").removeClass('off');
   				$("#scroll_news_down").addClass('on');
   			}
   			
   			if(vis < all){
   				$("#scroll_news_up").removeClass('off');
   				$("#scroll_news_up").addClass('on');
   			}else{
   				$("#scroll_news_up").removeClass('on');
   				$("#scroll_news_up").addClass('off');
   			}
   			
   		}
   		
   		function scrollBlog(dir){
   			
   			var vis = $(".home_blog_item.vis").length;
   			var all = $(".home_blog_item").length;
   			
   			if(dir == 'down'){
   				if(vis > 1){
   				$(".home_blog_item.vis:last").fadeOut("slow");
   				$(".home_blog_item.vis:last").removeClass('vis');
   				}
   			}else{
   				if(vis < all){
   				$(".home_blog_item:not(.vis):first").fadeIn("slow");
   				$(".home_blog_item:not(.vis):first").addClass('vis');
   				}
   			}
   			
   			//reset after the update
   			var vis = $(".home_blog_item.vis").length;
   			
   			//set the nav
   			if(vis <= 1){
   				$("#scroll_blog_down").removeClass('on');
   				$("#scroll_blog_down").addClass('off');
   			}else{
   				$("#scroll_blog_down").removeClass('off');
   				$("#scroll_blog_down").addClass('on');
   			}
   			
   			if(vis < all){
   				$("#scroll_blog_up").removeClass('off');
   				$("#scroll_blog_up").addClass('on');
   			}else{
   				$("#scroll_blog_up").removeClass('on');
   				$("#scroll_blog_up").addClass('off');
   			}
   			
   		}
   		
   		//function to help with notifications signup
   		function notify(){
   			
   			var fn = $("#firstname").val();
   			var ln = $("#lastname").val();
   			var em = $("#email").val();
   			var ls_news = $("#lists-news").attr('checked');
   			var ls_tour = $("#lists-tour").attr('checked');
   			var ls_blog = $("#lists-blog").attr('checked');
   			var ls_team = $("#lists-team").attr('checked');
   			var ls_scotty = $("#lists-scotty").attr('checked');
   			var ls_crash = $("#lists-crash").attr('checked');
   			var ls_tony = $("#lists-tony").attr('checked');
   			
   			var ls = ls_news+','+ls_tour+','+ls_blog+','+ls_team+','+ls_scotty+','+ls_crash+','+ls_tony;
   			
   			var pars = 'firstname='+fn+'&lastname='+ln+'&email='+em+'&lists='+ls;
			
			$.ajax({
			type: "POST",
			url: "ajax.notifications.php",
			data: pars,
			success: function(msg){
				$("#mailinglist_notification").html(msg);
				$("#mailinglist_notification").addClass('notification');
			}
			});
   			return false;
   		}
   		
   		/****GET THE MEASURE SANS PX*******************************************/
		
		function sansPX(str){
			var tempMeasure = str;
			if(tempMeasure == ''){return 0;}
			else {
				if(tempMeasure.substring(tempMeasure.length-2, tempMeasure.length) == 'px'){
					var asInt = tempMeasure.substring(0, tempMeasure.length-2);
					return asInt;
				}else{
					return tempMeasure;
				}
			}
			
		}
 

//functions used in gallery
var current_movie_content ='';
var current_movie_container = '';

function showMovie(elem,fn){
	movieStarted();
	current_movie_content = $("#"+elem).html();
	current_movie_container = elem;
	
	var mp = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="560" height="316" id="videoplayer" align="middle">';
	mp += '<param name="allowScriptAccess" value="always" />';
	mp += '<param name="allowFullScreen" value="false" />';
	mp += '<param name="moviename" value="cms/uploads/gallery/'+fn+'/560.flv" />';
	mp += '<param name="movie" value="videoplayer.swf" />';
	mp += '<param name="quality" value="high" />';
	mp += '<param name="bgcolor" value="#000000" />';
	mp += '<embed src="videoplayer.swf" flashvars="moviename=cms/uploads/gallery/'+fn+'/560.flv" quality="high" bgcolor="#000000" width="560" height="316" name="videoplayer" align="middle" allowScriptAccess="always" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
	mp += '</object>';
	$("#"+elem).html(mp);
}

function movieStarted(){
	$('#gallery').cycle('pause');
}
		
function movieStopped(){
	$("#"+current_movie_container).html(current_movie_content);
	$('#gallery').cycle('resume');
}
		
/**** INTERNATIONAL FUNCTIONS **************************************************/

function chooseCountry(site){
	var pars = 'site='+escape(site);
	
	set_international_cookie(site);
	/*
	$.ajax({
	type: "POST",
	url: "ajax.set-intl-cookie.php",
	data: pars,
	success: function(msg){
		window.location=site;
	}
	});
	*/
	return false;
	
}

//*FUNCTION FOR SETTING COOKIE********************************************************************//

function set_international_cookie(site){
	
	//clear the old cookie
	var cleared = clear_international_cookie('intl_site');
	
	if(cleared){
	
	expireDate = new Date;
	//expireDate.setHours(expireDate.getHours()+(2)); //2hours
	expireDate.setTime(expireDate.getTime() + (1000 * 60 * 60 * 24 * 60));
	
	var flashPair = 'intl_site='+site+';expires='+expireDate.toGMTString()+';path=/';
	document.cookie = flashPair;
	
	window.location=site;
	
	}
	
}

//************************************************************************************************//

//*FUNCTION FOR CLEARING COOKIE********************************************************************//

//function for clearing cookie.
function clear_international_cookie(cookieName){
	
	var thisCookie = document.cookie.split('; ');
	
	expireDate = new Date;
	expireDate.setHours(expireDate.getHours()-1);
	
	for(i=0; i<thisCookie.length; i++){
		if(cookieName == thisCookie[i].split('=')[0]){
			thisCookie[i] = 'intl_site='+thisCookie[i].split('=')[1]+';expires='+expireDate.toGMTString()+';path=/';
		}
	}
	
	return true;
}

//************************************************************************************************//

//*FUNCTION FOR GETTING COOKIE********************************************************************//

function get_international_cookie_value(cookieName){
	var thiscookie = document.cookie.split('; ');
	for(i=0; i<thiscookie.length; i++){
		if(cookieName == thiscookie[i].split('=')[0]){
			return thiscookie[i].split('=')[1];
		}
	}
	//if we got no matches
	return false
}

//************************************************************************************************//


function initInternationalNav(){
	
	$("#intl_selector").click(function () {
      $("#intl_selector_options").toggle();
    });

}



