﻿var optm = false;

$(function(){
	var mnac = false;
	var elm = false;

	$('ul.tabs li a').hover(function(){
		if(!$(this).hasClass('active')){
			$(this).stop().animate({'margin-top':'-3px'}, 150);
			$(this).find('span').stop().animate({'height':'23px'}, 150);
		}
	},function(){
		if(!$(this).hasClass('active')){
			$(this).stop().animate({'margin-top':'0px'}, 150);
			$(this).find('span').stop().animate({'height':'20px'}, 150);
		}
	});

	$('ul.ajaxtabs li a').click(function(){
		var t = $(this);
		var rel = t.attr('rel');
		var url = t.attr('url');
		var loader = t.attr('loader');
		if(!t.hasClass('active')){
			t.parents('ul').find('li a').each(function(){
				if($(this).hasClass('active')){
					$(this).removeClass('active');
					$(this).css({'margin-top':'0px'});
					$(this).find('span').css({'height':'20px'});
				}
			});
			t.addClass('active');
			t.find('span').css({'height':'20px'});
			$(loader).show();
			$(rel).html('');
			$.get(url, function(html){
				$(rel).html(html);
				$(loader).hide();
			});
		}
		return false;
	});

	$('ul.top_nav li a.menu').hover(function(){
		var t = $(this);
		if(mnac && !t.parent('li').hasClass('active') && t.parent('li').find('div.submenu').length){

			var width = (t.parent('li').find('div.submenu').attr('width')) ? t.parent('li').find('div.submenu').attr('width') : t.parent('li').find('div.submenu').outerWidth();
			t.parent('li').find('div.submenu').attr('width', width);

			$('div.submenu div').css({'width':'auto'});
			t.parent('li').find('div.submenu div.subelement').css({'width':''+width+'px'});

			$('ul.top_nav li').removeClass('active');
			$('div.submenu').hide('fast');
			t.parent('li').addClass('active').find('div.submenu').show('fast');
			setTimeout(function(){ $('ul.top_nav li:not(.home,.active) a.menu').css({'margin-top':'0px', 'height':'20px'});}, '150');
		}
		if(t.parent('li').hasClass('active')){
			t.stop().animate({'margin-top':'-3px', 'height':'24px'}, 150);
		} else {
			t.stop().animate({'margin-top':'-3px', 'height':'23px'}, 150);
		}

	},function(){
		var t = $(this);
		if(t.parent('li').hasClass('active')){
			t.stop().animate({'margin-top':'0px', 'height':'21px'}, 150);
		} else {
			t.stop().animate({'margin-top':'0px', 'height':'20px'}, 150);
		}
	});

	$('ul.top_nav li div.submenu div.subelement ul.subelement li a').hover(function(){
		var t = $(this);
		if(t.hasClass('suba') && !t.parent('li').hasClass('active')){ hideallsubmenus(); }
		t.stop().animate({'padding-right':'25px', 'padding-left':'10px'}, 200);
		if(t.parent('li').find('div.submenu2').length) {
			showsubmenu(t);
			elm = true;
		}
		t.parent('li').find('div.submenu2').unbind('mouseenter mouseleave');
	},function() {
		var t = $(this);
		if(!t.parent('li').hasClass('active')) { t.stop().animate({'padding-right':'15px', 'padding-left':'20px'}, 200); }

		t.parent('li').find('div.submenu2').bind('mouseenter', function(){ elm = true; });
		t.parent('li').find('div.submenu2').bind('mouseleave', function(){ setTimeout(function(){ if(!elm){ hidesubmenu(t); } }, '10'); elm = false; });
		setTimeout(function(){
		if(!elm) {
			hidesubmenu(t);
		}
		}, '10');
		elm = false;
	});

	$('ul.top_nav li a.menu').click(function(){
		var t = $(this);
		if(optm){
			$('ul.options li a.default').removeClass('active');
			$('ul.options li ul').hide('fast');
			optm = false;
			$('html').unbind('click');
		}
		if(t.parent('li').find('div.submenu').length) {

			var width = (t.parent('li').find('div.submenu').attr('width')) ? t.parent('li').find('div.submenu').attr('width') : t.parent('li').find('div.submenu').outerWidth();
			t.parent('li').find('div.submenu').attr('width', width);

			if(mnac) {
				t.parent('li').removeClass('active').find('div.submenu').hide('fast');
				mnac = false;
				$('html').unbind('click');
				t.css({'margin-top':'-3px', 'height':'23px'});
				$('div.submenu div.subelement').css({'width':'auto'});
			} else {
				t.parent('li').find('div.submenu div.subelement').css({'width':''+width+'px'});
				t.css({'margin-top':'-3px', 'height':'24px'});
				t.parent('li').addClass('active').find('div.submenu').show('fast');
				mnac = true;
				$('html').click(function(e){
					$('ul.top_nav li:not(.home) a.menu').css({'margin-top':'0px', 'height':'20px'});
					$('ul.top_nav li').removeClass('active');
					$('div.submenu').hide('fast');
					$('div.submenu div.subelement').css({'width':'auto'});
					mnac = false;
				});
			}
			return false;
		}
	});

	$('ul.top_nav li a.menu').dblclick(function(){
		window.location.href = $(this).attr('href');
	});

	$('ul.circle_pagination li a').click(function(){
		var t = $(this);
		var l = parseInt(t.attr('len'));
		var i = (parseInt(t.attr('num'))*l);
		var rel = t.attr('rel');
		var tar = (t.attr('tar')) ? t.attr('tar') : 'ul li';
		t.parents('ul').find('li').removeClass('current');
		t.parent('li').addClass('current');
		$(rel).stop().scrollTo($(rel+' '+tar+':eq('+(i)+')'), 700);
		
	});

	$('a[rel^=video]').live('click', function(){
		var t = $(this);
		var type = t.attr('rel').substring(6);
		var url = t.attr('url');
		var href = t.attr('href');
		var poster = t.attr('poster');

		switch(type) {
			case 'file' : type_html = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0" width="672" height="442">'+
				'<param name="allowfullscreen" value="true" />'+
				'<param name="allowscriptaccess" value="always" />'+
				'<param name="movie" value="http://asosat.tv/play" />'+
				'<param name="quality" value="high" />'+
				'<param name="bgcolor" value="#000000" />'+
				'<param name="flashvars" value=\'file='+url+'&autostart=true&image='+poster+'&skin=http://asosat.tv/player/glow.zip&bufferlength=5\' />'+
				'<embed src="http://asosat.tv/play" bgcolor="#000000" quality="high" width="672" height="442" flashvars=\'file='+url+'&autostart=true&image='+poster+'&skin=http://asosat.tv/player/glow.zip&bufferlength=5\' allowFullScreen="true" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer" />'+
				'</object>';
			break;
			case 'swf' : type_html = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0" width="672" height="442">'+
				'<param name="allowfullscreen" value="true" />'+
				'<param name="allowscriptaccess" value="always" />'+
				'<param name="movie" value="'+url+'" />'+
				'<param name="quality" value="high" />'+
				'<param name="bgcolor" value="#000000" />'+
				'<embed src="'+url+'" bgcolor="#000000" quality="high" width="672" height="442" allowFullScreen="true" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer" />'+
				'</object>';
			break;
			case 'html5' : type_html = '<video dir=ltr width="672" height="442" controls style="background: #000;" preload="auto" autoplay="true" poster="'+poster+'"><source src="'+url+'"></video>';
			break;
		}

		var html = '<div class="videobox_overlay" dir=ltr></div><div class="videobox">'+type_html+'</div>';

		$('body').append(html);
		$('div.videobox_overlay').click(function(){ closeVideoBox(); });
		setVideoBoxPosition();
		$.get(href);
		return false;		
	});

	$('div.selector a.title').click(function(){
		var t = $(this);
		if(t.hasClass('active')) {
			t.removeClass('active');
			t.parent('div').find('ul').hide();
		} else {
			t.addClass('active');
			t.parent('div').find('ul').show();
		}
	});

	$('div.selector ul li a').click(function(){
		var t = $(this);
		var rel = t.attr('rel');
		var tar = t.attr('tar');
		var txt = t.text();
		if(!t.parent('li').hasClass('current')) {
			t.parents('div.selector').find('a.title').text(txt);
			t.parents('ul').find('li').removeClass('current');
			t.parent('li').addClass('current');
			$(tar).hide();
			$(rel).show();
		}
		return false;
	});

var ser_timer = null;

	$('div.searchbox input').keyup(function(e){
		var t = $(this);
		var txt = t.val();
		var a =  t.parent('div').find('a');
		var top = t.parents('div.searchbox').offset().top + 24;
		var left = t.parents('div.searchbox').offset().left;
		var width = t.parents('div.searchbox').outerWidth() -2;
		var task = t.attr('task');

		if(txt.length>0){
			a.show();
		} else {
			a.hide();
		}

		if(e.keyCode=='13'){
			window.location.href = (task) ? 'http://asosat.tv/search?q='+encodeURIComponent(txt)+'&task='+task : 'http://asosat.tv/search?q='+encodeURIComponent(txt);
		}
		else if(txt.length>2){
			clearTimeout(ser_timer);
			ser_timer = setTimeout(function(){ removeResults(); getResults(txt,width,top,left); }, '500');
		} else {
			clearTimeout(ser_timer);
			removeResults();
		}
	});

	$('div.searchbox input').focus(function(){
		var t = $(this);
		var txt = t.val();
		var top = t.parents('div.searchbox').offset().top + 24;
		var left = t.parents('div.searchbox').offset().left;
		var width = t.parents('div.searchbox').outerWidth() -2;
		if(txt.length>2 && $('div.search_results').length=='0'){
			getResults(txt,width,top,left);
		}
	});

	$('form#commentForm').submit(function(){
		var t = $(this);
		var results = t.serialize();
		var cont = t.find('textarea').val();

		if(cont!='لێرە سەرنجەکەت بنووسە.'){
			t.find('button').attr('disabled', 'disabled').css({opacity:'0.6'});
			$('p#comments_loader').show();
			$.post("http://asosat.tv/ajax/comments.php", results, function(html){
				$('p#comments_loader').hide();
				$('div.comments').html(html);
				t.find('button').attr('disabled', null).css({opacity:'1'});
			});
		}
		return false;
	});

	$('div.social_name').css({opacity:'0'});

	$('ul.socials li a').hover(function(){
		var title = $(this).attr('name');
		$(this).stop().animate({'margin-top':'-5px'}, 200);
		$('div.social_name').stop().animate({opacity:'1'}, 200);
		$('div.social_name').text(title);
	}, function(){
		$(this).stop().animate({'margin-top':'0px'}, 200);
		$('div.social_name').stop().animate({opacity:'0'}, 200, function(){
			$('div.social_name').text('');
		});
	});

	$('ul.socials li a').mousedown(function(){
		var task = $(this).attr('task');
		var pid = $(this).attr('pid');
		$.get('http://asosat.tv/ajax/set_share.php', 'id='+pid+'&task='+task);
	});

	$('a#print').click(function(){
		var url = $(this).attr('href');
		var top = (screen.availHeight-450)/2;
		var left = (screen.availWidth-800)/2;
		window.open(url, null, "width=800,height=450,top="+top+",left="+left+",status=no,toolbar=no,menubar=yes,location=no,scrollbars=yes,resizable=yes");
		return false;
	});

});

