
var loc = String(document.location);
if (loc.indexOf('www') == -1) {
	var redirect = loc.replace('http://tve', 'http://www.tve');
	document.location = redirect;
}

$(document).ready(function(){
	var $lock = false;
	$("div.new_header_nav .l").hover(function(){
		if (typeof($(this).data("lock")) == "undefined" || $(this).data("lock") == false) {
			var $li = $(this);
			$li.data("lock", true);
			$(this).find("ul").slideDown("fast", function(){
				$li.data("lock", false);
			});
		}
	}, function(){
		$(this).find("ul").slideUp("fast");
	});
	
	var first = true;
	$("#search_input").click(function(){
		if (first) {
			$(this).val("").css({'color': '#111'});
			first = false;
		}
	});
	
	if (screen.width > 1027) {
		$(".search_li").show();
	}
	
	$("#shadow").css({'opacity': 0.5}).click(function(){
		tve.hide_floater();
	});
	
	if ($("#error_message").length) {
		$("#shadow").fadeIn("fast");
		$("#error_message").css({"left": ($(window).width()/2)-200}).fadeIn("fast");
	};
	
	$("select.series_redirect").change(function(){
		location.href = '/series/' + $(this).val();
	});
	
	$("input.int_only").keypress(function(e){
		if (e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)) {
			return false;
		}
	});
	
	$("#switch_box .button").click(function(){
		var rel = $(this).attr("rel");
		$("#switch_box .button").removeClass("selected");
		$(this).addClass("selected");
		$("#switch_box .window").hide();
		$("#switch_box ."+rel).show();
	});
	
	// forums
	if (location.hash) {
		if (location.hash.indexOf("#sonid=")!=-1) {
			var mid = location.hash.split("=");
			$("#son"+mid[1]).attr("class", "subject show highlight1");
		}
	}
	
	$("div.forum_show a.forum_subject").click(function(e){
		e.preventDefault();
	});
	
	// Onload
	init();
});

init = function () {
	tve.set_floater_position();
}

