﻿$(document).ready(function() {

	//MakeSearchBox();

	//SetBookmarkLink ();

	//PreventEnterSubmit ();

	//ShowCommentForm ();

	//ClearForm ();

	$('.headbox_list_li, .headbox_list_li *').hover
	(
		function(){
			 $(this).css('cursor', 'pointer'); //mouseover
		},
		function(){
			 $(this).css('cursor', 'pointer'); // mouseout
		}
	);

	$('.headbox_list_li').click
	(
		function ()
		{
			n ($(this).find('a').attr('href'));
			return false;
		}
	);


	$('.headbox_list_li .playnow').hover(
		function(){
			 $(this).css({top:'-32px'}); //mouseover
		},
		function(){
			 $(this).css({top:'0px'}); // mouseout
		}
	);

});
 
function MakeSearchBox() { 
$("#search").val('Search');
$("#search").attr("title","Press Enter to search");
$("#search").focus(function() {if ($("#search").val() == "Search") { $("#search").val(''); } });
$("#search").blur(function() {if ($("#search").val() == "") { $("#search").val('Search'); } });
$("#search").keypress(function(e) { if (e.keyCode == 13) { if ($("#search").val().length >= 3) { n('/search/' + encodeURIComponent(makeallowedrequest($("#search").val().replace(" ","-")))); }return false; }});
}
 
function makeallowedrequest(str) { 
str = str.replace("%","");
str = str.replace("^","");
str = str.replace("$","");
return str;
}
 
function redirect(url) {  window.location.href = url;   }
function n(url) { 
redirect(url); }

function navigateWithReferrer(url)
{
    var fakeLink = document.createElement ("a");
    if (typeof(fakeLink.click) == 'undefined')
        location.href = url;  // sends referrer in FF, not in IE
    else
    {
        fakeLink.href = url;
        document.body.appendChild(fakeLink);
        fakeLink.click();   // click() method defined in IE only
    }
}



function SetBookmarkLink ()
{
	// thanks! http://calisza.wordpress.com/2008/11/03/javascript-jquery-bookmark-script/
	
	// add a "rel" attrib if Opera 7+
	if(window.opera) {
		if ($("a.jqbookmark").attr("rel") != ""){ // don't overwrite the rel attrib if already set
			$("a.jqbookmark").attr("rel","sidebar");
		}
	}

	$("a.jqbookmark").click(function(event){
		event.preventDefault(); // prevent the anchor tag from sending the user off to the link
		var url = this.href;
		var title = this.title;

		if (window.sidebar) { // Mozilla Firefox Bookmark
			window.sidebar.addPanel(title, url,"");
		} else if( window.external ) { // IE Favorite
			window.external.AddFavorite( url, title);
		} else if(window.opera) { // Opera 7+
			return false; // do nothing - the rel="sidebar" should do the trick
		} else { // for Safari, Konq etc - browsers who do not support bookmarking scripts (that i could find anyway)
			 alert('Unfortunately, this browser does not support the requested action,'
			 + ' please bookmark this page manually.');
		}

	});
}


function PreventEnterSubmit ()
{
	// thanks!  http://blogs.mscommunity.net/blogs/borissevo/archive/2008/12/11/using-jquery-to-prevent-form-submit-when-enter-is-pressed.aspx
	// thanks!  http://docs.jquery.com/Events/jQuery.Event#event.stopPropagation.28.29
	// thanks!  http://www.scottklarr.com/topic/425/how-to-insert-text-into-a-textarea-where-the-cursor-is/
	// thanks!  http://plugins.jquery.com/project/fieldselection
	$("#ctl00_ContentPlaceHolder1_ctl00_comment").bind("keypress", function(e) {
		if (e.keyCode == 13) {
			var newlinechar = ($.browser.msie ? "\r\n" : "\n");
		
			/// -- this uses the jquery-fieldselection plugin
			var range = $(this).getSelection();
			/// --
				
			var pos = range.start;
			if (range.end != range.start)
			{
				eraseRange (this, pos, range.end);
			}
			setCaretToPos (this, pos + insertAtPos (this, pos, newlinechar));
        }
	});
	$("#aspnetForm").bind("keypress", function(e) {
		if (e.keyCode == 13) {
			e.preventDefault ();
		}
	});
}



function insertAtPos (txtarea, strPos, text)
{
	var front = (txtarea.value).substring(0,strPos);
	var back = (txtarea.value).substring(strPos,txtarea.value.length);
	txtarea.value=front+text+back;
	return text.length;
}


function eraseRange (txtarea, start, end)
{
	var front = (txtarea.value).substring(0, start);
	var back = (txtarea.value).substring(end, txtarea.value.length);
	txtarea.value=front+back;
	return start-end;
}



//
// thanks! http://stackoverflow.com/questions/499126/jquery-set-cursor-position-in-text-area
//

function setSelectionRange(input, selectionStart, selectionEnd) {
  if (input.setSelectionRange) {
    input.focus();
    input.setSelectionRange(selectionStart, selectionEnd);
  }
  else if (input.createTextRange) {
    var range = input.createTextRange();
    range.collapse(true);
    range.moveEnd('character', selectionEnd);
    range.moveStart('character', selectionStart);
    range.select();
  }
}

function setCaretToPos (input, pos) {
  setSelectionRange(input, pos, pos);
}




function ShowCommentForm ()
{
	$("#commentformwarning").css("display", "none");
	$("#commentformfields").css("display", "block");
}



function doPostBack (formname)
{
	$("#"+formname).submit ();
}





function ClearForm ()
{
	var postok = $('#postOk').value();
	
	if (postok == '1')
	{
		$('#aspnetForm').clear ();
	}
}

