/* created by jdg */
/******************/
jQuery(document).ready(function($) {

	get_panel_livetip();

	$('body.tv2.page-login .login input:first').focus(); // focus-t login formhoz tartozo elso input

	// Header tvguide
	var timecontainer = "#header #guide ul#shows";
	var showscontainer = "#header #guide ul#currentshows";

	$(timecontainer+" li").hover(function() {
		$(this).addClass("active");
		var n = $(timecontainer+" li").index($(this));
		if(n !== 0) {
			$(timecontainer+" li:nth-child(1)").removeClass("current");
			$(showscontainer+" li:nth-child(1)").removeClass("current");
			$(showscontainer+" li:eq("+n+")").show();
		}
	}, function() {
		$(this).removeClass("active");
		var n = $(timecontainer+" li").index($(this));
		if(n !== 0) {
			$(timecontainer+" li:nth-child(1)").addClass("current");
			$(showscontainer+" li:nth-child(1)").addClass("current");
			$(showscontainer+" li:eq("+n+")").hide();
		}
	});
	// /Header tvguide

	// Follow tooltips
	$("#follow a span, .videolist h2 a span").tooltip({
		relative: true,
		offset: [-5, 0],
		position: "top center"
	});
	// /Follow tooltips

	// Menu dropdown
  $("ul#menu li.dropdown").hover(function(){
		$(this).addClass("active");
		$('div:first',this).show();
  }, function(){
		$(this).removeClass("active");
		$('div:first',this).hide();
  });
	// /Menu dropdown

	// Video icons and block hover
  $(".videothumbnail a").prepend("<span class=\"videoicon\"></span>");
	// /Video icons

	// Searchbox
	$("#search input").focus(function() {
		if(this.value == this.defaultValue) this.value = "";
	}).blur(function() {
		if(!this.value.length) this.value = this.defaultValue;
	});
	// /Searchbox
	
	// Date mask
	//$("#d1").mask("9999/99/99");
	//$("#d2").mask("9999/99/99");
	// /Date mask

	// Calendar widget
	if ($('#advancedsearch').length) {
		$(".widget input").each(function() { $(this).attr("readOnly", true); }); // ha van js akkor readonlyk legyenek a fieldek
		var date1 = new Date();
		var date2 = new Date();
		if($("input#d1").val() != "") {
			var date1string = $("input#d1").val();
			var date1array = new Array();
			date1array = date1string.split('/');
			date1.setFullYear(date1array[0],date1array[1]-1,date1array[2]); 
		} 
		if(($("input#date2").val() != "") && ($("input#d1").val() != "")) {
			var date2string = $("input#d2").val();
			var date2array = new Array();
			date2array = date2string.split('/');
			date2.setFullYear(date2array[0],date2array[1]-1,date2array[2]); 
		} else {
			var date1string = $("input#d1").val();
			var date1array = new Array();
			date1array = date1string.split('/');
			date2.setFullYear(date1array[0],date1array[1]-1,date1array[2]);
		}
		$('#widgetcalendar').DatePicker({
			flat: true,
			format: 'Y/m/d',
			date: [new Date(date1), new Date(date2)],
			calendars: 3,
			mode: 'range',
			starts: 1,
			prev: '&larr;',
			next: '&rarr;',
			locale: {
				days: ["vasárnap", "hétfő", "kedd", "szerda", "csütörtök", "péntek", "szombat", "vasárnap"],
				daysShort: ["vas", "hét", "ked", "sze", "csü", "pén", "szo", "vas"],
				daysMin: ["va", "hé", "ke", "sz", "cs", "pé", "sz", "va"],
				months: ["január", "február", "március", "április", "május", "június", "július", "augusztus", "szeptember", "október", "november", "december"],
				monthsShort: ["jan", "feb", "már", "ápr", "máj", "jún", "júl", "aug", "sze", "okt", "nov", "dec"],
				weekMin: ''
			},
			onRender: function(date) {
				var firstvideo = new Date();
				firstvideo.setFullYear(2007,1,11);
				return {
					disabled: (date.valueOf() < firstvideo.valueOf())
				}
			},
			onChange: function(formated) {
				var date = new Array();
				date = String(formated).split(",");
				$('.widget input#d1').val(date[0]);
				if(date[0] != date[1]) { $('.widget input#d2').val(date[1]) } else { $('.widget input#d2').val('') };
			}
		});
		var state = false;
		$('.widget a.pick').click(function() { // toggle
			$('#widgetcalendar').stop().animate({height: state ? 0 : $('#widgetcalendar div.datepicker').get(0).offsetHeight}, 300);
			state = !state;
			return false;
		});
		$('.widget input').click(function() { // show only
			if(!state) {
				$('#widgetcalendar').stop().animate({height: $('#widgetcalendar div.datepicker').get(0).offsetHeight}, 300);	
				state = !state;
			}
			return false;
		});
		$(document.body).not($(self)).click(function() { $('#widgetcalendar').stop().animate({ height: 0}); state = !state; }); // hide only
		$('#widgetCalendar div.datepicker').css('position', 'absolute');
	}
	// z-index fix for ie
	var zIndexNumber = 1000;
	if ($('#advancedsearch').length) {
		$('div').each(function() {
			$(this).css('zIndex', zIndexNumber);
			zIndexNumber -= 10;
		});
	}
	// /Calendar widget
	
	// Distress message
	// /Distress message
	
	// Tags
	$("#tag-filter").val() == "";
	
	var delay = (function() {
	  var timer = 0;
	  return function(callback, ms) {
	    clearTimeout(timer);
	    timer = setTimeout(callback, ms);
	  };
	})();
	
	$("#tag-filter").keyup(function() {
		delay(function() {
			var search = $("#tag-filter").val(), count = 0;
			if(search.length > 0) {
				$("#taglist").show();
				$("#taglist-full").hide();
				$.ajax({ 
					type: "POST", 
					url: "/content/themes/tv2/tagsearch.php", 
					data: "search="+search,
					beforeSend: function() { 
						$('#taglist').html('<img src="/content/themes/tv2/images/wpspin_dark.gif" alt="" />'); 
					},
					success: function(tags) { 
						$("#taglist").empty().show();
						if(tags.length > 0) { 
							$("#taglist").append(tags); 
						} 
					} 
				});
			} else {
				$("#taglist-full").show();
				$("#taglist").hide();
			}
		}, 500 );
	});
	// /Tags

	// TV Guide
	$("#guidepage td.details.more.closed").children(".more").hide();
	$("#guidepage td.details.more").click(function() {
		$(this).children(".more").slideToggle("fast");
	});
	// /TV Guide
	
	// External links
	$("a[rel='external']").click(function() {
		window.open($(this).attr("href"));
		return false;
	});
	// /External links
	
	$("#searchsubmit").click(function() {
		if($("#searchfield").val() == "keress a videók között" || $("#searchfield").val() == "") {
			return false;
		} else {
			return true;
		}
	});

	// Rating 
	$("#rateit, #rateit-disabled").live("mouseover", function() {
		$("#ratetext-normal").hide();
		$("#ratetext-hover").show();
	});
	$("#rateit, #rateit-disabled").live("mouseout", function() {
		$("#ratetext-hover").hide();
		$("#ratetext-normal").show();
	});
	$("#rateit-disabled").live("click", function() { return false; });
	$("#rateit").live("click", function() {
		var post_id = $(this).attr("class");
		if(post_id) {
			$.ajax({
				type: "POST",
				url: "/content/themes/tv2/panel-rating.php",
				data: "mode=add&pid="+post_id,
				beforeSend: function() {
					$("#rating").html('<img src="/content/themes/tv2/images/wpspin_dark.gif" alt="" />');
				},
				success: function(data) { 
					$("#rating").html(data); 
				} 
			});
		}
		return false;
	});
	// /Rating
	
	// Videosearch channel onchange event
	$("select#c").change(function() {
		var category_index = "";
		category_index = $("select#c").val();
		if(category_index != -1) window.location.href = window.location.pathname+'/videok?c='+category_index;
	});
	// /Videosearch channel onchange event

});

