/* CRISS ANGEL */
/* Home Page Javascript
----------------------------------------------------------------------------- */


/* READY FUNCTION
----------------------------------------------------------------------------- */


	$(document).ready(function() {
	
		if (page == "index") cufon();
	
	});


/* LOAD FUNCTION
----------------------------------------------------------------------------- */


	$(window).load(function() {

		
/* FLASH: HEADER
----------------------------------------------------------------------------- */

		
		var plan; 
	
		if (plan_id == 45) {
	 
			plan = "45"; 
	 
		} else if (plan_id == 44) {
	 
			plan = "44"; 
	 
		} else if (plan_id == 56) {
	 
			plan = "56"; 
	 
		} else if (plan_id == 55) { 
	 
			plan = "55"; 
	 
		} else if (plan_id == 54) { 
	 
			plan = "43"; 
	 
		} else { 
	 
			plan = "43";
	
		}
	
		var loc = window.location;
		var so = new SWFObject(asset_host + "/flash/load.swf", "myswf", "100%", "365", "8", "#000000", true);
		so.useExpressInstall(asset_host + "/flash/expressinstall.swf");
		so.addVariable("plan", plan);
		so.addVariable("loc", loc);
		var _p = asset_host;
		so.addVariable("Path", _p);
		so.addParam("allowScriptAccess", "always");

		if (section == "home") so.addVariable("home", "1");
		if (section == "home") so.addVariable( "mute", "true" );
		if (section == "news") so.addVariable("page", "2");
		if (section == "mindfreak") so.addVariable("page", "3");
		if (section == "loyal") so.addVariable("page", "4");
		if (section == "criss") so.addVariable("page", "5");
		if (section == "merch") so.addVariable("page", "6");
		if (section == "legal") so.addVariable("page", "7");
		if (section == "members") so.addVariable("page", "7");
	
		so.write("header");
		
		

	
/* POPUP EXTERNAL LINKS
----------------------------------------------------------------------------- */


		$('a[rel=external]').click(function(){
		
			link = $(this).attr('href');
			window.open(link);
			return false;
	
		});
	

/* HOME PAGE FUNCTIONS
----------------------------------------------------------------------------- */


		if (page == "index") {
			
			twitter();
			placeholderText('#optin input[type=text]', 'Enter your email address');		
			slideshows();
			videoPlayer();
			$('.tabbed > ul').tabs({ cache: true, load: slideshows });
			$('.widget-media.browse.audio a.listen').click(audioPlayer);
			friends();
		//	texts();

		}
		
	});
	
	
/* FUNCTION: ADJUST TWEET HEIGHT
----------------------------------------------------------------------------- */


	function slideshows() {


/* News Ticker */		

		
		// Change hrefs
		$('#ticker ul.headlines li > a:visible').attr('href', '#happenings');
		
		// Cycle
		$('#ticker ul.headlines').cycle({
		
			speed: 300,
			timeout: 6500
		
		}).show();
				
		
/* Celebrity Photos */


		$('#celebphotos ul.photos').cycle({
		
			speed: 300,
			timeout: 5000,
    		next: '#next1',
   			prev: '#prev1'

		});


/* Fan Photos */


		$('#fanphotos ul.photos').cycle({
		
			speed: 300,
			timeout: 5000,
    		next: '#next2',
   			prev: '#prev2'

		});
		
		
/* Fan Photos */


		$('#featured').hover(function() {
		
			$('div.gallery').siblings('a').stop();
			$('div.gallery').siblings('a.next').animate({ right: 0 });
			$('div.gallery').siblings('a.prev').animate({ left: 0 });

		}, function() {
		
			$('div.gallery').siblings('a').stop();
			$('div.gallery').siblings('a.next').animate({ right: -50 });
			$('div.gallery').siblings('a.prev').animate({ left: -50 });
		
		});
		

	}

	
/* FUNCTION: TWITTER
----------------------------------------------------------------------------- */

		
	function twitter() {
	
		// Unhide Tweets
		$('#tweets').show();

		// Setup Slideshow
		$('#twitter_update_list').cycle({

			speed: 300,
			timeout: 7000,
			before: adjustTweetHeight
		
		});			
		
	}
	
	// Adjust the height of the tweet for each slide
	function adjustTweetHeight() {
	
		$('#twitter #tweets > ul').animate({ height: $(this).outerHeight() });
	
	}
	
	
/* Twitter API */	
	
		
	function twitterCallback2(twitters) {
	  var statusHTML = [];
	  for (var i=0; i<twitters.length; i++){
		var username = twitters[i].user.screen_name;
		var status = twitters[i].text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
		  return '<a href="'+url+'">'+url+'</a>';
		}).replace(/\B@([_a-z0-9]+)/ig, function(reply) {
		  return  reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'">'+reply.substring(1)+'</a>';
		});
		statusHTML.push('<li><span>'+status+'</span> <a style="font-size:85%" href="http://twitter.com/'+username+'/statuses/'+twitters[i].id+'">'+relative_time(twitters[i].created_at)+'</a></li>');
	  }
	  document.getElementById('twitter_update_list').innerHTML = statusHTML.join('');
	}
	
	function relative_time(time_value) {
	  var values = time_value.split(" ");
	  time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
	  var parsed_date = Date.parse(time_value);
	  var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
	  var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
	  delta = delta + (relative_to.getTimezoneOffset() * 60);
	
	  if (delta < 60) {
		return 'less than a minute ago';
	  } else if(delta < 120) {
		return 'about a minute ago';
	  } else if(delta < (60*60)) {
		return (parseInt(delta / 60)).toString() + ' minutes ago';
	  } else if(delta < (120*60)) {
		return 'about an hour ago';
	  } else if(delta < (24*60*60)) {
		return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
	  } else if(delta < (48*60*60)) {
		return '1 day ago';
	  } else {
		return (parseInt(delta / 86400)).toString() + ' days ago';
	  }
	}
		
	
