// global es namespace
var esCommon = {
	"timerRegistry" : {
		"list" : new Array(),
		"add" : function (object) {
			object.handler = setInterval(object.callback,object.interval);
			this.list.push(object);
			return this;
		},
		"get" : function (name) {
			for (var i = 0; i < this.list.length; i++)
				if (this.list[i].name == name)
					return i;
			
			return null;
		},
		"remove" : function (name) {
			var index = this.get(name);
			if (index == null)
				return;
			clearInterval(this.list[index].handler);
			this.list.splice(index,1);
		}
	}
};

// add truncate method to string class 
String.prototype.trunc =
    function(n,useWordBoundary){
        var toLong = this.length>n,
            s_ = toLong ? this.substr(0,n-1) : this;
        s_ = useWordBoundary && toLong ? s_.substr(0,s_.lastIndexOf(' ')) : s_;
        return  toLong ? s_ +'...' : s_;
     };

// jQuery document ready
jQuery.noConflict();
jQuery(document).ready(function() {
   
   
	jQuery("div.icons .ttp").hide();
	jQuery("div.icons img")
	   	.mouseenter(function() {
	   		var desc = jQuery(this).next();
	   		if (desc.is(".ttp")) {
	   			desc
	   				.css("top",jQuery(this).position().top - desc.outerHeight())
	   				.fadeIn("slow");
	   		}
	   	})
	   	.mouseleave(function() {
	   		var desc = jQuery(this).next();
	   		if (desc.is(".ttp")) {
	   			desc.fadeOut("fast");
	   		}
	   	});   
   
        
   jQuery("input, textarea").focus(function() {
		if(this.value == this.defaultValue)
	   		this.select();
	});
	
   // object to hold new logic
   var newsHolder = {
		"init" : function () {
			jQuery("div.news-content")
				.hide();
			jQuery("div.news-content p")
				.each(function(index) {
					jQuery(this)
						.replaceWith(
							newsHolder.textToParagraph(
								jQuery(this).text().trunc(300,true) + ""
							)
						);
			   });
			jQuery(".menu_down a.news")
			   .mouseover(function() {
			   		jQuery("div.news-content").slideDown();
			   })
			   .mouseout(function() {
			   		jQuery("div.news-content").slideUp();
			   });
		},
		"textToParagraph" : function (text) {
			var result = '';
		   	var paragraph_list = text.split("\n");
		   	for (var i = 0; i < paragraph_list.length; i++)
		   	{
		   		paragraph = jQuery.trim(paragraph_list[i]);
		   		if (paragraph.length > 0)
		   			result += '<p>' + paragraph + '</p>';
		   	}
		   	return result;
		}   
   };
   
	// object to hold logic for banner rotation
	var bannerHolder = {
	   "list" 		: jQuery(".banner .element"),
	   "current" 	: 0,
	   "init" 		: function() {
		   for (var i = 0; i < this.list.length; i++)
			   jQuery(".banner .control").append("<a href=\"#\">" + (i + 1) + "</a>");
		   jQuery(".banner .control a").click(function (){
			   bannerHolder.set(jQuery(this).text() - 1);
		   });
		   this.registerTimer();
		   this.next();
	   },
	   "next"		: function() {
		   this.list.hide();
		   jQuery(".banner .control a").removeClass("selection");
		   if (this.current == this.list.length)
			   this.current = 0;
		   this.list.eq(this.current).fadeIn();
		   jQuery(".banner .control a").eq(this.current).addClass("selection");
		   this.current++;
	   },
	   "set"		: function(index) {
		   if (index >= this.list.length)
			   return;
		   this.current = index;
		   this.next();
		   this.resetTimer();
	   },
	   "registerTimer" : function () {
			esCommon.timerRegistry
				.add({
					"name"	: "bannerRotator",
					"callback" : function () {
						bannerHolder.next();
					},
					"interval" : 10000
				});
	   },
	   "unregisterTimer" : function () {
		   esCommon.timerRegistry
				.remove("bannerRotator");
	   },
	   "resetTimer" : function () {
		   this.unregisterTimer();
		   this.registerTimer();
	   }
   };
	
	// init banner holder
	bannerHolder.init();
	newsHolder.init();
	
	jwplayer().onPlay(function () {
		bannerHolder.unregisterTimer();
	});
});
