(function(jQuery)
{
	jQuery.extend({			
		noticeAdd: function(options)
		{	
			var defaults = {
				inEffect: 			{opacity: 'show'},	// in effect
				inEffectDuration: 	600,				// in effect duration in miliseconds
				stayTime: 			3000,				// time in miliseconds before the item has to disappear
				text: 				'',					// content of the item
				stay: 				false,				// should the notice item stay or not?
				type: 				'notice' 			// could also be error, succes
			}
			
			// declare varaibles
			var options, noticeWrapAll, noticeItemOuter, noticeItemInner, noticeItemClose;
								
			options 		= jQuery.extend({}, defaults, options);
			noticeWrapAll	= (!jQuery('.notice-wrap').length) ? jQuery('<div></div>').addClass('notice-wrap').appendTo('body') : jQuery('.notice-wrap');
			noticeItemOuter	= jQuery('<div></div>').addClass('notice-item-wrapper');
			noticeItemInner	= jQuery('<div></div>').hide().addClass('notice-item ' + options.type).appendTo(noticeWrapAll).html('<p>'+options.text+'</p>').animate(options.inEffect, options.inEffectDuration).wrap(noticeItemOuter);
			noticeItemClose	= jQuery('<div></div>').addClass('notice-item-close').prependTo(noticeItemInner).html('x').click(function() { jQuery.noticeRemove(noticeItemInner) });
			
			// hmmmz, zucht
			if(navigator.userAgent.match(/MSIE 6/i)) 
			{
		    	noticeWrapAll.css({top: document.documentElement.scrollTop});
		    }
			
			if(!options.stay)
			{
				setTimeout(function()
				{
					jQuery.noticeRemove(noticeItemInner);
				},
				options.stayTime);
			}
		},
		
		noticeRemove: function(obj)
		{
			obj.animate({opacity: '0'}, 600, function()
			{
				obj.parent().animate({height: '0px'}, 300, function()
				{
					obj.parent().remove();
				});
			});
		}
	});
})(jQuery);

function resultPopup(results) {
jQuery.noticeAdd({
        text: results,
        stay: false,
        type: 'notice',
        stayTime: 5000
});
}

var myT;

function clearDefault(dstring) {
  if (dstring.defaultValue==dstring.value) dstring.value = ""
}

function sendRequest()
{
	minLength = document.getElementById('searchbox').value.length;
	
		if(myT != undefined)
		{
		clearTimeout(myT);
		}
		myT = setTimeout('beginSearch()', 500);
}
	
function beginSearch() {
	document.getElementById("searchcontent").innerHTML="<div id=loading></div>";
	var s_term = document.getElementById("searchbox").value;
      s_term = s_term.replace("&", "and");
      s_term = s_term.replace("%", "percent");
	var s_platform = document.getElementById("selectarea").value;
	var s_order = document.getElementById("sortarea").value;
	var mycountr = ajaxRequest('/count-review.php', 'q=' + s_term + '&platform=' + s_platform,resultPopup,'popupContact');
	
	ajaxRequest("/getreview.php", 'q=' + s_term + '&platform=' + s_platform + '&limit=20' + '&order=' + s_order , replaceContent, 'searchcontent');	
	
	ajaxRequest("/getreview_latest.php", 'q=' + s_term + '&platform=' + s_platform + '&limit=7' , replaceContent, 'searchcontent_latest');		
	
	ajaxRequest("/getreview_top.php", 'q=' + s_term + '&platform=' + s_platform + '&limit=7' , replaceContent, 'searchcontent_top');		
	
	ajaxRequest("/getreview_worst.php", 'q=' + s_term + '&platform=' + s_platform + '&limit=7' , replaceContent, 'searchcontent_worst');		
	
	document.getElementById('p1').innerHTML=document.getElementById("selectarea").value;
	document.getElementById('p2').innerHTML=document.getElementById("selectarea").value;
	document.getElementById('p3').innerHTML=document.getElementById("selectarea").value;
	
}	

function ajaxRequest(page, post_variables, return_to, target) {
	ajax = new Ajax();
	ajax.doPost(page, post_variables , return_to, target);	
}


function replaceContent(str, content) {
	document.getElementById(content).innerHTML=str;
}

function showList() {
	var s_term = document.getElementById("searchbox").value;
	var s_platform = document.getElementById("selectarea").value;
	var s_order = document.getElementById("sortarea").value;
	ajaxRequest("/friendlist.php", 'q=' + s_term + '&platform=' + s_platform + '&limit=20' + '&order=' + s_order , replaceContent, 'searchcontent');	
}

function moreResults(limitnumber) {
	var s_term = document.getElementById("searchbox").value;
	var s_platform = document.getElementById("selectarea").value;
	var s_order = document.getElementById("sortarea").value;
      s_term = s_term.replace("&", "and");
      s_term = s_term.replace("%", "percent");
	ajaxRequest("/getreview.php", 'q=' + s_term + '&platform=' + s_platform + '&limit=' + limitnumber + '&order=' + s_order , replaceContent, 'searchcontent');	
}

function addtoList(code, typef) {
ajaxRequest('/showlist.php', 'code='+code+'&type='+typef, replaceContent, 'listHolder');
}

function helpful(version, code, divid) {
var passedid = divid;
ajaxRequest('/helpful.php', 'version='+version+'&code='+code, replaceContent, passedid);	
}

var myP;

function showUserSearch(user) {
ajaxRequest('/searchUser.php', 'user='+user, replaceContent, 'userDisplay');	
}

function checkUsername(user) {
if (document.getElementById('author1')) {
document.getElementById('author1').style.display="none";
}
if (document.getElementById('author2')) {
document.getElementById('author2').style.display="none";
}
if(myP != undefined)
		{
		clearTimeout(myP);
		}
		myP = setTimeout('showUserSearch("'+user+'")', 1000);
}

function spawnReview(review) {
document.getElementById('popupContact').innerHTML = '';
ajaxRequest('/ajax-review.php', 'code='+review, replaceContent, 'popupContact');
centerPopup();
loadPopup();
}

//SETTING UP OUR POPUP  
//0 means disabled; 1 means enabled;  
var popupStatus = 0;

//loading popup with jQuery magic!  
function loadPopup(){  
//loads popup only if it is disabled  
if(popupStatus==0){  
$("#backgroundPopup").css({  
"opacity": "0.7"  
});  
$("#backgroundPopup").fadeIn("slow");  
$("#popupContact").fadeIn("slow");  
popupStatus = 1;  
}  
} 

//disabling popup with jQuery magic!  
function disablePopup(){  
//disables popup only if it is enabled  
if(popupStatus==1){  
$("#backgroundPopup").fadeOut("slow");  
$("#popupContact").fadeOut("slow");  
popupStatus = 0;  
}  
}

//centering popup  
function centerPopup(){  
//request data for centering  
var windowWidth = document.documentElement.clientWidth;  
var windowHeight = document.documentElement.clientHeight;  
var popupHeight = $("#popupContact").height();  
var popupWidth = $("#popupContact").width();  
//centering  
$("#popupContact").css({  
"position": "fixed",  
"top": windowHeight/2-popupHeight/2,  
"left": windowWidth/2-popupWidth/2  
});  
//only need force for IE6  
$("#backgroundPopup").css({  
"height": windowHeight  
});  
}
