/* CRISS ANGEL */
/* JAVASCRIPT
----------------------------------------------------------------------------- */


	var $j = jQuery.noConflict();


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


	$j(document).ready(function() {
	
	/*
		$j('div.widget-advertising.overlay').wrap('<div id="overlay"></div>');
		$j('#overlay div.widget-advertising').append('<div id="text"><p>Don&#39;t miss the first LIVE episode in MINDFREAK history as Criss attempts to escape from an imploding building! Watch it LIVE on Wednesday, July 30 at 10 PM EST / 7 PM PST on A&amp;E, or <a class="mindfreak" href="http://www.crissangel.com/mindfreak">click here</a> to watch it live NOW!</p><a class="close" href="#">&times; Close</a></div>');
		$j('#overlay a.close').click(hideOverlay); 
	
 	*/
	
 
/* HIDDEN AUDIO PLAYER
----------------------------------------------------------------------------- */


		var myAudio = new mediaObject(asset_host + "/flash/audio_player.swf", "520", "467", "#000000", 'hidden','true'); 
		myAudio.setFeeds(site_host + "/playlists/audio.xml");


/* POPUP VIDEO PLAYER
----------------------------------------------------------------------------- */


		var videoPopup = new mediaObject(asset_host + "/flash/popup_video.swf", "500", "500", "#000000", 'popup');
		videoPopup.setFeeds(asset_host + "/xml/videos-all.xml");
		videoPopup.setPopupOptions("window_name", "toolbar=0, location=0, directories=0, status=0, menubar=0, scrollbars=0, resizable=0, width=500, height=500", "/player-video");


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


		if (page == "can-site" || page == "can-profile") {
				
			// SMALL HEADER
			var so = new SWFObject(asset_host + "/flash/preloadNH.swf", "nheader", "100%", "255", "8", "#000000");
			so.addParam( "allowScriptAccess", "always" );
			so.addVariable("netPath", site_host);
			so.addVariable("path", asset_host + "/flash/network_header.swf");
			so.write("navigation");
		
		} else {
			
			// MAIN 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");
			if (section == "store") so.addVariable("page", "6");
			if (section == "contest") so.addVariable("mute", "true");

			
			//muting
			if ( topic == "believe-23" ) {
				so.addVariable( "mute", "true" );
			}
			
			so.write("header");
		
		}
		
		
/* FLASH: CIRQUE COUNTDOWN
----------------------------------------------------------------------------- */


		if ($j('#cirque').length > 0) {

			var cirque_so = new SWFObject( asset_host + "/flash/cirque_countdown.swf", "cirqueSWF", "292", "362", "8", "#000000" );
			cirque_so.addParam( "allowScriptAccess", "always" );
			cirque_so.addParam( "wmode", "transparent" );
			cirque_so.addVariable( "xml_param", asset_host + "/xml/cirqueClock.xml" );
			cirque_so.write( "cirque" ); 

		}
		
		
/* FLASH: ROTATING BANNERS
----------------------------------------------------------------------------- */
		
		
		if ($j('#banners').length > 0) {
		
			var so = new SWFObject(asset_host + "/flash/banners.swf", "banners", "100%", "100%", "8", "#000000");
			so.addParam("scale", "noscale");
			so.addVariable( "xml", asset_host + "/xml/banners.xml" )
		
			if (page == "index") so.addVariable("placement", "Home");
			if (page == "loyal") so.addVariable("placement", "Home");
		
			so.write("banners");
		
		}
		
		
/* FLASH: VIDEO PLAYER (MINDFREAK PLAYER)
----------------------------------------------------------------------------- */
		
		
		if ($j('#mindfreak-player').length > 0) {

			var _cc;
			
			if (plan_id == 45 || plan_id == 44 || plan_id == 55 || plan_id == 56) {
			
				_cc = 'true';
				
			} else {
			
				_cc = 'false';
				
			}	
	
			var myVideo = new mediaObject(asset_host + "/flash/small_player.swf?logo=mftv&cc=" + _cc, "520", "484", "#000000", 'mindfreak-player','false'); 
	
			if (plan_id == 55 || plan_id == 56) {
			
				myVideo.setFeeds(site_host + "/playlists/mindfreak-exclusive.xml");
			
			} else {
			
				myVideo.setFeeds(site_host + "/playlists/mindfreak.xml");
			
			}
	
			myVideo.setLinks("mftv","mftv-video");
			myVideo.playMedia();

		} 
		
		
/* FLASH: VIDEO PLAYER (CATV)
----------------------------------------------------------------------------- */

		
		if ($j('#catv').length > 0) {

			var _cc;
			
			if (plan_id == 45 || plan_id == 44 || plan_id == 55 || plan_id == 56) {
			
				_cc = 'true';
				
			} else {
			
				_cc = 'false';
				
			}

			var myVideo = new mediaObject(asset_host + "/flash/small_player.swf?logo=catv&cc=" + _cc, "520", "484", "#000000", 'catv','false'); 

			if (plan_id == 55 || plan_id == 56) {
			
				myVideo.setFeeds(site_host + "/playlists/criss-exclusive.xml");
			
			} else {
			
				myVideo.setFeeds(site_host + "/playlists/criss.xml");
			
			}

			myVideo.setLinks("catv","catv-video");
			myVideo.playMedia();
			
		}
	
		
/* FLASH: ENLARGED VIDEO PLAYER
----------------------------------------------------------------------------- */

		
		if (page == "mftv-video") {
		
			if (plan_id == 54 || plan_id == 55 || plan_id == 56) {
			
				var myVideo = new mediaObject(asset_host + "/flash/large_player.swf?logo=mftv&cc=true", "793", "616", "#000000", "player","true"); 
	
			} else {
			
				var myVideo = new mediaObject(asset_host + "/flash/large_player.swf?logo=mftv&cc=false", "793", "616", "#000000", "player","true"); 
	
			}	
	
			myVideo.setFeeds(site_host + "/playlists/video.xml");
			myVideo.setLinks("videos","video");
			myVideo.playMedia(topic);

		}
		

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


	});


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


	$j(window).load(function() {	
	
		twitter();
		addFlash();
		showOverlay();
		doMagic();
		slideshow();
		tricksCookie();
				

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


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


/* CHAT FUNCTIONS
-----------------------------------------------------------------------------

	if (page == "loyalfest" || page == "chat-popup") {
		
		var embed = $j('div#chat table');
		
		$j('a.launchchat').click(function(){
		
			$j('div#chat').addClass('dim');
			$j('div#chat').prepend('<a class="reembed" href="#">Re-embed Chat Here</a>');
			$j('div#chat table').remove();

			window.open( site_host + '/chat-popup','Chat','width=600,height=440,resizable=0,scrollbars=0,toolbar=0,location=0,directories=0,status=0,menubar=0');
			
			$j('a.reembed').click(function(){

				$j('div#chat').append(embed);
				$j('a.reembed').remove();
				
				return false;
				
			});
			
			return false;
			
		});
		
		$j('a.closechat').click(function(){
	
			window.close();
			
		});
		
	} */

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


	});
	

