/**
 *	@filename basic.js
 *	@author Fridolin Koch
 * 	@contact frido@nkcmedia.net
 *	@version 0.1
 *	@lastedit 1.10.2009
 **/
//preload some images
//navistart
$.fn.insertAtCaret = function (myValue) {
	return this.each(function(){
			//IE support
			if (document.selection) {
					this.focus();
					sel = document.selection.createRange();
					sel.text = myValue;
					this.focus();
			}
			//MOZILLA / NETSCAPE support
			else if (this.selectionStart || this.selectionStart == '0') {
					var startPos = this.selectionStart;
					var endPos = this.selectionEnd;
					var scrollTop = this.scrollTop;
					this.value = this.value.substring(0, startPos)+ myValue+ this.value.substring(endPos,this.value.length);
					this.focus();
					this.selectionStart = startPos + myValue.length;
					this.selectionEnd = startPos + myValue.length;
					this.scrollTop = scrollTop;
			} else {
					this.value += myValue;
					this.focus();
			}
	});
};
//some images

var autologin_off = new Image();
autologin_off.src = "styles/LiveSAMP2009/imageset/autologin_off.png";
var autologin = new Image();
autologin.src = "styles/LiveSAMP2009/imageset/autologin.png";

$(document).ready(function (){

	//login
	$('#do_login').click(function (){
		$('#header #login form').submit();
	});
	$('#header #login form input[name=password]').keypress(function(e){
		if(e.which == 13)
		{
			$('#header #login form').submit();
		}
	});
	$('#header #autologin_button').click(function ()	{

		var image = $('#header #autologin_button img').attr('alt');
		if(image == "off")
		{
			$('#header #autologin_button img').attr('src',autologin.src);
			$('#header #autologin_button img').attr('alt',"on");
			$('#header #autologin_button img').attr('title',"Die automatische Anmeldung ist aktiviert.");
			$('#header #login form').prepend('<input type="checkbox" name="autologin" checked="checked" id="autologin_hidden_field" />');
		}
		else if(image == "on")
		{
			$('#header #autologin_button img').attr('src',autologin_off.src);
			$('#header #autologin_button img').attr('alt',"off");
			$('#header #autologin_button img').attr('title',"Die automatische Anmeldung ist deaktiviert.");
			$('#header #login form #autologin_hidden_field').remove();
		}

	});
	//input fields
	$("input[type!=image]").focus(function ()	{
		$(this).css("border-color","#F7A700").css("background-color","#f6ca6f");
	});
	$("input[type!=image]").blur(function ()	{
		$(this).css("border-color","#6f9dbe").css("background-color","#FFF");
	});
	$('.start_list tr.forum,.topiclist tr.row').hover(
		function ()	{
			$(this).children('td').css('border-color','#F7A700');
		},
		function ()	{
			$(this).children('td').css('border-color','#6f9dbe');
		}
	);
	$('.post').each(function (){
		if($(this).children('.postprofile').height() >= $(this).children('.pcontent').height())
		{
			$(this).children('.pcontent').css('border','0');
		}
		else if($(this).children('.postprofile').height() <= $(this).children('.pcontent').height())
		{
			$(this).children('.postprofile').css('border','0');
		}
	});
	/* Samp Server panel */
	var panel_open = false;
	//vertical center
	$('#server_panel').css('top', String( ((parseInt($("body").innerHeight())-500) / 2) + getYOffset() ) + "px");

	var scroll_interval = null;

	$(window).scroll(function (event) {
		var top = String( ((parseInt($("body").innerHeight())-500) / 2) + getYOffset() ) + "px"
		 window.clearTimeout(scroll_interval);
		 scroll_interval = window.setTimeout(function (){
		 	$('#server_panel').animate({'top' : top},800);
		 }, 500);

    });
    $("#server_panel #button").click(function (){
    	if(panel_open == false)
    	{
    		$('#server_panel').animate({'left' : "50px"},800).animate({'left' : "0px"},500);
    		$('#server_panel .inner p.load').show();
    		$('#server_panel .inner .list').load("playerlist.inc.php",{"action" : "show" },function ()	{
    			$('#server_panel .inner p.load').hide();
    			$('#server_panel .inner').show();
    		});
    		panel_open = true;
    	}
    	else
    	{
    		$('#server_panel').animate({'left' : "50px"},500).animate({'left' : "-368px"},800);
    		$('#server_panel .inner p.load').show();
    		$('#server_panel .inner').hide();
    		panel_open = false;
		}
    });
 /*   $("#server_panel").mouseover(function ()	{
    	if(panel_open == false)
   	    {
	   	    $('#server_panel').animate({'left' : "0px"},1000);
	   		$('#server_panel .inner p.load').show();
	   		$('#server_panel .inner .list').load("playerlist.inc.php",{"action" : "show" },function ()	{
	   			$('#server_panel .inner p.load').hide();
	   			$('#server_panel .inner').show();
	   		});
	   		panel_open = true;
	   	}
    });
    $("#server_panel").mouseout(function ()	{
    	if(panel_open == true)
   	    {
   	   		$('#server_panel').animate({'left' : "-368px"},1000);
   			$('#server_panel .inner p.load').show();
   			$('#server_panel .inner').hide();
   			panel_open = false;
	   	}
    });*/
    /* Status Stuff */
    $('.edit_my_status').click(function (event)	{
    	var left = event.pageX;
    	var top = event.pageY;
    	$("#status_editor").show();
    	$("#status_editor").css("top",top).css("left",left-19);

    	var userid = parseInt($(this).attr('rel'));
    	$.post("status.inc.php",{'action' : "load" , "param" : userid},function (data)	{
    		$('#status_editor .inner input.status').attr("disabled",false);
    		$('#status_editor .inner input.status').val(data);
    	});

	})
	$('#status_editor .inner .close_link').click(function ()	{
		$('#status_editor').hide();
	});
	$('#status_editor .inner .save_status').click(function ()	{
		var status = $('#status_editor .inner input.status').val();
		var expire =  $('#status_editor .inner select').val();
		$.post("status.inc.php",{'action' : "save" , "status" : status,"expires" : expire },function (data)	{
			if(data.error)
			{
				alert(data.msg);
			}
			else
			{
				window.location.reload();
			}
    	},"json");
	});
	$('.delete_my_status').click(function ()	{
		$.post("status.inc.php",{'action' : "del"},function (data)	{
			if(data.error)
			{
				alert(data.msg);
			}
			else
			{
				alert("Status gelöscht");
				window.location.reload();
			}
    	},"json");
	});

	$('a.like').click(function (){

		var pid = $(this).attr('rel');

		$.post("status.inc.php",{"action" : "like_post" , "param" : pid },function (data)	{



		},'json');

	});
	$('.show_next_youtube').click(function ()	{

		//$(this).parent('div').parent('div').hide();
		$(this).parent('div').parent('div').next().next().show();

		return false;

	});

	$('.youtube_info').each(function ()	{

		var $this = $(this);
		$this.width(520);

		$.get('youtube.inc.php',{'v' : $(this).attr('role')},function (data)	{

			if(data.error)
			{
				var cont = '<p style="color:red;"><b>' + data.error_msg + '</b></p>';
				$this.html(cont);
			}
			else
			{
				var cont = '<p><b><a href="">' + data.title +'</a></b></p>';
				cont += '<p style="font-size:11px;color:#666;margin-bottom:4px;">' + data.info + '</p>';
				cont += '<p style="font-size:11px;margin-top:2px;"><b>Bewertung: ' + data.rating + '<br />Views: '+ data.views +'</b></p>';
				$this.html(cont)
			}

		},'json');


	});
});
function getYOffset() {
    var pageY;
    if(typeof(window.pageYOffset)=='number') {
       pageY=window.pageYOffset;
    }
    else {
       pageY=document.documentElement.scrollTop;
    }
    return pageY;
}
function popup(url, width, height, name)
{
	if (!name)
	{
		name = '_popup';
	}

	window.open(url.replace(/&amp;/g, '&'), name, 'height=' + height + ',resizable=yes,scrollbars=yes, width=' + width);
	return false;
}

