/*
 * jQuery web&co rotator
 */
(function($) {
$.widget('ui.icanslider', {

	_init: function() {
		var self = this;
		this.container = $(this.element).find('ul');
		this.wrapper = self.container.parent().prev(); 
		$(this.container).css('top', '0px');
		this.mode = ($(this.element).hasClass('small')) ? 2 : 1 ;
		this.items = this.container.children();
		
		var clone = $.extend(true, {}, this.container.children().clone())
		var list = this.container.children();
		
		var arr1 = $.makeArray(this.container.children());
//		var arr2 = $.makeArray(clone);
//		var arr3 = $.makeArray(clone);
//		var arr4 = $.makeArray(clone);
//		var all = $.merge(arr1,arr2);
//		all = $.merge(all,arr3);
//		all = $.merge(all,arr4);
		
		this.items = arr1;
		
		this.container.empty();
		this.container.append(arr1);
		this.active = false;
		
		var info = $('.info.small .text span');
		var overlay = $('.overlay .span');
		var current = self.container.children(":first-child");
		
		Cufon.replace(info);
		Cufon.replace(overlay);
		
//		if(!$.browser.msie || ($.browser.msie && !$.browser.version.substring(0, 1) != '9')) {
//		}
		$('.rotator ul li').not(':first-child').find('img').not('.ticket img').css({opacity: 0.0});
		$('.rotator ul li').not(':first-child').find('.overlay').css({marginTop: 0});
		$('.rotator ul li:first-child').addClass('active');
	    
		
		self._setLink(current.find('a').attr('href'));
		info.html(current.find('.info').html());
		
		$('.rotator li').each(function(){
			var info = $(this).find('.info');
			//info.hide();
			var overlay = $(this).find('.overlay');
			//overlay.hide();
			overlay.parent().hover(
				function(){
//					if(!overlay.is(':animated')) {
//						overlay.fadeIn('slow');
//						self.active = true;
//					} else {
//						overlay.hide();
//						self.active = false;
//					}
				},
				function(){
//					if(!overlay.is(':animated')) {
//						overlay.fadeOut('slow');
//						self.active = false;
//					} else {
//						overlay.hide();
//						self.active = false;
//					}
				}
			);
		});
		
		setInterval(function(){
			if(!self.active) {
				self._setNext();
			}
		}, 8000)
	},
	
	_setLink : function(link) {
		var self = this;
		self.wrapper.click(function(){
			location.href = link;
		})
		
		
	},
	
	_setNext: function() {
		var self = this;
		var offset = parseInt($(this.items[0]).width());
		var pos = parseInt($(this.container).css('left')) + offset;
		// self.container.prepend(self.container.children(":last-child"));
		var current = self.container.children(":first-child");
		current.show();	
		
		var info = $('.info.small .text span');
		info.html(current.find('.info').html());
		Cufon.replace(info);

		 var active = $(this.container).find('.active');
		 
		 if ( active.length == 0 ) {
		     active = $('#slideshow IMG:last');
		 }
		 
		 var next =  active.next().length ? active.next() : self.container.children(":first-child");
		 Cufon.replace(next.find('overlay span'));
		 self._setLink(next.find('a').attr('href'));  
		 
		 active.addClass('last-active');
		 
//		 if($.browser.msie && $.browser.version.substring(0, 1) == '7') {
//			 var offset = parseInt($(this.items[0]).width());
//			 var pos = parseInt($(this.container).css('left')) + offset;
//			 self.container.prepend(self.container.children(":last-child"));
//			 $(this.container).css('left', offset * -1);
//				$(this.container).animate(
//					{left: 0}, 
//					2000, 
//					function() {});
//
//		 } else {
			 next.addClass('active');
			 if($.browser.msie && $.browser.version.substring(0, 1) == '8') {
				 active.find('.overlay').css({marginTop: '0px'});  
				 next.find('.overlay').css({marginTop: '-108px'});
			 } else {
				 active.find('.overlay').animate({marginTop: '0px'}, { queue:true, duration:600 });  
				 next.find('.overlay').animate({marginTop: '-108px'}, { queue:true, duration:600 });
			 }
			 active.find('.image img').not('.ticket').animate({opacity: 0.0}, { queue: true, duration: 300 });  
			 next.find('.image img').not('.ticket').animate({opacity: 1.0}, { queue: true, duration: 300 });  
			 
			 active.removeClass('active last-active');
			 
//		 }
		 
	}
});

icanslider = $.extend($.ui.icanslider, {
	version: '1.0.0',
	defaults: {
		itemClassTrigger: 'item',
		prevButtonTrigger: '#button1',
		nextButtonTrigger: '#button2'
	}
});

})(jQuery);
