/**
 * 
 * Copyright (c) 2008 xantus s.a. All rights reserved.
 * @author ksykula
 *
 * 
 */

var HotArticlesPreviewPanel = {

	HOT_ARTICLES_PREVIEW_PANEL_HOLDER_ID :"#hot_articles_preview_panel_holder",

	HOT_ARTICLES_PREVIEW_PANEL_ID :"#hot_articles_preview_panel",

	HOT_ARTICLE_PREVIEW_HOLDER_ID :"#hot_article_preview_holder",

	BIG_IMAGE_HOLDER_ID :"#big_image_holder",
	
	BIG_IMAGES_ID :"#big_images",
	
	HOT_ARTICLES_PREV_ID :"#hot_articles_previews",

	SMALL_IMAGE_HOLDER_ID_PRE :"#small_image_holder",

	CSS_SELECTED :"selected",
	
	CSS_ARROW :"arrow",

	articles :null,

	selectedIndex :null,
	
	imageWidth : 0,
	
	displayTime : 10000,
	
	duration : 1000,
	
	skipIteration : false,
	
	

	__highlightSmallImage : function(index) {
		imgH = $(this.SMALL_IMAGE_HOLDER_ID_PRE + index,
				this.HOT_ARTICLES_PREVIEW_PANEL_ID);
		imgH.addClass(this.CSS_SELECTED);
		imgH.next().addClass(this.CSS_ARROW);
	},

	__setPreview : function(index) {
		
		bigImages = this.__getBigImages();
		hotArtPrevs = this.__getHotArtsPrevs();		
		
		bigImages.stop( true, true);
		hotArtPrevs.stop( true, true);
		
		this.__slide([bigImages,hotArtPrevs], index );		
		 	
	},
	
	__getBigImages : function()
	{
		return $(this.BIG_IMAGES_ID,
				this.HOT_ARTICLES_PREVIEW_PANEL_HOLDER_ID);
	},
	
	__getHotArtsPrevs : function()
	{
		return $(this.HOT_ARTICLES_PREV_ID,
				this.HOT_ARTICLES_PREVIEW_PANEL_HOLDER_ID);	
	},
	
	__slide : function( prevElements, previewIndex )
	{
		var left = -previewIndex * this.imageWidth;
		left = left + 'px';
		for( i=0; i<prevElements.length; i++ )
		{
			prevElements[i].animate({ 
		        left: left 		        
	      	}, 
	      	{ duration:HotArticlesPreviewPanel.duration, easing : 'swing' } );
		}		
	},
	
	__insertIntoDOM : function( articles )
	{
		bigImages = this.__getBigImages();
		hotArtPrevs = this.__getHotArtsPrevs();	
		
		bigImages.empty();
		hotArtPrevs.empty();
		
		bigImages.append(this.articles[0].img);
		hotArtPrevs.append(this.articles[0].preview);
		this.imageWidth = bigImages.children().get(0).width;
		bigImages.css('width',this.articles.length * this.imageWidth);
		hotArtPrevs.css('width',this.articles.length * this.imageWidth);
		
		for( i=1; i<this.articles.length; i++ )
		{		
			bigImages.append(this.articles[i].img);
			hotArtPrevs.append(this.articles[i].preview);			
		}
	},

	init : function( articles, selectedIndex, options ) 
	{
		this.articles = articles;
		this.__insertIntoDOM(articles);
		this.select(selectedIndex);
		if( options )
		{
			if( options.duration )
			{
				this.duration = options.duration;
			}
			if( options.displayTime )
			{
				this.displayTime = options.displayTime;
			}
		}
		var interval = this.duration + this.displayTime;
		$("#hot_articles_preview_panel_holder").everyTime( interval ,
				function() {
			
					if( !HotArticlesPreviewPanel.skipIteration )
					{
						var nextIndex = HotArticlesPreviewPanel.selectedIndex + 1;
			
						if( nextIndex >= HotArticlesPreviewPanel.articles.length )
						{
							nextIndex = 0;
						}
		
						HotArticlesPreviewPanel.select( nextIndex );
					}
					HotArticlesPreviewPanel.skipIteration = false;
				});
	},
	
	select : function(index) {
		HotArticlesPreviewPanel.skipIteration = true;
		if (this.selectedIndex != null) {
			imgH = $(this.SMALL_IMAGE_HOLDER_ID_PRE + this.selectedIndex,
					this.HOT_ARTICLES_PREVIEW_PANEL_ID);
			imgH.removeClass(this.CSS_SELECTED);
			imgH.next().removeClass(this.CSS_ARROW);
		}
		this.selectedIndex = index;
		this.__highlightSmallImage(index);
		this.__setPreview(index);
		
	}
	
	
}

