// JavaScript Document
(function($){

	$.fn.jPopupNews = function(){
		return this.each(function(){
			var opt = 'status=1,toolbar=0,location=0,menubar=0,directories=0,scrollbars=1,width=800,height=600';
			
			$(this).click(function(){
				var w = window.open($(this).attr('href'),'popup-news',opt);
				return false;
			});
		});
	}

	$.jQueryUI = function(el){
		var $el = el || $('body:first');
		
		// News more link action
		$el.find('a[href$="#more"]').click(function(){
			var $div = $(this).parents('li:first').find('div.more');
			if ($div.is(':visible')) $div.hide('slow'); else $div.show('slow');
			return false;
		});
		
		$el.find('a.js-popup-news').jPopupNews();
		
		if (typeof $.fn.jHintInput == 'function'){
			$el.find('.js-hint-input').jHintInput();
		}
	}
	
})(jQuery);

$(document).ready(function(){
	
	var videoPlayerID = 1;

	if (!isDefined('Form')) Form = null;
	if (!isDefined('Admin')) Admin = null;
	if (!isDefined('TV')) TV = null;

	if (Form) Form.load();

	Menu.load();

	// Admin menu
	if (Admin){
		Admin.load();
	}

	Website.load();

	// Tv program data
	if (TV){
		TV.load();
		$.timer(20000,function(timer){
			TV.update();
		});
	}

	$(document).pngFix();
	
	$('div.weather div.tabs').each(function(){
		var $box = $(this);
		$box.find('> a').click(function(){
			$box.find('li').hide();
			var arr = $(this).attr('href').split('#');
			if (arr[1]=='first'){
				$box.find('> a:eq(0)').removeClass('first').addClass('first-a');
				$box.find('> a:eq(1)').removeClass('second-a').addClass('second');
			} else {
				$box.find('> a:eq(0)').removeClass('first-a').addClass('first');
				$box.find('> a:eq(1)').removeClass('second').addClass('second-a');
			}
			$box.find('li.'+arr[1]).show();
			return false;
		});
		
		$box.find('> a:first').click();
	});
	
	$.fn.assignVideoPlayer = function(o){
		var defaults = {
			'mute' : false,
			'autostart' : false
		};
		var options = $.extend(defaults, o || {});
		
		function _getFlashVars(el, o){
			return $.extend(options, {
				'width' : el.width(),
				'height' : el.height(),
				'file' : el.data('container')['file'],
				'image' : el.data('container')['image']
			}, o || {});
		}
		
		return this.each(function(){
			var id = 'video-player-link-'+videoPlayerID, objID = 'video-object-'+videoPlayerID;
			var $this = $(this);
			$this.data('container', {
				'objID' : objID,
				'image' : $this.find('> img').attr('src'),
				'file' : $this.attr('href')
			})
			$this.attr('id',id);
			$this.html('<div class="js-video" id="'+objID+'"></div>');
			videoPlayerID++;
			jwplayer(objID).setup(_getFlashVars($this, {flashplayer : '/swf/player.swf'}));
		});
	}
	
	$('.video-player').each(function(){
		$.playingVideo = null;
		$.playingFirst = true;
		
		if (videoPlayerID == 1){
			if ($(this).parents('.exclusive-video').size() == 0)
				$(this).assignVideoPlayer({'mute' : true, 'autostart' : true});
			else
				$(this).assignVideoPlayer({'mute' : false, 'autostart' : true});
			$.playingVideo = $(this).data('container')['objID'];
		} else
			$(this).assignVideoPlayer();
		
		var player = jwplayer($(this).data('container')['objID']);
		player.onPlay(function(){
			if ($.playingVideo && $.playingVideo != this.id)
				jwplayer($.playingVideo).pause(true);
			$.playingVideo = this.id;
		}).onIdle(function(){
			$.playingVideo = null;
		}).onPause(function(){
			if ($.playingFirst && $.playingVideo == 'video-object-1' && this.getMute()){
				this.setMute(false);
				this.play();
				$.playingFirst = false;
			} else $.playingVideo = null;
		}).onComplete(function(){
			$.playingVideo = null;
		});
	});
	
	$('#exclusive-carousel_1').each(function(){
		var $box = $(this);
		
		$box.find('div.nav a').click(function(){
			$box.find('.video-player').each(function(){
				var player = jwplayer($(this).data('container')['objID']);
				if (player.getState() == 'PLAYING' || player.getState() == 'BUFFERING')
					player.pause(true);
			});
		});
	});
	
	$('.js-carousel').each(function(){
		var $this = $(this);
		var arr = $this.attr('id').split('_'), count = parseInt(arr[1]);
		
		$this.jcarousel({
			scroll:	count
		});
		var Carousel = $this.data('jcarousel');
		
		$this.find('div.nav a[href$="#next"]').click(function(){
			Carousel.next();
			return false;
		});
		$this.find('div.nav a[href$="#prev"]').click(function(){
			Carousel.prev();
			return false;
		});
	});
	
	$('#announces-carousel').each(function(){
		var $this = $(this), $p = $this.parent();
		
		$this.jcarousel({
			scroll : 2,
			wrap : 'circular',
			auto : 4
		});
		var Carousel = $this.data('jcarousel');
		
		$p.find('div.nav a[href$="#next"]').click(function(){
			Carousel.next();
			Carousel.options.auto = 0;
			return false;
		});
		$p.find('div.nav a[href$="#prev"]').click(function(){
			Carousel.prev();
			Carousel.options.auto = 0;
			return false;
		});
	});
	
	$('#video-bottom-carousel, #bottom-news-carousel').each(function(){
		var $this = $(this), $p = $this.parent();
		var current = 1, $span = $p.find('.js-current'), $count = $p.find('.js-count');
		var step = $(this).attr('id') == 'bottom-news-carousel' ? 2 : 3;
		var prev = false, next = true;
		
		function _showCurrent(){
			var top = current*step;
			if (top > parseInt($count.text())) top = $count.text();
			$span.text(((current-1)*step+1)+'-'+top);
		}
		
		$this.jcarousel({
			scroll : 1,
			buttonPrevCallback : function(instance, obj, p){
				prev = p;
			},
			buttonNextCallback : function(instance, obj, p){
				next = p;
			}
		});
		var Carousel = $this.data('jcarousel');
		
		$p.find('div.nav a[href$="#next"]').click(function(){
			Carousel.next();
			if (next && current<3){
				current++;
				_showCurrent();
			}
			return false;
		});
		$p.find('div.nav a[href$="#prev"]').click(function(){
			Carousel.prev();
			if (prev && current>1){
				current--;
				_showCurrent();
			}
			return false;
		});
	});
	
	$('div.videos ul').each(function(){
		if ($.browser.mozilla) $(this).css('top', '44px');
	});
	
	$.jQueryUI();
	
});

