function userTime(time){
	var month=new Array(12);
	month[0]="01";
	month[1]="02";
	month[2]="03";
	month[3]="04";
	month[4]="05";
	month[5]="06";
	month[6]="07";
	month[7]="08";
	month[8]="09";
	month[9]="10";
	month[10]="11";
	month[11]="12";
	
	var date = new Date();
	date.setTime(time);
	
	var year = date.getFullYear();	
	var m = month[date.getMonth()];
	if(date.getDate()<10){
		var day = "0" + date.getDate();
	}
	else {
		var day = date.getDate();
	}
	if(date.getHours()<10){
		var hour = "0" + date.getHours();
	}
	else {
		var hour = date.getHours();
	}
	if(date.getMinutes()<10){
		var min = "0" + date.getMinutes();
	}
	else {
		var min = date.getMinutes();
	}
	
	return year + "-" + m + "-" + day + " " + hour + ":" + min;		
}

function setKeyHeight(){
	row1_height = $("#row_1").height() - 31;
	row2_height = $("#row_2").height() - 31;
	row3_height = $("#row_3").height() - 31;
	row4_height = $("#row_4").height() - 31;
		
	if(row1_height < 176) {
		row1_height = 176;
	}
	if(row2_height < 176) {
		row2_height = 176;
	}
	if(row3_height < 176) {
		row3_height = 176;
	}
	if(row4_height < 176) {
		row4_height = 176;
	}
	
	for(i=1;i<10;i++){
		if(i<4){
			$("#key_"+i).height(row1_height);
		}
		if(i>3 && i<7){
			$("#key_"+i).height(row2_height);
		}
		if(i>6 && i<10){
			$("#key_"+i).height(row3_height);
		}
	}
}

function centerGenTitle(start_width,start_margin){
	var width = $("#prank_list_big").children(".header").children("h1").width();
	var margin_left = (656 - (width + 22))/2;
	$("#prank_list_big").children(".header").children("h1").css("width", start_width + "px");
	$("#prank_list_big").children(".header").children(".left").css("marginLeft", start_margin);
	$("#prank_list_big").children(".header").children(".left").animate({
		marginLeft: [margin_left + "px","easeOutBounce"]
	}, 1000);
	$("#prank_list_big").children(".header").children("h1").animate({
		width: [width,"easeOutBounce"]
	}, 1000);
}

function showImg(src){
	var show_modal = true;
	var z = 1001;
	var img = new Image();
	img.src = src + "?sid=" + Math.random();
	$(img).load(function(){
		if($("body > div").is(".ui-widget-overlay")){
			show_modal = false;
			z = 2001;
		}
		if(show_modal == true){
			$("body").append("<div class=ui-widget-overlay></div>");
			$(".ui-widget-overlay").width(function(i,w){
				return $("body").width();
			});
			$(".ui-widget-overlay").height(function(i,h){
				return $("body").height();
			});
			$(".ui-widget-overlay").css("z-index",z);
		}
		$("body").append("<div class=img_disp></div>");
		$(".img_disp").css("z-index",(z+1));
		$(".img_disp").append("<a href=#>X</a>");
		$(".img_disp").append("<img src="+img.src+" />");
		$(".img_disp > img,.img_disp > a").click(function(){
			$(this).parent().remove();
			if(show_modal == true){
				$(".ui-widget-overlay").remove();
			}
			return false;
		});
		$(".img_disp").css({"left":function(i,v){
			return ($(window).width() - $(this).outerWidth())/2;
		},"top":function(i,v){
			return (($(window).height() - $(this).outerHeight())/2) + $(window).scrollTop();
		}});
	});
	return false;
}

function confirmDialog(title,html,callback){
	$("body").append('<div id="confirm_dialog" title="'+title+'">'+html+'</div>');
	$("#confirm_dialog").dialog({
		autoOpen: true,
		modal: true,
		resizable: false,
		draggable: false,
		closeText: "X",
		closeOnEscape: false,
		minHeight: 80,
		buttons: {
			Cancel: function() {
				$(this).dialog("close");
			},
			"OK": function() {
				$(this).dialog("close");
				callback();
			}
		}
	});
}

