/* Revision: $Id: main.js 8903 2010-11-23 13:12:55Z igor $ */

//------------------------------------------
// Ticker 1...

var list, listSize, curentTop, newTop;

function ticker()
{
	list = $("div#header div.ticker ul");

	listSize = $("div#header div.ticker ul li").size();

	//---
	if(listSize!=0){
	curentTop = parseInt(list.css("top"));

	newTop = curentTop - 20;

	list.animate( {top:newTop+"px"}, 750, "linear", function(){

	  // If we've gone past the end of the list...
	  if ( (newTop*-1) >= (20*listSize) )
	  {
		 newTop = 0;
		 list.css("top", "20px");                  // Move back above the list.
		 list.animate( {top:newTop+"px"}, 750 );  // Move to teh first element.
	  }

	  setTimeout("ticker()", 4000);

	});
	}

} // - function ticker()

function trackMediaView(id, type) {
	$.post("/media/", {track:id,media:type}, function(d){}, "json");
}

//------------------------------------------
// jQuery

$(document).ready(function() {

	initialise_swfs();

	var media = new Array();

	//var media_type = null;

	//------------------------------------------
	// E-Newsletter traching...

	$("#newsletter").submit(function() {

		pageTracker._trackPageview('/e-newsletter-signup/');

		return true;

	});

	//------------------------------------------
	// Ticker 2...

	//Only start the ticker on the homepage...
	if ( $("body.home").size() == 1 )
	{
		setTimeout("ticker()", 4000);
	}

	//------------------------------------------

	$(".tweet p").css("display","none");
	$(".tweet").tweet({
		username: "meltdownfest", // meltdownfest
		count: 3,
		loading_text: "loading tweets..."
	});

	//------------------------------------------

	$(".show-popup2").click(function () {

		var li = $(this).parent();
		clicked(li);

		return false;

	});

	$(".show-popup").click(function () {

		var li = $(this).parent().parent().parent();
		clicked(li);

		return false;

	});


	function clicked( li ){

		//Remove the current image/object element from the DOM
		$("#popover-image img").remove();

		//Remove any flash stuff...
		$("#mediaspace embed").remove();
		$("#mediaspace object").remove();

		//---

		var url = li.children("a").find( "span.url" ).text();
		var title = li.children("a").find( "span.title" ).text();
		var copy = li.children("a").find( "span.copy-full" ).text();
		copy += "<div id=\"mediaTags\">Tags:" + li.next().html() + "</div>";
		var preview = li.children("a").find( "span.preview" ).text();

		$("#popover-title").text(title);
		$("#popover-copy").html(copy);

		if ( li.hasClass("image") ){
			showImage(url);
		} else if ( li.hasClass("video") ){
			showVideo(url, preview);
		} else if ( li.hasClass("audio") ){
			showVideo(url, preview); // <----------------- Use showVideo() as the functions turned out identical.
		}

	} // - function clicked( li )


	function showVideo( url, preview ){

		var pageScroll =  parseInt( $( document ).scrollTop() );

		var width = 470;
		var height = 320;

		//---

		var so = new SWFObject('/flash/player-licensed.swf','ply','470','320','9','#ffffff');
		so.addParam('allowfullscreen','true');
		so.addParam('allowscriptaccess','always');
		so.addParam('wmode','opaque');
		//so.addVariable('skin','/flash/stylish_slim.swf');
		so.addVariable('skin','/flash/skin-modieus.swf');
		so.addVariable('file', url);
		so.addVariable('image', preview);
		so.addVariable('fullscreen', 'true');
		//so.addVariable('usefullscreen', 'true');
		//so.addVariable('showfsbutton','true');
		so.write('mediaspace');

		//---

		$("#popover-flash").show();

		//---

		// Display the relevant div's.
		$("body").css("overflow", "hidden");

		$("#popover").height((height+66)).width(width);

		$("#popover").css("margin-left", ((width/2)*-1)+"px");
		$("#cover").css("top", pageScroll).show();

		$("#popover").css("top", (pageScroll+100) ).show();


	} // - function showVideo

	function showImage( url ){

		var image = new Image();

		$(image).load(function () {

			var pageScroll =  parseInt( $( document ).scrollTop() );

			var width = $(image).attr("width");
			var height = $(image).attr("height");

			//---

			//Add the new image into the DOM.
			$(image).appendTo("#popover-image");

			//---

			// Display the relevant div's.
			$("body").css("overflow", "hidden");

			$("#popover").height((height+66)).width(width);

			$("#popover").css("margin-left", ((width/2)*-1)+"px");
			$("#cover").css("top", pageScroll).show();

			$("#popover").css("top", (pageScroll+55) ).show();

		}).attr('src', url);

	} // - function showImage( url )

	//-----------------------------------------

   $("#popover-close").click(function () {

      $("#cover").hide();
      $("#popover").hide();

      $("body").css("overflow", "auto");

      return false;

   });

   //-----------------------------------------

   /*
   $(".youtube").click(function () {

      media.length = 0;

      populateMediaArray("event-video","youtube");

      var url = $(this).attr("href");

      media_type = 'youtube';

      showimage( url );

      return false;

   });

   //---

   $(".flickr").click(function () {

      media.length = 0;

      populateMediaArray("event-photos","flickr");

      var url = $(this).attr("href");

      media_type = 'flickr';

      showimage( url );

      return false;

   });
   */

   //------------------------------------------

   $("#popover-next").click(function () {

      var desiredId = $("#popover-next span").text();

      $("#popover").fadeOut('fast', function(){
         showimage( media[desiredId][0] );
      });

      return false;

   });

   $("#popover-prev").click(function () {

      var desiredId = $("#popover-prev span").text();

      $("#popover").fadeOut('fast', function(){
         showimage( media[desiredId][0] );
      });

      return false;

   });

   //---
   /*
   *  Displays the media to the div...
   */
   /*
   function showimage ( url )
   {

      //---

      var pageScroll =  parseInt( $( document ).scrollTop() );

      var img = $("#popover img");

      //---
      // Find the current link in the array...

      var currentIndex = 0; // Ends up holding the current link.

      for (var i = 0; i < media.length; i++)
      {
         if (url == media[i][0]){ currentIndex = i; break; }
      }

      //---
      // Set the previous & next links...

      //previous
      $("#popover-prev span").text(currentIndex-1);
      if ( currentIndex == 0 ){ $("#popover-prev span").text(media.length-1);  }

      //next
      $("#popover-next span").text(currentIndex+1);
      if ( currentIndex+1 == media.length ){ $("#popover-next span").text("0"); }

      //---

      $("#popover-title").text(media[currentIndex][1]);

      //---

      if ( media_type == 'youtube' )
      {

         var a_youtube_path = media[currentIndex][2];

         //---

         //Stand youtube sizes...
         var width = 425;
         var height = 344;

         //---

         if($.browser.msie===true)
         {
            var o_youtube = document.createElement("embed");
            $(o_youtube).attr("src",a_youtube_path).attr("width",width).attr("height",height).attr("allowfullscreen","true").attr("type","application/x-shockwave-flash");
         }
         else
         {
            var o_youtube = document.createElement("object");
            var o_parm1 = document.createElement("param");
            var o_parm2 = document.createElement("param");
            var o_embed = document.createElement("embed");

            $(o_youtube).attr("width",width).attr("height",height);

            $(o_parm1).attr("name","movie").attr("value",a_youtube_path);
            $(o_parm2).attr("name","allowFullScreen").attr("value","true");
            $(o_embed).attr("src",a_youtube_path).attr("width",width).attr("height",height).attr("allowfullscreen","true").attr("type","application/x-shockwave-flash");

            $(o_youtube).append(o_parm1).append(o_parm2).append(o_embed);

         } // - if($.browser.msie===true)

         //Remove the current image/object element from the DOM.
         $("#popover-image img").remove();
         $("#popover-image embed").remove();
         $("#popover-image object").remove();

         $(o_youtube).appendTo("#popover-image");

         $("#popover-image *").show();

         //---

         $("ul#join object").css("visibility", "hidden");
         $("ul#join embed").css("visibility", "hidden");

         $("#popover").height((height+66)).width(width);

         $("#popover").css("margin-left", ((width/2)*-1)+"px");
         $("#cover").css("top", pageScroll).show();
         $("#popover").css("top", (pageScroll+((height)/2)) ).show();

         $("html").css("overflow", "hidden");

          //---

      } // - if ( media_type = 'youtube' )

      //---

      if ( media_type == 'flickr' )
      {

         var image = new Image();

         $(image).load(function () {

            var width = $(image).attr("width");
            var height = $(image).attr("height");

            //---

            //Remove the current image/object element from the DOM
            $("#popover-image img").remove();
            $("#popover-image embed").remove();
            $("#popover-image object").remove();

            //Add the new image into the DOM.
            $(image).appendTo("#popover-image");

            //---

            $("ul#join object").css("visibility", "hidden");
            $("ul#join embed").css("visibility", "hidden");

            // Display the relevant div's.

            $("#popover").height((height+66)).width(width);

            $("#popover").css("margin-left", ((width/2)*-1)+"px");
            $("#cover").css("top", pageScroll).show();

            $("#popover").css("top", (pageScroll+100) ).show();

            $("html").css("overflow", "hidden");

            //---

         }).attr('src', url); // <---- Set the image source.

      } // - if ( media_type = 'flickr' )

      //---

      return false;

   } // - function showimage ( url )
   */

	//-----------------------------------------

	function populateMediaArray ( s_id, s_class )
	{

		var lastIndex = 0;

		$( "div#"+s_id+" a."+s_class ).each(function( intIndex ){

			media[intIndex] = new Array();

			/*
            media[i][0] - The href
            media[i][1] - The Title
            media[i][2] - Youtube embed path.
         */

			media[intIndex][0] = $( this ).attr("href");    // Link

			//---

			media[intIndex][1] = $( this ).children("img").attr("alt");

			//---

			media[intIndex][2] = $( this ).children("span").text();

		});



	} // - function populateMediaArray

   //-----------------------------------------

	$("#bcname").focus( function() {

		if (this.value == $("#bconame").attr("value"))
			this.value = '';

	});
	$("#bcname").blur( function() {

		if (this.value == '')
			this.value = $("#bconame").attr("value");

	});

	$("#bcemail").focus( function() {

		if (this.value == $("#bcoemail").attr("value"))
			this.value = '';

	});
	$("#bcemail").blur( function() {

		if (this.value == '')
			this.value = $("#bcoemail").attr("value");

	});

	$("#bccomment").focus( function() {

		if (this.value == $("#bcocomment").attr("value"))
			this.value = '';

	});
	$("#bccomment").blur( function() {

		if (this.value == '')
			this.value = $("#bcocomment").attr("value");

	});


	$(".frmClear").focus(function() {
		var el = $(this);
		if(el.val() == el.attr("defaultValue"))
			el.val("");
	});
	$(".frmClear").blur(function() {
		var el = $(this);
		if(el.val().length < 1)
			el.val(el.attr("defaultValue"));
	});

	$("#enewsletter").submit(function(e) {
		e.preventDefault();
		$("#cm-name2").val($("#cm-name1").val());
		$("#cm-email2").val($("#cm-email1").val());
		$("#newslettermodal").modal();
		return false;
	});
	$("#newslettermodal form").submit(function() {
		if($("#cm-name2").val() == "name")
			$("#cm-name2").val("");
		if($("#cm-email2").val() == "email address")
			$("#cm-email2").val("");
		if($("#cm-address2").val() == "house number")
			$("#cm-address2").val("");
		if($("#cm-street2").val() == "street name")
			$("#cm-street2").val("");
		if($("#cm-postcode2").val() == "postcode")
			$("#cm-postcode2").val("");

		if($("#cm-agreeterms2:checked").size() < 1) {
			alert("You need to agree to the terms & conditions");
			return false;
		}
		
		pageTracker._trackPageview('/e-newsletter-signup-2/');
		return true;
	});


}); // - $(document).ready


