// Image cycles
function slideSwitch() {
    var $active = $('#slideshow IMG.active');

    if ( $active.length === 0 ) {$active = $('#slideshow IMG:last');}

    // use this to pull the images in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#slideshow IMG:first');

    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}

$(function() {
    setInterval( "slideSwitch()", 4000 );
});

// Tweet bits
var Social=
{
    init:
    function()
    {
        $.getJSON("http://api.twitter.com/1/statuses/user_timeline.json?screen_name=mediamakerltd&count=3&include_rts=1&callback=?",
        function(tweetdata)
        {
            var tl=$(".tweets ul");
            tl.find('li').remove();
            $.each(tweetdata,
                function(i,tweet)
                {
                    if(tweet.retweeted_status===undefined)
                    {
                        tl.append('<li>@'+tweet.user.screen_name+': '+Social.urlToLink(tweet.text)+'<div class="clr"></div><span class="posted">'+Social.parseDate(tweet.created_at)+'</span></li>');
                        if(i===0)
                            {tl.addClass("first");}
                    }
                    else
                    {
                        tl.append('<li>RT @'+tweet.retweeted_status.user.screen_name+': '+Social.urlToLink(tweet.retweeted_status.text)+'<div class="clr"></div></div><span class="posted">'+Social.parseDate(tweet.created_at)+'</span></li>');
                        if(i===0)
                            {tl.addClass("first");}
                    }
                });
            });
    },
    
    parseDate:
    function(stamp)
    {		      
        var v = stamp.split(' ');
        var date = new Date(Date.parse(v[1] + " " + v[2] + ", " + v[5] + " " + v[3] + " UTC")).toLocaleString();
        var hour = v[3].substr(0, 2);
        var ampm = hour<12 ? ' AM' : ' PM';
        if (hour>12) {hour-= 12;}
        if (hour===0) {hour = 12;}      
        return date.substr(0, 11)+' • ' + v[3].substr(0, 5) + ampm;
    },
    
    urlToLink:
    function(text)
    {
        var exp=/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;return text.replace(exp,"<a href='$1'>$1</a>");
    }
};

// Mini bubble
$(function () {
  $('.minibubble').each(function () {
    // options
    var distance = 10;
    var time = 250;
    var hideDelay = 500;

    var hideDelayTimer = null;

    // tracker
    var beingShown = false;
    var shown = false;
    
    var trigger = $('.trigger', this);
    var popup = $('.popup', this).css('opacity', 0);

    // set the mouseover and mouseout on both element
    $([trigger.get(0), popup.get(0)]).mouseover(function () {
      // stops the hide event if we move from the trigger to the popup element
      if (hideDelayTimer) {clearTimeout(hideDelayTimer);}

      // don't trigger the animation again if we're being shown, or already visible
      if (beingShown || shown) {
        return;
      } else {
        beingShown = true;

        // reset position of popup box
        popup.css({
          top: -40,
          left: 12,
          display: 'block' // brings the popup back in to view
        })

        // (we're using chaining on the popup) now animate it's opacity and position
        .animate({
          right: '-=' + distance + 'px',
          opacity: 1
        }, time, 'swing', function() {
          // once the animation is complete, set the tracker variables
          beingShown = false;
          shown = true;
        });
      }
    }).mouseout(function () {
      // reset the timer if we get fired again - avoids double animations
      if (hideDelayTimer) {clearTimeout(hideDelayTimer);}
      
      // store the timer so that it can be cleared in the mouseover if required
      hideDelayTimer = setTimeout(function () {
        hideDelayTimer = null;
        popup.animate({
          left: '-=' + distance + 'px',
          opacity: 0
        }, time, 'swing', function () {
          // once the animate is complete, set the tracker variables
          shown = false;
          // hide the popup entirely after the effect (opacity alone doesn't do the job)
          popup.css('display', 'none');
        });
      }, hideDelay);
    });
  });
});

$(function () {
  $('.minibubbleEv').each(function () {
    // options
    var distance = 10;
    var time = 250;
    var hideDelay = 500;

    var hideDelayTimer = null;

    // tracker
    var beingShown = false;
    var shown = false;
    
    var triggerEv = $('.triggerEv', this);
    var popupEv = $('.popupEv', this).css('opacity', 0);

    // set the mouseover and mouseout on both element
    $([triggerEv.get(0), popupEv.get(0)]).mouseover(function () {
      // stops the hide event if we move from the trigger to the popup element
      if (hideDelayTimer) {clearTimeout(hideDelayTimer);}

      // don't trigger the animation again if we're being shown, or already visible
      if (beingShown || shown) {
        return;
      } else {
        beingShown = true;

        // reset position of popup box
        popupEv.css({
          top: -40,
          left: -185,
          display: 'block' // brings the popup back in to view
        })

        // (we're using chaining on the popup) now animate it's opacity and position
        .animate({
          right: '-=' + distance + 'px',
          opacity: 1
        }, time, 'swing', function() {
          // once the animation is complete, set the tracker variables
          beingShown = false;
          shown = true;
        });
      }
    }).mouseout(function () {
      // reset the timer if we get fired again - avoids double animations
      if (hideDelayTimer) {clearTimeout(hideDelayTimer);}
      
      // store the timer so that it can be cleared in the mouseover if required
      hideDelayTimer = setTimeout(function () {
        hideDelayTimer = null;
        popupEv.animate({
          left: '+=' + distance + 'px',
          opacity: 0
        }, time, 'swing', function () {
          // once the animate is complete, set the tracker variables
          shown = false;
          // hide the popup entirely after the effect (opacity alone doesn't do the job)
          popupEv.css('display', 'none');
        });
      }, hideDelay);
    });
  });
});

// STLight Options
//stLight.options(
//{
//    publisher:'20985f05-9be9-4ed5-8530-9de93547ad36'
//});

// Fader
$(document).ready(function () {
	$('#mainnav').children().append('<div class="hover"></div>');
	$('#mainnav').children().hover(
		   function() {         
				  $(this).children('div').stop(true, true).fadeIn('1000');             
		   }, 
		   function() {
				  $(this).children('div').stop(true, true).fadeOut('1000');                  
	}).click (function () {
		   $(this).addClass('selected');     
	});
	$('.contactfooter a.contactbubble').append('<div class="hover"></div>');
	$('.contactfooter a.contactbubble').hover(
		   function() {         
				  $(this).children('div').stop(true, true).fadeIn('1000');             
		   }, 
		   function() {
				  $(this).children('div').stop(true, true).fadeOut('1000');                  
	}).click (function () {
		   $(this).addClass('selected');     
	});

	Social.init();
});



