/**
 * Reload boxes Helper
 */
function reloadHelper(){
	
	/**
	 * Private variable
	 */
	var self = this;
	var objLinks = null;
	var arrayBoxes = [];
	var objPhotos = null;
	var objClose = null;
	var arrayPhotos = [];
	var typeAction = null;
	var boxNumber = 0;
	var intervalHandler = null;
	var timeReloader = 5000;
	/**
	 * Constructor
	 */
	var construct = function(){
		
	}
	
	/**
	 * Set object links
	 *  
	 * @param jQuery object
	 */
	this.setObjLinks = function( object ){
		if( object.length != 0 ) {
			objLinks = object;
		}
	}
	
	/**
	 * Set array boxes to reload
	 * 
	 * @param array array
	 */
	this.setArrayBoxes = function( array ){
		if( array.length != 0) {
			arrayBoxes = array;
		}
	}
	
	/**
	 * Delete active links
	 */
	var unActiveLinks = function(){
		objLinks.find("a").each(function(i){
			$(this).removeClass("active");
		})
	}

	/**
	 * Active one link
	 */
	var activeOneLink = function(k){
		if( k != null ){
			objLinks.find("a").each(function(i){
				if( k == i )
					$(this).addClass("active");
			})
		}
	}
	
	/**
	 * Hide boxes 
	 */
	var hideBoxes = function( action ){
		if( action == null ) action = '';
		for ( box in arrayBoxes ) {
			if(action=='slow')
				arrayBoxes[box].hide('slow');
			else
				arrayBoxes[box].hide();
		}
	}
		
	/**
	 * Set object photos
	 * 
	 * @param jQuery object
	 */
	this.setObjPhotos = function( object ){
		if( object.length != 0 ){
			objPhotos = object;
		}
	}
	
	/**
	 * Set array photos
	 * 
	 * @param array array
	 */
	this.setArrayPhotos = function( array ){
		if( array.length != 0) {
			arrayPhotos = array;
		}		
	}
	
	/**
	 * Replace photos
	 * 
	 * @param jQuery place 
	 * @param string photo 
	 */
	var replacePhoto = function( place, photo ){
		place.css("background","url("+photo+")");
	}
	
	/**
	 * showBox
	 */
	var showBox = function(i){
		unActiveLinks();
		activeOneLink(i);
		hideBoxes();
		arrayBoxes[i].show();
		if( objPhotos != null ){
			replacePhoto( objPhotos, arrayPhotos[i] );
		}
	}
	
	/**
	 * reloadBoxes
	 */	
	var reloadBoxes = function(){
		boxNumber++;
		if(boxNumber==(arrayBoxes.length)) 
			boxNumber = 0;
		showBox(boxNumber);
	}
	
	/**
	 * autoReloadBoxes
	 */	
	this.autoReloadBoxes = function(time){
		if( time == null ) 
			time = timeReloader;
		intervalHandler=setInterval(reloadBoxes,time);
		arrayBoxes[0].parent().mouseenter(function(){
			clearInterval(intervalHandler);
		}).mouseleave(function(){
			clearInterval(intervalHandler);
			intervalHandler=setInterval(reloadBoxes,time);
		})		
	}
	
	/**
	 * Render 
	 */
	var render = function(){
		showBox(0);
		objLinks.find("a").each(function(i){
			if(typeAction=='mouseenter'){
				$(this).mouseenter(function(){
					showBox(i);
					boxNumber = i;
				})
			}
			else{
				$(this).click(function(){
					showBox(i);
					boxNumber = i;
					openDescriptTV();
				})
			}
		})
	}

	/**
	 * closeDescriptionTV 
	 */
	this.closeDescriptTV = function( object ){
		if( object.length != 0 ){
			objClose = object;
			object.click(function(){
				hideBoxes('slow');
				unActiveLinks();
				$(this).hide();
				$('.menu_small').css('border-bottom','none');
			});
		}
	}
	
	/**
	 * rollDetails
	 */
	this.rollDetails = function( object_button, object_roll ){
		if( object_button.length != 0 && object_roll.length != 0 ){
			object_button.click(function(){
				if(object_button.attr('class').search("active") != -1){
					object_roll.slideDown();
					$(this).removeClass('active');
					$(this).find('span').text('Zwiń szczegóły');
				}
				else{
					object_roll.slideUp();
					$(this).addClass('active');
					$(this).find('span').text('Rozwiń szczegóły');
				}
			});
		}
	}
	
	/**
	 * openDescriptionTV 
	 */
	var openDescriptTV = function(){
		if(objClose != null){
			objClose.show();
			$('.menu_small').css('border-bottom','1px solid #EBEBEB');
		}
	}

	/**
	 * slideUpDown 
	 */
	this.slideUpDown = function(){	
		if(objLinks != null){
			$(".licontent").hide();
			objLinks.click(function(){
				if($(this).attr('class').search("active") != -1){
					$(this).parent().find(".licontent").slideUp();
					$(this).removeClass('active');
				}
				else {
					$(".licontent").slideUp();
					objLinks.removeClass('active');
					$(this).parent().find(".licontent").slideDown();
					$(this).addClass('active');
				}
			});
		}
	}
	
	/**
	 * List 
	 */
	this.openList = function( object ){	
		if( object.length != 0 ) {
			object.find("a.mainLink").click(function(){
				if($(this).parent().attr('class').search("active") != -1){
					$(this).parent().find(".licontent").slideUp('slow');
					$(this).parent().removeClass('active');
				}
				else {
					$(".licontent").slideUp('slow');
					$("a.mainLink").parent().removeClass('active');
					$(this).parent().find(".licontent").slideDown('slow');
					$(this).parent().addClass('active');
				}
			});
		}
	}
	
	/**
	 * Destructor
	 */
	this.destruct = function( action ){
		if( action == null ) action = '';
		if( action.length != 0 ) {
			typeAction = action;
		}
		render();
	}
	
	construct();
}