$(document).ready(function(){
	$('#tyco-wrapper img[src$=.png]').fixpng();
	$('#tyco-wrapper label.overlabel').overlabel();
	$('#tyco-wrapper .eq').equalizeCols();
	/*begin carousel*/
	try{
		 var listItems = $("#home-carrousel ul").children();
		 if(listItems.length>0){
			var mycarousel_itemList= new Array(listItems.length);
			for (i=0; i<listItems.length; i++)
			{
				var node = listItems[i];
				var anchorTag= $(node).children();
				var imageTag= $(node).children().children();
				var objItem = {
					href:$(anchorTag).attr('href'),
					url:$(imageTag).attr('src'),
					width:$(imageTag).width()
				};
				mycarousel_itemList[i]=objItem;
			}		
			
			function mycarousel_itemVisibleInCallback(carousel, item, i, state, evt)
			{
				var idx = carousel.index(i, mycarousel_itemList.length);
				if(state=='next' || state=='init'){
					carousel.add(i, mycarousel_getItemHTML(mycarousel_itemList[idx - 1]));
					}
				if(state=='prev')
					{
					carousel.add(i, mycarousel_getItemHTML(mycarousel_itemList[idx - 1]));	
					}
			};			
			function mycarousel_itemVisibleOutCallback(carousel, item, i, state, evt)
			{
				carousel.remove(i);
			};
			function mycarousel_getItemHTML(item)
			{
				return '<a target="_blank" href="'+item.href+'"><img   src="' + item.url + '" alt="" style="height:70px;" /></a>';
			};
			/*
			function mycarousel_initCallback(carousel)
			{
				 carousel.clip.hover(function() {
					  carousel.stopAuto();
				 }, function() {
					  carousel.startAuto();
				 });
				 carousel.startAuto(10);			
			};
			*/
			function mycarousel_initCallback(carousel)
			{
				$('#home-carrousel div.jcarousel-prev, #home-carrousel div.jcarousel-next').
					mousedown(function(){
						var _this = $(this);
						var _css = 'jcarousel-prev';
						if(_this.hasClass('jcarousel-next') ){
							_css = 'jcarousel-next';
						}
						var _t = setInterval('$("#home-carrousel div.'+_css+'").trigger("click");',300);
						_this.data('tevents',_t);
					}).mouseup(function(){
						var _this = $(this);
						var _t = _this.data('tevents');
						clearInterval(_t);
					});
			}
			jQuery.easing['BounceEaseOut'] = function(p, t, b, c, d) {
				if ((t/=d) < (1/2.75)) {
					return c*(7.5625*t*t) + b;
				} else if (t < (2/2.75)) {
					return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
				} else if (t < (2.5/2.75)) {
					return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
				} else {
					return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
				}
			};
			jQuery('#home-carrousel ul').jcarousel({
				wrap: 'circular',
				scroll:1,
				visible:8,/*
				easing: 'BounceEaseOut',*/
				animation: 250,/*
				auto:2,*/
				itemVisibleInCallback: {onBeforeAnimation: mycarousel_itemVisibleInCallback},
				itemVisibleOutCallback: {onAfterAnimation: mycarousel_itemVisibleOutCallback},
				initCallback: mycarousel_initCallback,
				buttonNextEvent: "click",
				buttonPrevEvent: "click"
			});
		 }
	}
	catch(ex){}
	if( $.browser.msie && $.browser.version < 8 ){
		$('#tyco-wrapper #nav-main>ul>li').hover(
			function () {
				var _this = $(this);
				$('table.sub-menu', _this).addClass('show');
				_this.css({'z-index':'2'});
			}, 
			function () {
				var _this = $(this);
				$('table.sub-menu', _this).removeClass('show');
				_this.css({'z-index':'1'});
			}
		);
	}
	/*end carousel*/
});


/* =jQuery Plugins*/
(function($){
	/* = plug-in : overlabel */
	$.fn.overlabel = function(options){
		var opts = $.extend( {},$.fn.overlabel.defaults, options );
		var selection = this.filter('label[for]').map(function(){
				var label = $(this);
				var id = label.attr('for');
				var field = $('#'+id);
				if(!field) return;
				var o = $.meta ? $.extend( {}, opts, label.data() ) : opts;
				label.addClass(o.label_class);
				var hide_label = function(){ label.css(o.hide_css) };
				var show_label = function(){ this.value || label.css(o.show_css) };
				$(field)
					.parent().addClass(o.wrapper_class).end()
					.focus(hide_label).blur(show_label).each(hide_label).each(show_label);
				return this;
		});
		return opts.filter ? selection : selection.end();
	};
	$.fn.overlabel.defaults = {
		label_class: 'overlabel-apply',
		wrapper_class: 'overlabel-wrapper',
		hide_css: { 'display': 'none' },
		show_css: { 'display': 'block' },
		filter: false
	};
	/* =plug-in : fix-png */
	$.fn.fixpng = function(){
		var hack = {
			isOldIE: $.browser.msie && $.browser.version < 7,
			filter: function(src){ return "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod='scale',src='"+src+"');"; }
		};
		return this.each(function(){
			if(hack.isOldIE){
				var $$=$(this);
				if($$.attr('src')){
					var span = document.createElement('span');
					$(span).attr({
						id: $$.attr('id'), className: $$.attr('class')
					});
					$(span).addClass('img-png');
					$(span).css({
						display: 'inline-block', width: $$.width(), height: $$.height(), filter: hack.filter($$.attr('src')), float: $$.attr('align')=='left'?'left':($$.attr('align')=='right'?'right':'none')
					});
					this.outerHTML = span.outerHTML;
				}
			}
		});
	};
	/* =plug-in : equal-heights */
	$.fn.equalizeCols = function(){
		var height = 0,
			 reset = $.browser.msie ? "1%" : "auto";
		return this
			.css("height", reset)
			.each(function() {
				height = Math.max(height, $(this).outerHeight(true));
			})
			.css("height", height)
			.each(function() {
				var h = $(this).outerHeight(true);
				if (h > height) {
					$(this).css("height", height - (h - height));
				};
			});
	};
})(jQuery);