/**
* Jump to page
*/
function jumpto()
{
	var page = prompt(jump_page, on_page);

	if (page !== null && !isNaN(page) && page == Math.floor(page) && page > 0)
	{
		if (base_url.indexOf('?') == -1)
		{
			document.location.href = base_url + '?start=' + ((page - 1) * per_page);
		}
		else
		{
			document.location.href = base_url.replace(/&amp;/g, '&') + '&start=' + ((page - 1) * per_page);
		}
	}
}

/**
* Mark/unmark checklist
* id = ID of parent container, name = name prefix, state = state [true/false]
*/
function marklist(id, name, state)
{
	var parent = document.getElementById(id);
	if (!parent)
	{
		eval('parent = document.' + id);
	}

	if (!parent)
	{
		return;
	}

	var rb = parent.getElementsByTagName('input');

	for (var r = 0; r < rb.length; r++)
	{
		if (rb[r].name.substr(0, name.length) == name)
		{
			rb[r].checked = state;
		}
	}
}

/**
* Resize viewable area for attached image or topic review panel (possibly others to come)
* e = element
*/
function viewableArea(e, itself)
{
	if (!e) return;
	if (!itself)
	{
		e = e.parentNode;
	}

	if (!e.vaHeight)
	{
		// Store viewable area height before changing style to auto
		e.vaHeight = e.offsetHeight;
		e.vaMaxHeight = e.style.maxHeight;
		e.style.height = 'auto';
		e.style.maxHeight = 'none';
		e.style.overflow = 'visible';
	}
	else
	{
		// Restore viewable area height to the default
		e.style.height = e.vaHeight + 'px';
		e.style.overflow = 'auto';
		e.style.maxHeight = e.vaMaxHeight;
		e.vaHeight = false;
	}
}