// SYNC
function get_panel_loginbox() {
	var username = "";
	if(($.cookie("wp_user_logged_in"))) {
		var username = $.cookie("wp_user_logged_in");
	}
	jQuery.ajax({ 
		url: "/content/themes/tv2/panel-login.php", 
		type: "GET",
		data: (username != "") ? "user="+username : "",
		async: false,
		cache: true,
		success: function(result) {
			document.write(result);
		}
	});
}
function get_panel_commentbox(postid) {
	var username = "";
	if(($.cookie("wp_user_logged_in"))) {
		var username = $.cookie("wp_user_logged_in");
	}
	jQuery.ajax({ 
		url: "/content/themes/tv2/panel-commentbox.php", 
		type: "GET",
		data: (username != "") ? "id="+postid+"&user="+username : "id="+postid,
		async: false,
		cache: true,
		success: function(result) {
			document.write(result);
		}
	});
}
function get_panel_ratingbox(postid) {
	var username = "";
	if(($.cookie("wp_user_logged_in"))) {
		var username = $.cookie("wp_user_logged_in");
	}
	jQuery.ajax({ 
		url: "/content/themes/tv2/panel-rating.php", 
		type: "GET",
		data: (username != "") ? "pid="+postid+"&rateuser="+username : "pid="+postid,
		async: false,
		cache: true,
		success: function(result) {
			document.write(result);
		}
	});
}

// ASYNC
function postview(id) {
	jQuery.ajax({ 
		type: 'GET',
		url: '/content/plugins/wp-postviews/wp-postviews.php', 
		data: 'postviews_id='+id+'', 
		cache: false
	});
}

function get_panel_livetip() {
	jQuery.ajax({ 
		url: "/content/themes/tv2/panel-livetip.php", 
		async: true,
		cache: true,
		success: function(result) {
			$("#livemenu").append(result);
		}
	});
}

function get_panel_distress(slug) {
	jQuery.ajax({ 
		url: "/content/themes/tv2/panel-distress.php", 
		data: "slug="+slug,
		async: false,
		cache: true,
		success: function(result) {
			$("#distress-container").html(result)
			var channelslug = $("#distress").attr("class");
			if(($.cookie(""+channelslug+".distressid")) != channelslug) {
				$("#distress").stop().animate({ height: 'toggle'}, 300);
				$("#distress span#distressclose a").click(function() { 
					$(this).parent().parent().stop().animate({ opacity: 0, height: 'toggle', marginBottom: 0}, 300);
					$.cookie(""+channelslug+".distressid", channelslug, { expires: 1 });
					return false;
				 });
			}
		}
	});
}