jQuery( "document" ).ready( function()
{
	PowaKaddy_home.setup();
});

var PowaKaddy_home = (function PowaKaddy_home()
{
	return {
		current_index : null,
		current_interaction : null,
	
		setup : function setup()
		{
			var scope = this;

			// try to embed the Flash
			if( PowaKaddy.flashEnabled() )
			{
				this.flashSetup();
			}
			// if the homepage Flash fails, build the JS fallback.
			else
			{
				this.flashFallback();
			}
			
			// offer scroller
			this.offerCarousel();
			
			// social network
			this.socialNetworkSetup();
			
			// site features
			this.siteFeatureSetup();
		},
		
		flashSetup : function flashSetup()
		{
			if( jQuery("#hp-movie").length > 0 )
			{
				swfobject.embedSWF(INCLUDES_PATH+"swf/homepage_interaction.swf", "hp-movie", "940", "475", "9.0.0", "expressInstall.swf", {}, {wmode:"transparent"}, {});
			}
		},
		
		flashFallback : function flashFallback()
		{
			this.trolleyFallback();
		},
		
		// trolley carousel JS version
		trolleyFallback : function trolleyFallback(show_item_id)
		{
			var scope = this;
			
			// add event listeners
			jQuery("#hp-movie .navigation a, #hp-movie .trolleys .left a, #hp-movie .trolleys .right a").live("click", function(e)
			{
				e.preventDefault();
				
				var selection_id = this.className.replace("item-", "");
				scope.trolleyPopulate(selection_id);
			});
			
			if(this.data == null)
			{
				jQuery.getJSON( BASE_URL + 'services/interaction/getnonflashfallback', '', function(data)
				{
					scope.data = data;
					scope.trolleyFallback(show_item_id);
				});
			}
			else
			{
				this.trolleyPopulate(show_item_id);
			}
		},
		trolleyPopulate : function trolleyPopulate(show_item_id)
		{
			var current_id = null;
	
			if(show_item_id != null)
			{
				current_id = parseInt(show_item_id);
			}
			else
			{
				current_id = Math.floor(Math.random() * this.data.content.length);
			}
			this.current_index = current_id;

			jQuery("#hp-movie").replaceWith( this.trolleyParseData() );
		},
		trolleyParseData : function trolleyParseData()
		{
			var data = this.data;
			
			var template_html = data.template;
			var current_id = this.current_index;
			var previous_id = next_id = null;

			previous_id = (current_id-1).mod(data.content.length);
			next_id = (current_id+1).mod(data.content.length);
			
			// set up additional properties
			for(i=0, maxi=data.content.length; i<maxi; i++)
			{
				data.content[i].index = i;
			}

			// data shortcuts
			data.content[current_id].previous = data.content[previous_id];
			data.content[current_id].next = data.content[next_id];
			
			var template_vars = [];
			var var_regexp = /\#\#([^#]+)\#\#/g;
			var var_found;
			var var_val;
			
			// replace complex types with more structure - e.g. demo area
			if( data.content[current_id]['demo']['link'] != '' )
			{
				template_html = template_html.replace("##demo##", '<a href="#" class="interactive" onclick="##demo-link##"><img src="##demo-image##" alt="Try it for yourself" /></a>');
			}
			else
			{
				template_html = template_html.replace("##demo##", '');
			}
			
			while( var_found = var_regexp.exec(template_html))
			{
				eval("var_val = data.content[current_id]" + "['" + var_found[1].replace(/-/g, "']['") + "']");
				template_vars.push( [var_found[0], var_val] );
			}
			
			for(i=0, maxi=template_vars.length; i<maxi; i++)
			{
				template_html = template_html.replace(template_vars[i][0], template_vars[i][1]);
			}
			
			return template_html;
		},
		
		offerCarousel : function offerCarousel()
		{
			var offer_content = jQuery('#hp-offers');
			offer_content.css("top", '-'+offer_content.outerHeight()+'px');
			window.setTimeout( function() { offer_content.animate({'top':'0px'}, 850); }, 2000);
			
			var slider = jQuery('#hp-offers .offers').bxSlider({
				pager: true,
				buildPager: function(slideIndex)
				{
					switch (slideIndex){
						case 0:
							return '<a href="#" class="0">1<span></span></a>';
						case 1:
							return '<a href="#" class="1">2<span></span></a>';
						case 2:
							return '<a href="#" class="2">3<span></span></a>';
						default:
							return '<a href="#" class="default">0<span></span></a>';
					}
				},
				pagerSelector: "",
				pagerActiveClass: "active",
				nextImage: INCLUDES_PATH+"images/bg-link-arrow-r-grey.png",
				nextSelector: "#hp-offers .navigation .next",
				prevImage: INCLUDES_PATH+"images/bg-link-arrow-l-grey.png",
				prevSelector: "#hp-offers .navigation .previous",
				randomStart: true,
				auto: false,
				autoHover: true,
				autoDelay: 2000,
				pause: 7000
			});
		},
		
		// social network features
		socialNetworkSetup : function socialNetworkSetup()
		{
			var socialBlocks = jQuery("#feature-social .content");
			var currBlockId = 0;
			
			socialBlocks.each(function() {jQuery(this).css("display", "none")});
			jQuery(socialBlocks[currBlockId]).css("display", "block");
			
			if(socialBlocks.length > 1)
			{
				window.setInterval(function() {
					jQuery(socialBlocks[currBlockId]).fadeOut(300, function(){
						currBlockId = (currBlockId+1) % socialBlocks.length;
						jQuery(socialBlocks[currBlockId]).fadeIn(300);
					});
				}, 7000);
			}
		},
		
		// social network features
		siteFeatureSetup : function siteFeatureSetup()
		{
			var socialBlocks = jQuery("#site-features .site-feature");
			var currBlockId = 0;
			
			socialBlocks.each(function() {jQuery(this).css("display", "none")});
			jQuery(socialBlocks[currBlockId]).css("display", "block");
			
			if(socialBlocks.length > 1)
			{
				window.setInterval(function() {
					jQuery(socialBlocks[currBlockId]).fadeOut(300, function(){
						currBlockId = (currBlockId+1) % socialBlocks.length;
						jQuery(socialBlocks[currBlockId]).fadeIn(300);
					});
				}, 7000);
			}
		}
	};
})();
