/*

	

	--

	@file 		common.js
	@version 	1.0.0b
	@date 		2011-04-12 10:26:19 +0100 (Tue, 12 Apr 2011)
	@author 	Pete Luffman <pete@merchantmarketinggroup.com>
	@client 	Puma Energy

	Copyright (c) 2011 Merchant Marketing Group <http://www.merchantmarketinggroup.com>

*/

// Load IE6 PNG fix
if ($.browser.msie && $.browser.version < 7)
    $.getScript('templates/default/js/DD_belatedPNG_0.0.8a.js', function() { DD_belatedPNG.fix('.box-btm, .rss-icon, .mask, #pagination ul li, .news #slides li div') });

// Replace fonts
Cufon.replace('#section-head, #news-bar h3, #feature-global h3, .box h4, .intro-text, blockquote, .col h2');


$(function()
{	
	if($('body').hasClass('home')) // We're on the homepage
	{
		$.getScript('templates/default/js/jquery.puma-rotator.js', function()
		{ 
			/******
			* Setup slider
			*/
			
			$.ajax({
		        type: "GET",
				url: "feeds/home-slider.aspx",
				dataType: "xml",
				success: loadSlides
			});
			
			function loadSlides(data) 
			{
				var $list = $('<ul/>').attr('id','slides');
								
				$(data).find('item link').each(function(i)
				{	
					var img = new Image();
					var $listItem = $('<li/>').addClass('loading');
					
					$(img).hide().load(function () 
					{
						$(this)
							.appendTo($listItem)
							.fadeIn(function(){ $listItem.removeClass('loading') })
							
					})
					.attr('src', $(this).text())
		
					$list.append($listItem);
				});
		
				$list.appendTo('#slider').rotate(
				{
					timeout:8000,
					speed:1200,
					display:'slide',
					pagination:true,
					easing:'easeOutCirc'
				});
			}	
			
			/******
			* Setup news ticker
			*/
	
			$('#news-bar ul').rotate(
			{
				timeout:3000,
				speed:'slow',
				display:'sequential'
			});
		});
	}
	
	if($('body').hasClass('news')) // We're on the news
	{
		/******
		* Setup items for slider
		*/

		$.getScript('templates/default/js/jquery.scrollTo-1.4.2-min.js', function()
		{ 
			var $slider = $('#slider').empty();
			var $slides = $('<ul/>');
			var $pagination = $('<ul/>');
				
			$.ajax({
		        type: "GET",
				url: "feeds/news-slider.aspx",
				dataType: "xml",
				success: loadSlides
			});
			
			function loadSlides(data) 
			{					
				$('item', data).each(function(i)
				{	
					var img = new Image();
					var $slide = $('<li/>').addClass('loading');
					var $container = $('<div/>');
					var $heading = $('<h5/>');
					var $link = $('<a/>').html($('title', this).text()).attr('href', $('link', this).text());
					var $desc = $('<p/>').html($('description', this).text());
					
					$container
						.append($heading.append($link))
						.append($desc)
						.appendTo($slide);
					
					$(img).hide().load(function () 
					{
						$(this)
							.appendTo($slide)
							.fadeIn(function(){ $slide.removeClass('loading') })
							
					})
					.attr('src', 'images/' + $('author', this).text())
					
					$slides.append($slide);
					
					$('<li/>')
						.html($('<a href="#"/>').text(i+1))	
						.appendTo($pagination);
				});
				
				$slides
					.appendTo($slider)
					.wrap('<div id="slides"/>')
					.width($('li',$slides).outerWidth() * $('li',$slides).length);
					
				$pagination.appendTo($slider).wrap('<div id="pagination"/>');

				$slider.serialScroll({
					target:'#slides',
					items:'li', 
					axis:'x',
					navigation:'#pagination li a',
					duration:900,
					force:true, 
					lazy:true,
					easing:'easeOutCirc',
					interval:7000,
					constant:false,
					onBefore:function(e, elem, $pane, $items, pos)
					{
						$('div', $pane).css({left: -290})
					},
					onAfter:function(elem)
					{
						$('li', $pagination)
							.eq($('li',$slides).index(elem))
							.addClass('active')
							.siblings()
							.removeClass('active');
						 
						$('div', elem).animate({left: 0}, 400, 'easeOutCirc');
					}
				});
			}
		});
	}
	
	
	if($('body').hasClass('standard')) // We're on the news
	{
		$.getScript('templates/default/js/jquery.puma-rotator.js', function()
		{
			var $rotator = $('#rotator');
			var $intro = $('#content p:first');
			
			if($intro.hasClass('intro-text'))
			{
				$intro.after($rotator);
			}
			
			$('ul', $rotator).rotate({
				timeout:5000,
				speed:'slow',
				display:'crossfade'
			});
		});

	}
});


/******
* Easings
*/

$.extend($.easing,
{
	easeOutCirc: function (x, t, b, c, d) { return c * Math.sqrt(1 - (t=t/d-1)*t) + b; }
});


/******
* Helpers
*/


if (typeof Object.create !== 'function') 
{
    Object.create = function (o) {
        function F() {}
        F.prototype = o;
        return new F();
    };
}