/* FUNCTION: CUFON
----------------------------------------------------------------------------- */


	function cufon() {
	
		// Identify Cufon text
		if (!navigator.userAgent.match('MSIE')) {

			// Internet Explorer
			replaceUs = "#ticker ul.headlines > li > a:not(.comments), #happenings ul.leads h4, #twitter li > span, #tricks ul.clips li div > a:not(.comments), #watch ul.clips li div > a:not(.comments)";
		
		} else {
		
			// Good Browsers
			replaceUs = "#ticker ul.headlines > li > a:not(.comments), #happenings ul.leads h4, ,#tricks ul.clips li div > a:not(.comments), #watch ul.clips li div > a:not(.comments)";

		}
		
		// Hide
		$(replaceUs).css('visibility', 'hidden');
		
		// Replace
		Cufon.replace(replaceUs, { hover: true });
		
		// Show
		$(replaceUs).removeAttr('style');
	
	}


/* FUNCTION: PLACEHOLDER TEXT
----------------------------------------------------------------------------- */


	function placeholderText(input, text) {
	
		var $this = $(input);
	
		if (navigator.userAgent.match('WebKit')) {
			
			// Use real placeholder attr when available
			$this.attr('placeholder', text);
			
		} else {
			
			// Fake it for other browsers
			
			$this.val(text);
						
			$this.focus(function() {
			
				if ($this.val() == text) $this.val('');
			
			});
			
			$this.blur(function() {
			
				if (!$this.val()) $this.val(text);
			
			});
		
		}
	
	}


/* FUNCTION: VIDEO PLAYER
----------------------------------------------------------------------------- */


	function videoPlayer() {
	
		$('div.widget-media.player.video').each(function() {
		
			var $this = $(this);
		
			$('#featured').hover(function() {
		
				$this.children().stop();
				$this.children('ul.controls').animate({ bottom: 0 });
				$this.children('div.details').animate({ top: 0 });
				
				// Internet Explorer
				if (navigator.userAgent.match('MSIE')) {
				
					$this.find('div.flash').children('div').animate({
					
						height: '240px',
						marginTop: '29px'
					
					});
				
				}

			}, function() {
			
				$this.children().stop();
				$this.children('ul.controls').animate({ bottom: '-43px' });
				$this.children('div.details').animate({ top: '-29px' });

				// Internet Explorer
				if (navigator.userAgent.match('MSIE')) {

					$this.find('div.flash').children('div').animate({
					
						height: '296px',
						marginTop: '0'
					
					});
				
				}

			});
			
		});
	
	}
	
	
/* FUNCTION: FRIEND ALERT
----------------------------------------------------------------------------- */


	function friends() {
	
		if ($('.widget-alert').length > 0 && $('.widget-alert h3').text().match(/friend/i)) {
		
			$('.widget-alert').append('<p><a href="/friends">Confirm or reject their request on the Friends page</a></p>');
		
		}
	
	}

	
/* FUNCTION: AUDIO PLAYER
----------------------------------------------------------------------------- */


	function audioPlayer() {
	
		if (!navigator.userAgent.match('MSIE')) {

			// REMOVE OLD PLAYER
			$('#audioplayer').each(function() { $(this).remove(); });
				
			if ($(this).hasClass('stop')) {
	
				// REMOVE SELECTED INDICATOR
				$(this).text('Listen').removeClass('stop').addClass('listen');
	
			} else {
	
				// CREATE NEW VIDEO PLAYER CONTAINER
				$('body').append('<div id="audioplayer" style="position:absolute;top:-1px;left:-1px;width:1px;height:1px;"></div>');
				
				// GET CLIP
				var clip = $(this).attr('href');
				
				// EMBED NEW PLAYER    	
				var so = new SWFObject(site_host + '/flash/player.swf', 'player', '100%', '100%', '9', '#000000');
				so.addParam('allowScriptAccess', 'always');
				so.addVariable('playerID', 'player');
				so.addVariable('mediaMode', 'audio');
				so.addVariable('autoPlay', true);
				so.addVariable('clipURL', clip);
				so.write('audioplayer');
	
				// SET SELECTED INDICATOR
				$(this).parent().parent().parent().parent().siblings().find('a.stop').text('Listen').removeClass('stop').addClass('listen');
				$(this).text('Stop').addClass('stop').removeClass('listen');
				
			}
			
			return false;
			
		}
		
	}
	
	
/* FUNCTION: TEXTS
----------------------------------------------------------------------------- */


	function texts() {
	
		setInterval(function() {
		
			var key = Math.floor(Math.random() * 11);
			var instance = $('#texts iframe').attr('src').split('?')[0] + '?instance=' + key;
		
			$('#texts iframe').attr('src', instance);
			
		}, 60000);
	
	}