/**
 * @author Andrew Makarenko
 */
var showcaser;
$(document).ready(function(){
	showcaser= new Showcaser();
	showcaser.init();
});

function Showcaser(){
	var self = this;
	var images_class='showcase_img';
	var text_class='showcase_text';
	var link_class='showcase';
	var current_img=1;
	Showcaser.prototype.init = function(){
		self.hideImgsInTags();
		self.bindAction();
		self.loadTab(self.getAction());
		self.SelectCurrent();
	};
	Showcaser.prototype.hideImgsInTags= function(){
		
		$("."+images_class).each(function(){
			i=1;
			$(this).children().each(function(){
				if(i!=1)$(this).hide();
				i++;
			});
		});
		
	}
	Showcaser.prototype.SelectCurrent = function(){
		$("."+link_class).each(function(){
		t='#'+self.getAction();
			if(t==$(this).attr("href")) $(this).parent().parent().addClass("active");
		}
		);
	}
	Showcaser.prototype.getAction = function(){
		var url = window.location.href.toString();
		var match = url.split('#');
		return (match[1] ? match[1] : '1');
	};
	Showcaser.prototype.bindAction = function(){
		$('.'+link_class).click(function(){
			var url=$(this).attr("href");
			var match = url.split('#');
			$(".menu-item").removeClass("active");
			$(this).parent().parent().addClass("active");
			self.hideTab(self.getAction());
			self.loadTab((match[1] ? match[1] : '1'));
		});
		$('.preview-read-more').click(function(){
			self.loadNext();
		});
	};
	Showcaser.prototype.hideTab = function(tabId){
		$("#t-"+tabId).hide();
		$("#s-"+tabId).children().fadeOut("slow");
		$("#s-"+tabId).fadeOut("slow");
	};
	Showcaser.prototype.loadTab = function(tabId){
		current_img=1;
		$("#t-"+tabId).show();
		$("#s-"+tabId).fadeIn("slow");
		kids=$("#s-"+tabId).children();
		len=kids.length;
		i=1;
		$("#s-"+tabId).children().each(function(){
			if(i==current_img)$(this).fadeIn("slow");
			if(len==1)$('.preview-read-more').hide();
			else $('.preview-read-more').show();
			i++;
		});

	};
	Showcaser.prototype.loadNext = function(){
		t=parseInt(self.getAction());
		kids=$("#s-"+t).children();
		len=kids.length;
		if(len==1)return false;
		$("#s-"+t).children().fadeOut("slow");
		i=1;
		current_img++;
		if(len<current_img) current_img=1;
		$("#s-"+t).children().each(function(){
			if(i==current_img)$(this).fadeIn("slow");
			i++;
		});
		return false;
	}
};
