/**
 * showcase class
 *
 * [2009-10-01 js] created
 *
 * $Rev: 480 $ Revision of last commit
 * $Author: jan $ Author of last commit
 * $Date: 2008-09-26 08:30:09 +0200 (Fr, 26 Sep 2008) $ Date of last commit
 */
	
var Showcase = (function() {
		
	var 
		// rotation
		active_show_navi = 0,
		
		// jwplayer id, filled when player is rdy
		mplayer_id = false,
		
		// the player itself
	  mplayer = false,
	  
	  // showcase content
	  showcaseContent = {},
	  // showcase items
	  showcaseItems = {},
	  sessionid = 0,
	  
	  // looping status from showcase
	  loopStatus = false;

	  
  function showcase()
  {
  	var media_file = "", pic_file = "";    	
    
    // find out what kind of content we have to display:
    if(showcaseContent[active_show_navi].media) {
      // search for "flv" file extension:
      if(showcaseContent[active_show_navi].media.search(/flv$/i) > -1)
        media_file = showcaseContent[active_show_navi].media;
      // search for jpg/gif/png-extension:
      else if(showcaseContent[active_show_navi].media.search(/(jpg|gif|png)$/i) > -1)
        pic_file = showcaseContent[active_show_navi].media;
    }
		
    if(showcaseContent[active_show_navi].preview && !pic_file) {
      pic_file = showcaseContent[active_show_navi].preview;
    }
    
    if (!mplayer) {    	
    	// if player is not loaded, load it ( normally no need for it )
    	this.setPlayer($("#" + mplayer_id).get(0));
    }
    
    //alert(mplayer);
    
    // showcase entry with media content
    if (media_file) {

      $("#vt_home_pic").hide();
      //$("#vt_home_flash").show();
      	      	
      // create object with the video/image file
      var obj = pic_file ? 
               { file: path.editorial_video + media_file, image: path.editorial_img + pic_file } : 
               { file: path.editorial_video + media_file };
               
      // evade unexpected runtime errors
			try {               
 				// load files into player
      	mplayer.sendEvent("LOAD",obj);
			} catch(err) {
				// do nothing if an error might occurs
			}
    }
    // showcase entry with just a picture
    else if (pic_file) {    	
    	
    	// evade unexpected runtime errors
    	try {
    		// stop the player if its running
   			//mplayer.sendEvent('STOP');
    	} catch(err) {
    		// do nothing if an error might occurs
    	}

    	//$("#vt_home_flash").hide();
      $("#vt_home_pic").show().html('<div style="position: absolute; z-index: 99; top: -274px; width: 338px; height: 275px; background: #333;"><img src="' + path.editorial_img + pic_file + '" width="320" height="235" style="margin-top: 10px; margin-left: 7px;" /></div>');      
    }
    
    // set text
    $("#vt_text").html(showcaseContent[active_show_navi].text);
    // set link
    $("#vt_link").attr("href", showcaseContent[active_show_navi].link + "?i=" + sessionid);

		// delete previous selection  
    this.setItem.call(showcaseItems[ !active_show_navi ? (showcaseContent.length - 1) : (active_show_navi - 1)], "passive");	
    
    // set status for active element:
    this.setItem.call(showcaseItems[active_show_navi], "active");	    
   
    // rotates the selection    
    active_show_navi++;
  }
  
	return {
		
		init: function(content) {
			
			// set up the content
			showcaseContent = content;
			
			// get the items of the showcase
			showcaseItems = $(".player-list .item");
		},
		beginLoop: function() {
			
			active_show_navi = ( (active_show_navi) % showcaseContent.length);		
			showcase.apply(this);			
		},
	 	startLoop: function(time) {
	 	
	 		// initiate looping
			loopStatus = setInterval(function() { 
				
				Showcase.beginLoop(); 
			}, time);
	 	},		
	 	stopLoop: function() {
	 		
	 		clearInterval(loopStatus);
	 	},
	 	getCurrentItemId: function() {
	 		
	 		return "playlist-item" + active_show_navi;
	 	},
	 	setCurrentItem: function(index) {

	 		// delete previous selection ( -1 because of the last incrementation )
	 		this.setItem.call(showcaseItems[active_show_navi - 1], "passive");		 		
	 		
	 		// set new index
	 		active_show_navi = index;	 	 		
	 		
	 		// show the current item
	 		showcase.apply(this);
	 	},
	 	setPlayer: function(player) {
	 		
	 		mplayer = player;
	 	},
	 	setPlayerId: function(player_id) {
	 		
	 		mplayer_id = player_id;
	 	},
		setSid: function(sid) {
			sessionid = sid
		},
	 	setItem: function(status) {
	 		
			// button attributes
			var button = $(this).children(".price").children(".button");		
			
			// set item active
			if ( status == "active" )	{
				
				$(this).addClass("active");	
				button.removeClass("btn-grey-small").addClass("btn-orange-small");
				
			} else if ( status == "passive" ) {
				
				$(this).removeClass("active");
				button.removeClass("btn-orange-small").addClass("btn-grey-small");
			}		 		
	 	}	 	
	}	
})();