function showToolTip(event,text){
	var mouse_x = event.pageX;
	var mouse_y = event.pageY;
	var win_w = $(window).width();
	var win_h = $(window).height();
	var left = mouse_x - 75;
	var top = mouse_y + 20;
	

	event.target.setAttribute("title","");
	$("body").append("<div class=tooltip>" + text + "</div>");
	$(".tooltip").css({"left" : left, "top" : top});
	$(".tooltip").show(200,function(){
		if($(".tooltip").width() + left  > win_w){
			left = win_w - $(".tooltip").width() - 30;
		}
		if($(".tooltip").height() + top > win_h){
			top = win_h - $(".tooltip").height() - 30;
		}
		$(".tooltip").animate({"left" : left, "top" : top},200);
		$(".tooltip").bind("mouseleave",function(e){
			hideToolTip(e,$(this).html());
		});
	});
	return true;
}
function hideToolTip(event,text){
	var x = event.pageX;
	var y = event.pageY;
	var tooltip_x = parseInt($(".tooltip").css("left"));
	var tooltip_y = parseInt($(".tooltip").css("top"));
	event.target.setAttribute("title",text);
	if(x < tooltip_x || x > tooltip_x + $(".tooltip").width() || y < tooltip_y || y > tooltip_y + $(".tooltip").height()){
		$(".tooltip").hide(200,function(){
			$(".tooltip").remove();
		});
	}
}

function setPageHeight(){
	var content_height = $(window).height() - $("#contentend").height() - $("#foot").outerHeight() - 118;
	$("#contentwrapper").css({"min-height":content_height});
}

function showError(event,text){
	var $target = $(event.target);
	var offset = $target.offset();
	var w = $target.outerWidth();
	$("body").append("<div class=errortooltip>" + text + "</div>");
	$(".errortooltip").css({"left" : offset.left + w, "top" : offset.top});
	$(".errortooltip").show();
}

var BrowserDetect = {
		init: function () {
			this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
			this.version = this.searchVersion(navigator.userAgent)
				|| this.searchVersion(navigator.appVersion)
				|| "an unknown version";
			this.OS = this.searchString(this.dataOS) || "an unknown OS";
		},
		searchString: function (data) {
			for (var i=0;i<data.length;i++)	{
				var dataString = data[i].string;
				var dataProp = data[i].prop;
				this.versionSearchString = data[i].versionSearch || data[i].identity;
				if (dataString) {
					if (dataString.indexOf(data[i].subString) != -1)
						return data[i].identity;
				}
				else if (dataProp)
					return data[i].identity;
			}
		},
		searchVersion: function (dataString) {
			var index = dataString.indexOf(this.versionSearchString);
			if (index == -1) return;
			return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
		},
		dataBrowser: [
			{
				string: navigator.userAgent,
				subString: "Chrome",
				identity: "Chrome"
			},
			{ 	string: navigator.userAgent,
				subString: "OmniWeb",
				versionSearch: "OmniWeb/",
				identity: "OmniWeb"
			},
			{
				string: navigator.vendor,
				subString: "Apple",
				identity: "Safari",
				versionSearch: "Version"
			},
			{
				prop: window.opera,
				identity: "Opera"
			},
			{
				string: navigator.vendor,
				subString: "iCab",
				identity: "iCab"
			},
			{
				string: navigator.vendor,
				subString: "KDE",
				identity: "Konqueror"
			},
			{
				string: navigator.userAgent,
				subString: "Firefox",
				identity: "Firefox"
			},
			{
				string: navigator.vendor,
				subString: "Camino",
				identity: "Camino"
			},
			{		// for newer Netscapes (6+)
				string: navigator.userAgent,
				subString: "Netscape",
				identity: "Netscape"
			},
			{
				string: navigator.userAgent,
				subString: "MSIE",
				identity: "Explorer",
				versionSearch: "MSIE"
			},
			{
				string: navigator.userAgent,
				subString: "Gecko",
				identity: "Mozilla",
				versionSearch: "rv"
			},
			{ 		// for older Netscapes (4-)
				string: navigator.userAgent,
				subString: "Mozilla",
				identity: "Netscape",
				versionSearch: "Mozilla"
			}
		],
		dataOS : [
			{
				string: navigator.platform,
				subString: "Win",
				identity: "Windows"
			},
			{
				string: navigator.platform,
				subString: "Mac",
				identity: "Mac"
			},
			{
				   string: navigator.userAgent,
				   subString: "iPhone",
				   identity: "iPhone/iPod"
		    },
			{
				string: navigator.platform,
				subString: "Linux",
				identity: "Linux"
			}
		]

	};
	BrowserDetect.init();
	var oldBrowser = false;
	if(BrowserDetect.browser == "Explorer"){
		if(parseInt(BrowserDetect.version) >= 5 && parseInt(BrowserDetect.version) < 7){
			oldBrowser = true;
		}
	}
	if(BrowserDetect.browser == "Firefox"){
		if(parseInt(BrowserDetect.version) < 3){
			oldBrowser = true;
		}
	}
	if(BrowserDetect.browser == "Safari"){
		if(parseInt(BrowserDetect.version) < 3){
			oldBrowser = true;
		}
	}
	if(BrowserDetect.browser == "Opera"){
		if(parseInt(BrowserDetect.version) < 9){
			oldBrowser = true;
		}
	}