/* OVERLAY TOGGLE FUNCTIONS
----------------------------------------------------------------------------- */


	function addFlash() {
	
		$j("#header").addClass("flash");
		$j("#cirque").addClass("flash");
		$j("#banners").addClass("flash");
	
	}
	
	function showOverlay() {
	
		// $j('#page-home div.flash').css('visibility','hidden');
		// $j('#dimmer').fadeIn('fast');
		// $j('#overlay').animate({top: 0}, 'slow');
		
	}

	function hideOverlay() {
	
		$j('#overlay').slideUp('fast');
		$j('#dimmer').fadeOut('fast');
		$j('#page-home div.flash').css('visibility','visible');
		return false;
	
	}
	
	function slideshow() {
		
		if ($j('#page-login div.benefits div').length > 0) {

			$j('div.benefits div').cycle({
		
				fade: true,
				speed: 400,
				timeout: 6000
			
			});

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

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

		// Setup Slideshow
		$j('#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: TRICKS COOKIE
----------------------------------------------------------------------------- */

	function tricksCookie() {
		
		if (page == "tricks") {
			
			$j('#code').parent('form').submit(function() {
				
				$j.cookie("criss-angel-tricks-code", "true", { expires: 365, domain: "sparkart.net" });
		
			});
			
			var tricks_code = $j.cookie('criss-angel-tricks-code');
			
			if (tricks_code == "true") {

				$j('#code').hide();
				$j('#videos').show();

			} else if (tricks_code != "true") {

				$j('#code').show();
				$j('#videos').hide();

			};

		}
		
	}