$(document).ready(function(){

	$('.clearField').clearField();
	$('textarea').elastic();

	$().piroBox({
		my_speed: 400,
		bg_alpha: 0.6,
		slideShow : true,
		slideSpeed : 4,
		close_all : '.piro_close,.piro_overlay'

	});

	$('button').live('mousedown', function(){
		$(this).addClass('active');
	});

	$('button').live('mouseup mouseleave',function(){
		$(this).removeClass('active');
	});

	$('button').live({
	 mouseover: function(){
			$(this).addClass('hover');
	 },
	 mouseout: function(){
			$(this).removeClass('hover');
	 }
	});

	$('ul.options li a.default').click(function(){
		var t = $(this);
		if(t.parent('li').find('ul').length) {
			if(optm){
				t.removeClass('active');
				t.parent('li').find('ul').hide('fast');
				optm = false;
				$('html').unbind('click');
			} else {
				t.addClass('active');
				t.parent('li').find('ul').show('fast');
				optm = true;
				$('html').click(function(e){
					$('ul.options li a.default').removeClass('active');
					$('ul.options li ul').hide('fast');
					optm = false;
				});
			}
			return false;
		}
	});

	$('ul.top_nav li a.menu').each(function(){
		var t = $(this);
		var html = t.html();
		if(t.parent('li').find('div.submenu').length){
			t.html('<em></em>'+html);
		}
	});
	$('a.suba').each(function(){
		var t = $(this);
		var html = t.html();
		if(t.parent('li').find('div.submenu2').length){
			t.html('<em></em>'+html);
		}
	});

	var featureNews = {
		timer: null,
		id: 1,
		start: function() {
			this.timer = setInterval(function(){ featureNews.next(featureNews.id); }, '6000');
		},
		stop: function() {
			clearInterval(this.timer);
		},
		next: function(i) {
			var l = $('ul.news_slider li a').length;
			if(i==l) {
				$('ul.news_slider li a').removeClass('current');
				$('ul.news_slider li a:eq(0)').addClass('current');
				featureNews.id = parseInt(1);
				$('div.news_slider_top').stop().scrollTo($('div.news_slider_top div#news_slider_0'), 800, {easing:'easeInOutBack'});
			} else {
				$('ul.news_slider li a').removeClass('current');
				$('ul.news_slider li a:eq('+(i)+')').addClass('current');
				featureNews.id = parseInt(i+1);
				$('div.news_slider_top').stop().scrollTo($('div.news_slider_top div#news_slider_'+i+''), 800, {easing:'easeInOutBack'});
			}
		}
		

	};

	featureNews.next(0);
	featureNews.start();

	$('div.news_slider_top').hover(function(){
		featureNews.stop();
	}, function(){
		featureNews.start();
	});

	$('ul.news_slider li a').click(function(){
		var i = parseInt($(this).attr('rel'));
		featureNews.stop();
		featureNews.next(i);
		return false;
	});

	$('ul.followers').find('li:last').addClass('last');
	$('div.twitter_feed').find('div.item:last').addClass('last');

	var prc = parseInt($('div.programs ul li.current').index()/5);
	$('ul#programs_navigator li a:eq('+prc+')').click();

});