$(	
	function(){
		if(oldBrowser == true){
			$("#old_browser").dialog({
				autoOpen: true,
				modal: true,
				resizable: false,
				draggable: false,
				width: 460,
				position: ['center',20],
				closeText: "X"
			});
			$.get("/ajax/get_old_browser.php",function(response,text,object){
				$("#old_browser").html(response);
			});
			
		}
		$("a[href$='.gif'],a[href$='.jpg'],a[href$='.png'],a[href$='.jpeg']").live("click",function(){
			showImg($(this).attr("href"));
			return false;
		});
		$("a[title]").bind("mouseenter",function(e){
			if($(".tooltip").length != 0){
				$(".tooltip").stop(true, true);
				showToolTip(e,$(this).attr("title"));
			}
			else {
				showToolTip(e,$(this).attr("title"));
			}
		});
		$("a[title]").bind("mouseleave",function(e){
			hideToolTip(e,$(".tooltip").html());
		});
		$("#topmenu1").hover(function(){
			$("#m_tip_1").animate({bottom: ["1px","easeOutBounce"]},1000);
		},function(){
			$("#m_tip_1").animate({bottom: "238px"},300);
		});
		$("#topmenu2").hover(function(){
			$("#m_tip_2").animate({bottom: ["1px","easeOutBounce"]},1000);
		},function(){
			$("#m_tip_2").animate({bottom: "238px"},300);
		});
		$("#topmenu3").hover(function(){
			$("#m_tip_3").animate({bottom: ["1px","easeOutBounce"]},1000);
		},function(){
			$("#m_tip_3").animate({bottom: "238px"},300);
		});
		$("#topmenu4").hover(function(){
			$("#m_tip_4").animate({bottom: ["1px","easeOutBounce"]},1000);
		},function(){
			$("#m_tip_4").animate({bottom: "238px"},300);
		});
		$("input[type='submit'],input[type='button']").bind("mousedown",function(){
			$(this).addClass("click");
		});
		$("input[type='submit'],input[type='button']").bind("mouseup",function(){
			$(this).removeClass("click");
		});
		$(".toggle_button").toggle(function(){
			$(this).css({"backgroundPosition":"0 -22px"});
		},function(){
			$(this).css({"backgroundPosition":"0 0"});
		});
		$("#right_login_btn").click(function(){
			$(this).remove();
			$("#login_display").slideDown('slow');
			return false;
		});
		$("#right_remember").toggle(function(){
			$("#post_right_remember").val("1");
		},function(){
			$("#post_right_remember").val("0");
		});
		$("input[type='text']:disabled").addClass("disabled");
		$(window).resize(function(){
			setPageHeight();
		});
		
		setPageHeight();
	}
);