tve = new function() {

	this.open_all = 'visible';

	if (navigator.appVersion.indexOf('MSIE 6') != -1) {
		this.ie6 = true;
	}
	
	/* Resets error div floating position */
	this.set_floater_position = function() {
	}
	
	/* Displays requested floating div */
	this.floater_show = function(divname) {
		if (document.getElementById('floater') &&  document.getElementById('float_'+divname)) {
			var floater = document.getElementById('floater');
			var shadow = document.getElementById('shadow');
			
			// Set floater width
			var floater_width = parseInt(floater.style.width);
			floater.style.left = ((screen.width/2)-(floater_width/2))+"px";
			
			// Show floater
			if (this.ie6) {
				floater.style.top = document.documentElement.scrollTop+50+'px';
				shadow.style.top = document.documentElement.scrollTop+'px';
			}			
			
			$("#floater").fadeIn('fast');
			$("#shadow").fadeIn('fast');
			
			// Show selected div, hide others
			var divs = floater.childNodes;
			for (i in divs) {
				if (divs[i].tagName == 'DIV') {
					if (divs[i].id == 'float_'+divname) {
						divs[i].className = 'visible';
					} else {
						divs[i].className = 'hidden';
					}
				}
			}
		}
	}
	
	/* Hides informative floating div */
	this.hide_floater = function() {
		$("#floater").fadeOut('fast');
		$("#shadow").fadeOut('fast');
	}
	
	this.toggle_nav = function(elm, show) {
		this.banner = document.getElementById("banner");
		if (show == true) {
			elm.className = 'hover';
			if (this.ie6 && this.banner) {
				this.banner.style.visibility = "hidden";
			}
		}
		else {
			elm.className = 'nover';
			if (this.ie6 && this.banner) {
				this.banner.style.visibility = "visible";
			}
		}
	}
	
	/* Hides error floating div */
	this.hide_error = function() {
		$("#error_message").fadeOut('fast');
		$("#shadow").fadeOut('fast');
	}
	
	/* Sets reply form paramaters and then show the reply form */
	this.show_reply_window = function (rrid, title) {
		document.getElementById('reply_rrid').value = rrid;
		document.getElementById('reply_title').value = title;
		
		var floater = document.getElementById('reply_form_div');
		var shadow = document.getElementById('shadow');		
		if (this.ie6) {
			floater.style.top = document.documentElement.scrollTop+20+'px';
			shadow.style.top = document.documentElement.scrollTop+'px';
		}			
		$("#reply_form_div").fadeIn('fast');
		$("#shadow").fadeIn('fast');
		document.getElementById('reply_title').focus();
	}
	
	/* Hides the reply form while restering input fields */
	this.hide_reply_window = function() {
		// Clear all fields while hiding this window
		var form = document.getElementById('reply_form').elements;
		// Hide floaters
		$("#reply_form_div").fadeOut("fast");
		$("#shadow").fadeOut("fast");
		document.getElementById('emots_box').className = 'hidden';
	}

	/* Toggles the specified div between hidden and visible classes */
	this.toggle = function(divid) {
		if (document.getElementById(divid)) {
			var div = document.getElementById(divid);
			if (div.className.indexOf('visible') != -1) {
				var new_class_name = 'hidden';
			}
			else {
				var new_class_name = 'visible';
			}
			div.className = new_class_name;
		}
	}
	
	/* Decrypts email and opens the users mail console.
	 * Emails are encrypted to prevent bot abuse.
	 */
	this.oec = function(e) {
		this.e = e.replace(/,,/g, '@');
		this.e = this.e.replace(/!/g, '.');
		this.e = this.e.replace('24yt425', 'gmail');
		this.e = this.e.replace('mrym75', 'hotmail');
		location.href = 'mailto:' + this.e;
	}
	
	/* Pops up an articles replies window */
	this.open_reply_window = function(storyid) {
		var url = '/replies.php?id='+storyid+'#new';
		var w = window.open(url, 'replies_window_'+storyid, 'width=650, height=450, scrollbars=1, resizable=1');
		w.focus();
	}
	
	this.open_mitmash = function() {
		var url = '/mitmash';
		var w = window.open(url, 'mitmash_window', 'width=650, height=450, scrollbars=1, resizable=1');
		w.focus();
	}
	
	/* Add an emoticon to the reply form content textarea */
	this.add_emot = function(key) {
		var a = document.getElementById('content_div');
		a.value = a.value + key;
		this.toggle('emots_box');
	}
	
	/* Validates User registring and Password changing forms */
	this.validate_form = function(form) {
		for (i in form) {
			var fname = form[i].name;
			var fvalue = form[i].value;
			if (fname == 'email') {
				// Validate email
				if (fvalue.indexOf('@') == -1 || fvalue.indexOf('.') == -1 || fvalue.length < 5 ) {
					alert('כתובת מייל לא תקנית');
					return false;
				}
			}
			else if (fname.indexOf('password1') != -1) {
				// Validate password
				if (fvalue.length < 5) {
					alert('הסיסמה קצרה מדי. נא השתמשו ב5 תווים לפחות');
					return false;
				}
			}
			else if (fname == 'username') {
				// Validate username
				if (fvalue.length < 2) {
					alert('שם המשתמש קצר מדי. יש להשתמש לפחות בשתי אותיות');
					return false;
				}
			}
		}
		return true;
	}
	
	this.clear_mitmash = function(a, id, method) {
		var url = '/'+method+'/'+id;
		$.get(url, "", function(o) {
			if (o == 'true') {
				tve.fadeout(a.parentNode.parentNode);
			}
		});
	}
	
	this.fadeout = function(obj) {
		$(obj).hide("normal");
	}
	
	this.open_all_replies = function () {
		var elements = document.getElementsByTagName('DIV');
		for (var i=0; i<elements.length; i++) {
			if (elements[i].id.indexOf('areply') != -1) {
				elements[i].className = this.open_all;
			}
		}
		
		if (this.open_all == 'visible') {
			this.open_all = 'hidden';
		} else {
			this.open_all = 'visible';
		}
	}
	
	this.remove_mails = function () {
		var post = false;
		var form = document.getElementsByName('mailid[]');
		for (var i=0; i<form.length; i++ ){
			if (form[i].checked) {
				document.getElementById('mailbox_form').submit();
			}
		}
	}
	
	this.select_all_mails = function (e) {
		var form = document.getElementsByName('mailid[]');
	
		if (e.checked) {
			var c = true;
		} else {
			var c = false;
		}
		
		for (var i=0; i<form.length; i++ ){
			form[i].checked = c;
		}	
	}
	
	this.toggle_forum_message = function (a, id) {
		a = a.parentNode;
		if (a.className == 'subject hide') {
			a.className = 'subject show';
		}
		else if (a.className == 'subject show') {
			a.className = 'subject hide';
		}
		return false;
	}
	
	this.archive_channel = function() {
		location.href = document.getElementById('channel').value;
	}
	
	this.series_redirect = function (select) {
		location.href = select.value;
	}
	
	this.reload_quote = function () {
		$("#site_quote").html("<div class='ajaxon'></div>");
		$.get("/ajax/quote.php", {}, function(data){
			$("#site_quote").html(data);
		});
	}
	
}

carousel = function(elm) {
	
	var self = this;
	var $holder = $(elm).find(".holder");
	var iheight = $(elm).find("li").eq(0).height();
	var current = 0, loop_holder;
	var items = $(elm).find("img").length;
	var lock = false;
	
	$(elm).find("li").hover(function(){
		self.stop();
	}, function(){
		self.set_loop();
	});
	
	$(elm).find(".pager a").hover(function(){
		self.stop();
		current = $(this).attr("rel");
		self.move($(this).attr("rel"));
	}, function(){
		self.set_loop();
	});

	this.move = function(to, auto) {
		var to = to * iheight;
		self.set_pager();
		if (!auto || (auto && !lock)) {
			lock = true;
			$holder.animate({top: -to}, "200", "linear", function(){
				lock = false;
			});
		}
	}
	
	this.loop = function() {
		if (current == items) current = 0;
		self.move(current, true);
		self.set_loop();
		current++;
	}
	
	this.set_loop = function() {
		loop_holder = setTimeout("carousela.loop()", 5000);
	}
	
	this.stop = function() {
		clearTimeout(loop_holder);
	}
	
	this.set_pager = function() {
		$(elm).find(".pager a").removeClass("selected").eq(current).addClass("selected");
	}
	
	self.loop();
	
}

