/*	File: home.js		   */

/********************************
  the following routines require
	jquery.js
	jquery.tools.js
	jquery.gfeed.js
*********************************/
(function($) {

	// extend jquery with get rss feed function
	$.fn.getRssFeed = function(options) {
		var defaults = {
			'url' : '',
			'title' : '',
			'content_tag' : '',
			'link_prepend' : '',
			'entry_key' : 0
		};
		var options = $.extend(defaults, options);
		var key = parseInt(options.entry_key);
		var num = key + 1;
		var obj = null;
		return this.each(function()
		{
			obj = $(this);
			$.jGFeed(options.url, function(feeds) {
				// check for errors
				if (!feeds) {
					obj.html('<strong>'+title+'</strong>: Error accessing the feed.');
					return false;
				}
				var entry = feeds.entries[key];
				switch(options.content_tag) {
					case 'title':
						var content = entry.title;
						break;
					case 'content':
						var content = entry.content;
						break;
					default:
						var content = entry.contentSnippet;
						break;
				}
				if (options.title == 'Twitter')
				{
					content = content.replace(/#/, '');
					content = content.replace(/@/, '');
					content = content.replace(/(http:\/\/)(\S*)/, '');
				}
				// var link_tar = (entry.link.indexOf(location.hostname) == -1) ? ' target="_blank"' : '';
				var link_tar = '';
				var link = (entry.link != '') ? ' <a target="_blank" href="'+ options.link_prepend + entry.link +'"'+ link_tar +'>More &gt;</a>' : '';
				obj.html('<strong>'+ options.title +'</strong>: ' + content + link);
			}, num, 'ABQIAAAAipJWvOEUUwc35NRJ4PqS4hQt9LMlI5zAxwbaEAfyNDSO-oaIDBTze5So5wx51Cbt72i_T4frrqz5Cg');
		});
	};

})(jQuery)


// init home page explore values
var peek_boxes = true;
var fade_out_speed = 100;
var fade_in_speed = 500;
var peek_speed = 200;
var box_speed = 600;
var box_h = new Array('120px', '300px', '120px', '160px'); // initial, open, small, peek
var box_w = new Array('318px', '548px', '203px', '418px', '253px'); // initial, open, small, peek, peek_small
var box_top = new Array('0', '-180px', '0', '-40px'); // initial, open, small, peek
var box_left = new Array(); // id, init, open, small (left), small (right), peek, peek_small (left), peek_small (right)
	box_left[0] = new Array('explore_homes', '0', '0', '0', '0', '0', '0', '0');
	box_left[1] = new Array('explore_businesses', '331px', '216px', '216px', '561px', '266px', '431px', '266px');
	box_left[2] = new Array('explore_power_plants', '662px', '432px', '777px', '777px', '532px', '600px', '600px');
var teaser_h = new Array('82px', '122px'); // initial, peek

  $('#main_slideshow .zip .input_text').bind('focus', function(event) {
                        $('#main_slideshow .slideshow').data('scrollable').pause();
            });
            $('#main_slideshow .zip .input_text').bind('blur', function(event) {
                        $('#main_slideshow .slideshow').data('scrollable').play();
            });

function expandBox(obj)
{
	var box = obj.closest('.explore_block');
	switch(box.attr('id')) {
		case 'explore_homes':
			// val: box_left_key, sib_a_key, sib_b_key, sib_a_pos_key, sib_b_pos_key
			var val = new Array(0, 1, 2, 4, 4);
			var sib_a = $('#explore_businesses');
			var sib_b = $('#explore_power_plants');
			break;
		case 'explore_businesses':
			var val = new Array(1, 0, 2, 3, 4);
			var sib_a = $('#explore_homes');
			var sib_b = $('#explore_power_plants');
			break;
		case 'explore_power_plants':
			var val = new Array(2, 0, 1, 3, 3);
			var sib_a = $('#explore_homes');
			var sib_b = $('#explore_businesses');
			break;
		default:
	}

	// animate active box
	box.addClass('open_box');
	box.children('.explore_teaser').fadeOut(fade_out_speed);
	box.animate({
		height: box_h[1],
		width: box_w[1],
		top: box_top[1],
		left: box_left[val[0]][2]
	}, box_speed, function() {
		box.children('.explore_detail').fadeIn(fade_in_speed);
	});

	// animate sibling a
	if (sib_a.hasClass('open_box'))
	{
		sib_a.children('.explore_detail').fadeOut(fade_out_speed, function() {
			sib_a.children('.explore_teaser').fadeIn(fade_in_speed);
			sib_a.removeClass('open_box');
		});
	}
	sib_a.animate({
		height: box_h[2],
		width: box_w[2],
		top: box_top[2],
		left: box_left[val[1]][val[3]]
	}, box_speed);

	// animate sibling b
	if (sib_b.hasClass('open_box'))
	{
		sib_b.children('.explore_detail').fadeOut(fade_out_speed, function() {
			sib_b.children('.explore_teaser').fadeIn(fade_in_speed);
			sib_b.removeClass('open_box');
		});
	}
	sib_b.animate({
		height: box_h[2],
		width: box_w[2],
		top: box_top[2],
		left: box_left[val[2]][val[4]]
	}, box_speed);
}


// document onload routines
$(document).ready(function() {

    $('#ticker_item_prnews').getRssFeed({
            url : 'http://us.sunpowercorp.com/about/press-releases/feed/',
            title : 'Press',
            content_tag : 'title'
        });
    $('#ticker_item_twitter').getRssFeed({
		url : 'http://twitter.com/statuses/user_timeline/135618949.rss',
		title : 'Twitter'
	});
	$('#ticker_item_facebook').getRssFeed({
		url : 'http%3A%2F%2Fwww.facebook.com%2Ffeeds%2Fpage.php%3Fformat%3Datom10%26id%3D117237894968826',
		title : 'Facebook'		
	});
	$('#ticker_item_blog').getRssFeed({
		url : 'http://us.sunpowercorp.com/blogs/feed/',
		title : 'Blog',
        content_tag : 'title'        
	});
	$('#ticker_item_meltnews_0').getRssFeed({
		url : 'http://meltwaternews.com/magenta/xml/html/71/6/rss/164385.rss2.XML',
		title : 'News',
		content_tag : 'title'
	});
	$('#ticker_item_meltnews_1').getRssFeed({
		url : 'http://meltwaternews.com/magenta/xml/html/71/6/rss/164385.rss2.XML',
		title : 'News',
		content_tag : 'title',
		entry_key : 1
	});
	$('#ticker_item_meltnews_2').getRssFeed({
		url : 'http://meltwaternews.com/magenta/xml/html/71/6/rss/164385.rss2.XML',
		title : 'News',
		content_tag : 'title',
		entry_key : 2
	});
	/*$('#ticker_item_prnews').getRssFeed({
		url : 'http%3A%2F%2Fstage2.mediaroom.com%2Fsunpowercorp%2Findex.php%3Fs%3D15556%26pagetemplate%3Drss',
		title : 'Press',
		content_tag : 'title'
	});
	getPressXML();*/

	// init social ticker
	$('.ticker').scrollable({
		size: 1,
		circular: true,
		vertical: true
	}).autoscroll({
		interval: 7000
	});

	// init slideshows
	$('#main_slideshow .slideshow').scrollable({
		size: 1,
		circular: true,
		items: '.slides',
		onSeek: function(event, imgIndex) {
			$('.home_bg_img').fadeOut(200);
			$('.home_bg_img:eq('+imgIndex+')').fadeIn(500);
		}
	}).autoscroll({
		autopause: false,
		interval: 10000
	}).navigator({
		idPrefix: 'nav_item_'
	});

	// init home page explore blocks
	$('.explore_detail .close').click(function() {
		$('.explore_block').each(function(index) {
			var box = $(this);
			if (box.hasClass('open_box'))
			{
				box.children('.explore_detail').fadeOut(fade_out_speed, function() {
					box.children('.explore_teaser').fadeIn(fade_in_speed);
					box.removeClass('open_box');
				});
			}
			box.animate({
				height: box_h[0],
				width: box_w[0],
				top: box_top[0],
				left: box_left[index][1]
			}, box_speed);
		});
		return false;
	});
	$('.explore_teaser').click(function(){
		peek_boxes = false;
		expandBox($(this));
	});
	$('.explore_teaser').hover(function() {
		var box = $(this).closest('.explore_block');
		box.animate({
			height: box_h[3],
			top: box_top[3]
		}, peek_speed);
	}, function() {
		var box = $(this).closest('.explore_block');
		if (!box.hasClass('open_box'))
		{
			box.animate({
				height: box_h[0],
				top: box_top[0]
			}, peek_speed);
		}
	});

    function getPressXML() {
		var target  = $('#ticker_item_prnews');
		var url = '/cs/jsp/ajax_press_xml.jsp';
		var title = 'Press';
		$.ajax( {
		  url: url,
		  cache: false,
		  context: this,
		  dataType: 'xml',
		  success: function( xml ) {
			  	var node = $('NewsItem:first',xml);
				var duid = $('NewsComponent',node).attr('Duid');
				var content = $('NewsComponent NewsLines HeadLine',node).text();
				var morelink = ' <a href="http://us.sunpowercorp.com/about/newsroom/press-releases/?relID='+duid.substring(duid.lastIndexOf("_")+1)+'">More &gt;</a>';

				var pressTicker = '<strong>'+ title +'</strong>: ' + content + morelink;
				target.html(pressTicker);
		  }
		});
	}

	$('#main_slideshow .zip .input_text').bind('focus', function(event) {
            $('#main_slideshow .slideshow').data('scrollable').pause();
    });
    $('#main_slideshow .zip .input_text').bind('blur', function(event) {
            $('#main_slideshow .slideshow').data('scrollable').play();
    });


});

//window onload routines
$(window).load(function(){
	// peek the explore boxes in sequence
	if (peek_boxes)
	{
		$('.explore_block:eq(0)').animate({
			height: box_h[3],
			top: box_top[3]
		}, peek_speed, function(){
			if (peek_boxes)
			{
				$('.explore_block:eq(0)').animate({
					height: box_h[0],
					top: box_top[0]
				}, peek_speed, function(){
					if (peek_boxes)
					{
						$('.explore_block:eq(1)').animate({
							height: box_h[3],
							top: box_top[3]
						}, peek_speed, function(){
							if (peek_boxes)
							{
								$('.explore_block:eq(1)').animate({
									height: box_h[0],
									top: box_top[0]
								}, peek_speed, function(){
									if (peek_boxes)
									{
										$('.explore_block:eq(2)').animate({
											height: box_h[3],
											top: box_top[3]
										}, peek_speed, function(){
											if (peek_boxes)
											{
												$('.explore_block:eq(2)').animate({
													height: box_h[0],
													top: box_top[0]
												}, peek_speed);
											}
										});
									}
								});
							}
						});
					}
				});
			}
		});
	}
});