/**
* Set display of page element
* s[-1,0,1] = hide,toggle display,show
*/
function dE(n, s)
{
	var e = document.getElementById(n);

	if (!s)
	{
		s = (e.style.display == '' || e.style.display == 'block') ? -1 : 1;
	}
	e.style.display = (s == 1) ? 'block' : 'none';
}

/**
* Alternate display of subPanels
*/
function subPanels(p)
{
	var i, e, t;

	if (typeof(p) == 'string')
	{
		show_panel = p;
	}

	for (i = 0; i < panels.length; i++)
	{
		e = document.getElementById(panels[i]);


		if (e)
		{
			if (panels[i] == show_panel)
			{
				e.style.display = 'block';
				$("#"+panels[i] + '-tab a').addClass('active');
			}
			else
			{
				e.style.display = 'none';
				$("#"+panels[i] + '-tab a').removeClass('active');
			}
		}
	}
}

/**
* Call print preview
*/
function printPage()
{
	if (is_ie)
	{
		printPreview();
	}
	else
	{
		window.print();
	}
}

/**
* Show/hide groups of blocks
* c = CSS style name
* e = checkbox element
* t = toggle dispay state (used to show 'grip-show' image in the profile block when hiding the profiles)
*/
function displayBlocks(c, e, t)
{
	var s = (e.checked == true) ?  1 : -1;

	if (t)
	{
		s *= -1;
	}

	var divs = document.getElementsByTagName("DIV");

	for (var d = 0; d < divs.length; d++)
	{
		if (divs[d].className.indexOf(c) == 0)
		{
			divs[d].style.display = (s == 1) ? 'none' : 'block';
		}
	}
}

function selectCode(a)
{
	// Get ID of code block
	var e = a.parentNode.parentNode.getElementsByTagName('CODE')[0];

	// Not IE
	if (window.getSelection)
	{
		var s = window.getSelection();
		// Safari
		if (s.setBaseAndExtent)
		{
			s.setBaseAndExtent(e, 0, e, e.innerText.length - 1);
		}
		// Firefox and Opera
		else
		{
			// workaround for bug # 42885
			if (window.opera && e.innerHTML.substring(e.innerHTML.length - 4) == '<BR>')
			{
				e.innerHTML = e.innerHTML + '&nbsp;';
			}

			var r = document.createRange();
			r.selectNodeContents(e);
			s.removeAllRanges();
			s.addRange(r);
		}
	}
	// Some older browsers
	else if (document.getSelection)
	{
		var s = document.getSelection();
		var r = document.createRange();
		r.selectNodeContents(e);
		s.removeAllRanges();
		s.addRange(r);
	}
	// IE
	else if (document.selection)
	{
		var r = document.body.createTextRange();
		r.moveToElementText(e);
		r.select();
	}
}

/**
* Play quicktime file by determining it's width/height
* from the displayed rectangle area
*/
function play_qt_file(obj)
{
	var rectangle = obj.GetRectangle();

	if (rectangle)
	{
		rectangle = rectangle.split(',');
		var x1 = parseInt(rectangle[0]);
		var x2 = parseInt(rectangle[2]);
		var y1 = parseInt(rectangle[1]);
		var y2 = parseInt(rectangle[3]);

		var width = (x1 < 0) ? (x1 * -1) + x2 : x2 - x1;
		var height = (y1 < 0) ? (y1 * -1) + y2 : y2 - y1;
	}
	else
	{
		var width = 200;
		var height = 0;
	}

	obj.width = width;
	obj.height = height + 16;

	obj.SetControllerVisible(true);
	obj.Play();
}