$(document).ready(function(){
	if($("table.tblsort").get().length > 0){
		$("table.tblsort").tablesorter( { debug : false } );
	}
	// 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){
			window.sidebar.addPanel(title, url, "");
		} else if(document.all){
			window.external.AddFavorite(url, title);
		} else if(window.opera && window.print){
			alert('Press ctrl+D to bookmark (Command+D for macs) after you click Ok');
		} else if(window.chrome){
			alert('Press ctrl+D to bookmark (Command+D for macs) after you click Ok');
		}
	});
	$("#tellFreind").click(function(){
		tell_freind(this.title);
	});
});
function tell_freind(subject_row){
	var height = $(document.body).innerHeight() + "px"; 
	var width = $(document.body).innerWidth() + "px";
		
	$("#frmPopupSendEmail").remove();
	$("#formEmailBox").remove();
	$("#popupSendEmailOverlay").remove();
	
	$(document.body).append("<div id='popupSendEmailOverlay'></div><div id='formEmailBox'></div>");
	
	$("#popupSendEmailOverlay").css({
		"position"	: "absolute",
		"top"		: "0px",
		"left"		: "0px",
		"height" 	: height,
		"width"  	: "100%",
		"display"	: "block",
		"_filter"	: "alpha(opacity=0)",
		"opacity"	: "0",
		"background": "#FFF",
		"z-index" : "999999",
		
	});
	$("#formEmailBox").append("<form id='frmPopupSendEmail' method='post'><ul>"+
								"<input type='hidden' value='"+window.location+"' name='url'/>"+
								"<input type='hidden' value='"+window.document.title+"' name='title'/>"+
								"<li style='padding:5px; clear:both; display:block;'><label style='display:block; padding:5px; font-weight:bold; color:#000;'>Your name:</label><input type='text' style='width:580px; ' name='sender' id='sender' placeholder='Your full name'></li>"+
								"<li style='padding:5px; clear:both; display:block;'><label style='display:block; padding:5px; font-weight:bold; color:#000;'>Email address(es) to send the link to: <small>(separate email addresses with a comma)</small></label><textarea style='width:580px; height:100px; resize:none' name='receiver' id='receiver' placeholder='Enter the emails'></textarea></li>"+
								"<li style='padding:5px; clear:both; display:block; text-align:right'><input type='submit' name='send-email' id='send-email' value='Tell A Friend' class='yellowGrad fb'/>&nbsp;<input type='button' id='cancel-email-sending' value='Cancel' class='yellowGrad fb' /></li>"+
								"</ul></form>");
								
	$("#frmPopupSendEmail").css({
		"-webkit-border-radius" : "10px",
		"-moz-border-radius" : "10px",
		"border-radius" : "10px",
		"width" : "610px", 
		"background-color" : "#424242", 
		"border" : "10px solid #424242",
		"-webkit-box-shadow" : "0px 1px 3px rgba(0, 0, 0, 0.1)",
		"-moz-box-shadow" : "0px 1px 3px rgba(0, 0, 0, 0.1)",
		"box-shadow" : "0px 1px 3px rgba(0, 0, 0, 0.1)",
	});
	
	$("#frmPopupSendEmail ul").css({
		"-webkit-border-radius" : "10px",
		"-moz-border-radius" : "10px",
		"border-radius" : "10px",
		"background-color" : "#FFF", 
		"display" : "block",
		"padding" : "5px", 
	});
	
	var pagesize = pageSize();
	var top  = (((pagesize[3] - $("#formEmailBox").outerHeight()) - 100)/2) + "px" 
			  //( parseInt($(window).scrollTop()) + (parseInt(height)  - $("#popupMessageBox").innerHeight()) ) + "px";
	var formwidth = $("#frmPopupSendEmail").innerWidth();
	var left = ((parseInt(width) - parseInt(formwidth)) / 2) + "px";
	
	$("#formEmailBox").css({
		"position" : "fixed",
		"top"	:	top,
		"left"	:	left,
		"z-index" : "9999999999999999",
		"opacity" : "0",
		"_filter" : "alpha(opacity=0)",
		"-webkit-border-radius" : "10px",
		"-moz-border-radius" : "10px",
		"border-radius" : "10px",
	});
	$("#popupSendEmailOverlay").fadeTo("fast", .8);
	$("#formEmailBox").fadeTo("fast", 1);
	$("#cancel-email-sending").click(function(){
		$("#formEmailBox, #popupSendEmailOverlay").fadeOut("fast", function(){
			$("#formEmailBox, #popupSendEmailOverlay").remove();
		});
	});
	$("#frmPopupSendEmail").submit(function(){
		if($("input#sender").val() ==""){
			$("input#sender").focus();
			return false;
		}
		if($("textarea#receiver").val() ==""){
			$("textarea#receiver").focus();
			return false;
		}
		$("#send-email").get(0).disabled = true;
	    $("#cancel-email-sending").get(0).disabled = true;
		var data = $(this).serialize();
		$.ajax({
		   type: "POST",
		   url: "/request/xmlhttp/tell-friend/?request=tell-friends",
		   data: data,
		   dataType: "json",
		   success: function(result){
				$("#formEmailBox, #popupSendEmailOverlay").fadeOut("fast", function(){
					$("#formEmailBox, #popupSendEmailOverlay").remove();
				})
			   alert(result.message);
		   }
		 });
		return false;
	});
}
// JavaScript Document
function pageSize(){ //Get the page size of the document & window
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight); 
	return arrayPageSize;
}
function show_tooltip(objTooltip){
	$(objTooltip).hover(function(){	
		var offset = $(objTooltip).offset();
		var tip = $(objTooltip).attr("tooltip");
		var pagesize = pageSize();
		if(document.getElementById("tooltip"))$("div#tooltip").remove();
		$(document.body).append("<div id='tooltip' style='min-width:100px; padding:5px;'>"+tip+"</div>");
		$("div#tooltip").css({
				'display' : 'inline', 
				'position' : 'absolute', 
				'max-width' : '600px',
				"text-align" : "justify",
		});
		$(this).mousemove(function(e){
			var left = e.pageX + 10;
			var top  = e.pageY + 20;
			//var position = $(objTooltip).position();
			//var top = parseInt(position.top) + (parseInt($(objTooltip).innerHeight()) / 2) + 8;
			//var left = parseInt(position.left) + (parseInt($(objTooltip).innerWidth()) / 2) + 8;
			var x_pos = left + parseInt($("div#tooltip").innerWidth());
			var  wtop = (top - parseInt($(window).scrollTop())) + parseInt($("div#tooltip").innerHeight());
			
			if(wtop > pagesize[3]){
				top = top - parseInt($("div#tooltip").innerHeight());
				if(x_pos > pagesize[2]){
					left = parseInt(offset.left) - parseInt($("div#tooltip").innerWidth());
					$("div#tooltip").css({
						"-webkit-border-radius" : "5px",
						"-webkit-border-bottom-right-radius" : "0",
						"-moz-border-radius" : "5px",
						"-moz-border-radius-bottomright" : "0",
						"border-radius" : "5px",
						"border-bottom-right-radius" : "0",
					});
				}else{
					$("div#tooltip").css({
						"-webkit-border-radius" : "5px",
						"-webkit-border-bottom-left-radius" : "0",
						"-moz-border-radius" : "5px",
						"-moz-border-radius-bottomleft" : "0",
						"border-radius" : "5px",
						"border-bottom-left-radius" : "0",
					});
				}
			}else{
				if(x_pos > pagesize[2]){
					left = parseInt(offset.left) - parseInt($("div#tooltip").innerWidth());
					$("div#tooltip").css({
						"-webkit-border-radius" : "5px",
						"-webkit-border-top-right-radius" : "0",
						"-moz-border-radius" : "5px",
						"-moz-border-radius-topright" : "0",
						"border-radius" : "5px",
						"border-top-right-radius" : "0",
					});
				}else{
					$("div#tooltip").css({
						"-webkit-border-radius" : "5px",
						"-webkit-border-top-left-radius" : "0",
						"-moz-border-radius" : "5px",
						"-moz-border-radius-topleft" : "0",
						"border-radius" : "5px",
						"border-top-left-radius" : "0",
					});
				}
			}
			$("div#tooltip").css({
				'position' : 'absolute',
				'left' : left + "px",
				'top' : top + "px",
				'display' : 'block',
				'background-color' : '#424242',
				'color' : '#FFF',
				"z-index" : "9999999999999999",
				"-webkit-box-shadow" : "0px 1px 3px rgba(0, 0, 0, 0.1)",
				"-moz-box-shadow" : "0px 1px 3px rgba(0, 0, 0, 0.1)",
				"box-shadow" : "0px 1px 3px rgba(0, 0, 0, 0.1)",
			});
		});
		$("div#tooltip").css({
			'position' : 'absolute',
			'display' : 'block',
			'background-color' : '#424242',
			'color' : '#FFF',
			"z-index" : "9999999999999999",
			"opacity" : "0",
			"_filter" : "alpha(opacity=0)",
			"-webkit-box-shadow" : "0px 1px 3px rgba(0, 0, 0, 0.1)",
			"-moz-box-shadow" : "0px 1px 3px rgba(0, 0, 0, 0.1)",
			"box-shadow" : "0px 1px 3px rgba(0, 0, 0, 0.1)",
		});
		$("div#tooltip").fadeTo("fast", 1);
	}, function(){
		if(document.getElementById("tooltip"))$("div#tooltip").remove();
	});
}
function popup_ajax_content_box(url, data){
	var height = $(document.body).innerHeight() + "px"; 
	var width = $(document.body).innerWidth() + "px";
	var urlpath = url;
	var datavars = data;
	$("#contentBox").remove();
	$("#contentBoxOverlay").remove();
	
	$(document.body).append("<div id='contentBoxOverlay'></div><div id='contentBox'><div id='contentBoxData'><h2 align='center' id='contentBoxDataLoading'>Loading...</h2></div><div style='display:block; padding:10px 5px 0px 5px; background:#424242; text-align:right'><input type='button' class='yellowGrad' value=' Close ' id='close-contentbox' /></div></div>");
	
	$("#contentBoxOverlay").css({
		"position"	: "absolute",
		"top"		: "0px",
		"left"		: "0px",
		"height" 	: height,
		"width"  	: "100%",
		"display"	: "block",
		"_filter"	: "alpha(opacity=0)",
		"opacity"	: "0",
		"background": "#FFF",
		"z-index" : "999999",
	});
	$("#contentBox").css({
		"width" : "180px",
		"opacity" : "0",
 		"_filter" : "alpha(opacity=0)",
		"background": "#424242",
		"border" : "10px solid #424242",
		"-webkit-border-radius" : "10px",
		"-moz-border-radius" : "10px",
		"border-radius" : "10px",
	});
	$("#contentBoxData").css({
		"max-height" : "500px",
		"min-height" : "100px",
		"overflow-y" : "auto",
		"cursor" : "wait",
		"background": "#FFF",
		"-webkit-border-top-left-radius": "10px",
		"-webkit-border-top-right-radius": "10px",
		"-moz-border-radius-topleft": "10px",
		"-moz-border-radius-topright": "10px",
		"border-top-left-radius": "10px",
		"border-top-right-radius": "10px",
		"padding" : "10px",
	});
	var loading_top = (parseInt($("#contentBoxData").innerHeight()) - parseInt($("#contentBoxDataLoading").innerHeight()))/2;
	$("#contentBoxDataLoading").css({"margin-top": loading_top + "px", "padding" : "20px"});
	
    var pagesize = pageSize();
	var top  = (((pagesize[3] - $("#contentBox").outerHeight()) - 100)/2) + "px" 
			  //( parseInt($(window).scrollTop()) + (parseInt(height)  - $("#popupMessageBox").innerHeight()) ) + "px";
	var formwidth = $("#contentBox").innerWidth();
	var left = ((parseInt(width) - parseInt(formwidth)) / 2) + "px";
	
	$("#contentBox").css({
		"position" : "fixed",
		"top"	:	top,
		"left"	:	left,
		"z-index" : "9999999",		
		"width" : "auto",
	});
	var xhr = $.ajax({
	   type: "POST",
	   url: urlpath,
	   data: datavars,
	   success: function(result){
  		    $("#contentBox").hide();
		    $("#contentBoxData").html(result);
			$("#contentBoxData").css({
				"cursor" : "inherit",
			});
			if($("#contentBoxData").innerHeight() > 500){
				$("#contentBox").css({"height" : "500px"});
				
			}
			$("#contentBox").css({"max-width" : "700px !important"});
			if($("#contentBoxData").find("table").length > 0){
				$("#contentBox").css({"width" : "700px !important"});
				
			}
				
			var pagesize = pageSize();
			var top  = (((pagesize[3] - $("#contentBox").outerHeight()) - 100)/2) + "px" 
					  //( parseInt($(window).scrollTop()) + (parseInt(height)  - $("#popupMessageBox").innerHeight()) ) + "px";
			var formwidth = $("#contentBox").innerWidth();
			var left = ((parseInt(width) - parseInt(formwidth)) / 2) + "px";
			$("#contentBox").show();
			$("#contentBox").css({"top" : "0px", "left"	:	left});
			$("#contentBox").animate({
				"position" : "fixed",
				"top"	:	top,				
				"z-index" : "9999999",		
			}, 500);
	   }
	 });
	$("#contentBoxOverlay").fadeTo("fast", .8);
	$("#contentBox").fadeTo("fast", 1);
	$("#close-contentbox").click(function(){
		xhr.abort();
		$("#contentBox, #contentBoxOverlay").fadeOut("fast", function(){
			$("#contentBox, #contentBoxOverlay").remove();
		});
	});
}
$(document).ready(function(){
	$(".show-tooltip").each(function(i, el){
		show_tooltip(el);
	});
	$("a.pop-ajax-box").click(function(){
		popup_ajax_content_box(this.href, $(this).attr("data"));
		return false;
	});
});