/* ************************************************************************* */
// function to initialise swfs
function initialise_swfs()
{
	var divs = document.getElementsByTagName('div');
	for (var d=0; d<divs.length; d++)
	{
		if (divs[d].className.search(/embedflash/gi) != -1)
		{
			// start flash player if we can
			var inputs = divs[d].getElementsByTagName('input');
			var location = '';
			var width = '';
			var height = '';
			var version = '';
			var expressinstall = '';
			var flashvars = new Object();
			var params = new Object();
			var attributes = new Object();
			for (var i=0; i<inputs.length; i++)
			{
				switch (inputs[i].name)
				{
					case 'swflocation':
						location = inputs[i].value;
						break;
					case 'width':
						width = inputs[i].value;
						break;
					case 'height':
						height = inputs[i].value;
						break;
					case 'version':
						version = inputs[i].value;
						break;
					case 'expressinstall':
						expressinstall = inputs[i].value;
						break;
					case 'flashvar':
						var parts = inputs[i].value.split('|');
						if (parts.length > 1)
							flashvars[parts.shift()] = parts.join('|');
						break;
					case 'param':
						var parts = inputs[i].value.split('|');
						if (parts.length > 1)
							params[parts.shift()] = parts.join('|');
						break;
					case 'attribute':
						break;
				}
			}
			// do we have all reqd stuff?
			if (divs[d].id != '' && location != '' && width != '' && height != '' && version != '')
			{
				if (swfobject.hasFlashPlayerVersion(version))
					swfobject.embedSWF(location, divs[d].id, width, height, version, expressinstall, flashvars, params, attributes);
				else
				{
					var body = document.getElementsByTagName('body')[0];
					if (body.className.search(/noflash/gi) == -1)
						body.className = body.className + ' noflash';
				}
			}
			else
				alert('SWF: Bad parameter; need div#, swflocation, width, height, version');
		}
	}
} // end FUNCTION


