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


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


	$(document).ready(function() {
	
		
		if (page == "index") {
		
			markup();
			cufon();
			
			// PLUGIN: CLIQUE ACTIONS (Replaces submit buttons with text links)
			try { $('input[type=submit]').cliqueActions(); } catch (error) { alert('Error: Missing Clique Actions Plugin'); }
			
			}
	
	});


/* 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";
	
		}
		
		if (page != "index") {
	
			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");
			
		} else {
		
			var loc = window.location;
			var so = new SWFObject(asset_host + "/flash/ca_header-slim.swf", "_swf", "100%", "195", "9", "#000000", true);
			so.useExpressInstall(asset_host + "/flash/expressinstall.swf");
			so.addParam("menu", "false");
			so.addParam("scale","noscale");
			
			so.write("flashcontent");
		
		}

	
/* 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');		
			placeholderText('#query input[type=text]', 'Search store merchandise');	
			slideshows();
			$('.tabbed > ul').tabs({ cache: true, load: slideshows });
			friends();
			//scrollItems();
			truncate("#ticker-news a", 75);
			truncate("#ticker-upcoming h4 a", 40);
			truncate("#featured div.details a", 48);
			truncate("div.content.pane div.widget-item.browse ul.items li a", 54);
			truncate("#vault ul.photos li h4", 65);

		}
		
	});
	
	
/* FUNCTION: SLIDESHOWS
----------------------------------------------------------------------------- */


	function slideshows() {


/* News Ticker */		
		
		// Cycle
		$('#ticker-news ul.headlines').cycle({
		
			speed: 300,
			timeout: 7000
		
		}).show();
		

/* Upcoming Events Ticker */		

		
		// Cycle
		$('#ticker-upcoming ul.events').cycle({
		
			speed: 300,
			timeout: 7000
		
		}).show();
				
		
/* Celebrity Photos */


		$('#vault 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'

		});
		

/* Featured Merchandise */


		$('#featured-merchandise ul.items').before('<div class="pager">').cycle({
		
			speed: 600,
			timeout: 5000,
			pager: 'div.pager',
			before: function() { if (window.console) console.log(this.src); }

		});
		

/* 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: CUFON
----------------------------------------------------------------------------- */


	function cufon() {

		var din_condensed = [
		
			'#ticker-news ul.headlines > li > a:not(.comments)',
			'#ticker-upcoming ul li', 
			'#ticker-upcoming a.date', 
			'#ticker-upcoming h4 a', 
			'#blog ul.leads h4', 
			'#tricks ul.clips li div > a:not(.comments)', 
			'#watch ul.clips li div > a:not(.comments)',
			'#mindfreak-series ul.tabs a'
			//'div.details > a:not(.comments)'
						
		];
		
		var din_schrift = [
		
			'#shop > ul.tabs li a'
			
		];
		
		var garaline = [
		
			'#module > h3',
			'#blog > h3',
			'#shop > h3',
			'#vault h3',
			'#mindfreak-photos > h3',
			'#shop div.content > h3',
			'#featured div.sidebar > h3'
			
		];
		
		Cufon.replace(din_condensed, { fontFamily: 'din', hover: true });
		$(din_condensed.toString()).css('visibility', 'visible');

		Cufon.replace(din_schrift, { fontFamily: 'DINSchrift', hover: true });
		$(din_schrift.toString()).css('visibility', 'visible');
		
		Cufon.replace(garaline, { fontFamily: 'Garaline', hover: true });
		$(garaline.toString()).css('visibility', 'visible');
				
	}



/* 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: 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: 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);
	
	}
	
	
/* FUNCTION: MARKUP CHANGES
----------------------------------------------------------------------------- */


	function markup() {
	
		//Upcoming Ticker		
		$('#ticker-upcoming > ul.events > li:eq(0) > h4 > a > strong').appendTo('#ticker-upcoming > ul.events > li:eq(0) > a.date');
		$('#ticker-upcoming > ul.events > li:eq(1) > h4 > a > strong').appendTo('#ticker-upcoming > ul.events > li:eq(1) > a.date');
		$('#ticker-upcoming > ul.events > li:eq(2) > h4 > a > strong').appendTo('#ticker-upcoming > ul.events > li:eq(2) > a.date');
		$('#ticker-upcoming > ul.events > li:eq(3) > h4 > a > strong').appendTo('#ticker-upcoming > ul.events > li:eq(3) > a.date');
		$('#ticker-upcoming > ul.events > li:eq(4) > h4 > a > strong').appendTo('#ticker-upcoming > ul.events > li:eq(4) > a.date');
		
		//Actions
		$('#blog a.action.browse').text('Browse All Posts >');
		$('#mindfreak-photos a.action.browse').text('Browse All Photos >');
		
		
	}
	
	
/* FUNCTION: TWITTER
----------------------------------------------------------------------------- */	
	
	
	function twitter() {
	
		// Unhide Tweets
		$('#tweets').show();

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

			speed: 300,
			timeout: 7000
		
		});			
		
	}

	
/* 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: SHOP ITEMS SCROLLING
----------------------------------------------------------------------------- */

	
	function scrollItems() {
				
		$('#popular ul.items').jcarousel({
			
			scroll: 3,
			auto: 3,
			visible: 4,
			wrap: "last"

						
		});

	}
	
	
/* FUNCTION: TRUNCATE
---------------------------------------------------------------------------- */

	
	function truncate(selector, length) {
	
		$(selector).each(function() {
		if ($(this).text().length > length) {
		var text = $(this).text().slice(0, length);
		var text = text.slice(0, text.lastIndexOf(' ')) + '...';
		$(this).html(text);
		Cufon.refresh();
		}
		});
		
	}	
	
	
	
