$(document).ready(function(){
	
	xna_menu();
	
	my_lightbox("a[rel^='xnaPhoto'], a[rel^='lightbox']");
	
	$('.xnaTip').xnaTooltip();
	
	$('.xnaGetPage').click(function() {
		$('#pageGetContent').remove();
		var ptitle = $(this).attr('title');
		$.post('index.php?mod=ajax',{'act':'get_page','title':ptitle},function(xml) {
			var pageTitle = $(xml).find('title').text();
			var pageContent = $(xml).find('content').text();
			$('body').append('<div id="pageGetContent" title="'+pageTitle+'"></div>');
			$('#pageGetContent').html(pageContent);
			$('#pageGetContent').dialog({
				modal: true,
				width: 600,
				height: 400	
			});
		});
	});
	
	$("#loginLink").click(function() {
		$("#loginDialog").dialog({
			modal: true,
			width: 370	
		});
		$("#q_username").focus();
		return false;
	});
	
	$("#q_formLogin").submit(function() {
		
		if ($("#q_username").val() =="") {
			$("#q_username").css('border','1px solid red');
			$("#q_username").focus();
			return false;
		}
		
		if ($("#q_password").val() =="") {
			$("#q_password").css('border','1px solid red');
			$("#q_password").focus();
			return false;
		}
		
	});

});

function xna_menu()
{
	$("#nav a, .subnav a").removeAttr('title');
	$(" #nav ul ").css({display: "none"}); // Opera Fix
	
	$("#nav li").each(function()
	{	
		
		var $sublist = $(this).find('ul:first');
		
		$(this).hover(function()
		{	
			$sublist.stop().css({overflow:"hidden", height:"auto", display:"none"}).slideDown(400, function()
			{
				$(this).css({overflow:"visible", height:"auto"});
			});	
		},
		function()
		{	
			$sublist.stop().slideUp(400, function()
			{	
				$(this).css({overflow:"hidden", display:"none"});
			});
		});	
	});
}

function xna_quick_menu(menucls) {
	$('.'+menucls).mouseover(function() {
		var relArr = $(this).attr('rel').split('@');
		var photo_id = $(this).attr('id');
		var ptitle = $(this).attr('title');
		var gpath = relArr[0];
		var img = relArr[1];
		var uhome = relArr[2];
		
		var menuX = -2;
		var windX = $(window).width();
		var mouseX = $(this).offset().left-7;
		var mouseY = $(this).offset().top-6;
		if (mouseX > 850 && windX <= 1024) {
			menuX = -98;
		}
		
		var tipcon ='';
		$('#photo-hover').remove();
		tipcon +='<div id="photo-hover" style="left:'+mouseX+'px; top:'+mouseY+'px; display: block">';
		tipcon +='<div id="photo-hover-shadow" class="shadowDark">';
		tipcon +='<div id="photo-hover-inner">';
		tipcon += '<a href="index.php?mod=gallery&act=detail&id='+photo_id+'"><img id="photo-hover-link" border="0" src="'+gpath+'/t75_'+img+'" width="75" height="75" alt="" /></a>';
		tipcon += '<div id="photo-button-bar"><img id="photo-img-button" border="0" src="images/menu_down_hover.gif" width="15" height="75" alt="More options" title="More options" style="cursor:hand" onclick="photo_toggle_menu()" /></div>';
		tipcon += '<div id="photo-menu" style="width: '+thumbsize+'px; left: '+menuX+'px ; top: -2px; display: none; visibility: visible">';
		
		tipcon += '<div id="photo-menu-other-div" style="display: block">';
		tipcon += '<div class="menu_item_line_above"></div>';
		tipcon += '<img border="0" src="'+gpath+'/thumb_'+img+'" width="'+thumbsize+'" alt="" />';
		tipcon += '<p>'+ptitle+'</p>';
		tipcon += '<a id="photo-edit-link" class="block" href="index.php?mod=user&act=edit_photo&id='+photo_id+'">'+l_edit+'</a>';
		tipcon += '<a id="photo-delete-link" class="block" href="index.php?mod=user&act=delete_photo&id='+photo_id+'">'+l_delete+'</a>';
		if (uhome == 1) {
			tipcon += '<a id="photo-hide-link" class="block" href="index.php?mod=user&act=hide_photo&id='+photo_id+'&t=0">'+l_hide+'</a>';
		} else {
			tipcon += '<a id="photo-hide-link" class="block" href="index.php?mod=user&act=hide_photo&id='+photo_id+'&t=1">'+l_display+'</a>';
		}
		tipcon += '<div class="xna_logo"><img border="0" src="images/slogo.gif" /></div>';
		tipcon += '</div>';
		tipcon += '</div>';
		tipcon += '</div>';
		tipcon += '</div>';
		tipcon += '</div>';
		tipcon += '</div>';
		$('body').append('<div id="fixIE"></div>');
		$('#fixIE').html(tipcon);
	});

	$(document).click(function(ev) {
		var target = $(ev.target);
		if (target.parents("#photo-hover").length == 0) {
			$('#photo-img-button').attr('src','images/menu_down_hover.gif');
			$('#photo-menu').css('display','none');
			$('#photo-hover').css('display','none');
		}
	});
	
	$(document).keydown(function(ev) {
		var code = ev.keyCode ? ev.keyCode : ev.which;
		if (code == 27) {
			$('#photo-hover').css('display','none');
		}
	});
}
	
function photo_toggle_menu(operator,this_id) {
	var css = $('#photo-menu').css('display');
	if (css == 'none') {
		$('#photo-img-button').attr('src','images/menu_up_hover.gif');
		$('#photo-menu').css("display","block");
		$('#return_content').html('Loading...');
		$.post(operator,{'id':this_id},function(data) {
			$('#return_content').html(data);
		});
	} else {
		$('#photo-img-button').attr('src','images/menu_down_hover.gif');
		$('#photo-menu').css('display','none');
	}
}

function my_lightbox($elements)
{
jQuery($elements).prettyPhoto({
		"theme": 'light_square' /* light_rounded / dark_rounded / light_square / dark_square */																
	});
}

function myBBcode(tname) {
	$('textarea[name='+tname+']').bbcodeeditor(
	{
		bold:$('.bold'),italic:$('.italic'),underline:$('.underline'),link:$('.link'),quote:$('.quote'),image:$('.image'),smiley:$('.smiley'),
		exit_warning:true
	});
}

function showLoading() {
	var docH = $(document).height();
	var mouseY = $(window).scrollTop() + $(window).height()/2 - 100;	
	var mouseX = $(window).width()/2 - 100;
	$('body').append('<div id="loadingBox" style="z-index: 90000"><div class="ui-widget-overlay" style="height: '+docH+'px; display: block"></div><div id="loading" style="position: absolute; top: '+mouseY+'px; left: '+mouseX+'px; z-index: 1001; border: 1px solid #000; padding: 30px; background-color: #fff; display: block; text-align: center"><img border="0" src="images/progress_bar.gif" alt="loading..." /><br /><br />'+l_loading_progress+'</div></div>');
	
	$(window).scroll(function() {
		
	});
}

function hideLoading() {
	$('#loadingBox').remove();
}

$.fn.xnaPrice = function() {
	
	$(this).each(function() {
		var $this = $(this);
		$this.keyup(function() {
		var str = jQuery.trim($this.val());
		var strTemp = $.getNumber(str);
		
		if (strTemp.length < 3) {
			$this.val(strTemp);
		} else {
			var strResult = "";
			for(var i =0; i< strTemp.length; i++)
				strTemp = strTemp.replace(" ", "");
			for(var i = strTemp.length; i>=0; i--)
			{
				if(strResult.length >0 && (strTemp.length - i -1) % 3 == 0)
					strResult = " " + strResult;
				strResult = strTemp.substring(i, i + 1) + strResult;
			}
			$this.val(strResult);	
		}		
	});
	});	
	
	jQuery.getNumber = function(str) {
		for(var i = 0; i < str.length; i++)
		{	
			var temp = str.substring(i, i + 1);		
			if(!(temp >= 0 && temp <=9))
			{
				alert(l_number_rq);
				return str.substring(0, i);
			}
			
		}
		return str;
	}
}

$.fn.xnaImage = function(src){ 
	var $this = $(this);
	//$this.html('<center><img border="0" src="images/progress.gif" width="16" height="16"></center>');
	var i = new Image();
	i.src = src;
	$this.html(i);
//	$(i).load(function() {
//		$this.html(i);
//	});
}

$.fn.xnaCheckAll = function(cbname) {
	$(this).click(function() {
		var checked_status = this.checked;
		$("input[@name="+cbname+"]").each(function()
		{
			this.checked = checked_status;
		});
	});
}

function loadPage(divname,page,purl) {
	$('#'+divname).html('<img border="0" src="images/progress_bar.gif" alt="Loading..." /><br /><br />'+l_loading_progress);
	$.get(purl,{'page':page},function(xml) {
		$('#'+divname).html($("datatable",xml).text());
	});
}

function sendReport(report_id,actdo) {
	$('#sendreport_'+report_id).remove();
	
	var frm = '<div id="report_description"></div>';
	frm += '<div><textarea name="rp_content" id="rp_content" cols="70" rows="5" style="width: 500px"></textarea></div>';
	frm += '<input class="send" type="submit" name="submit" value="'+l_send_report+'" onclick="doReport('+report_id+',\''+actdo+'\');" />';
	$('body').append('<div id="sendreport_'+report_id+'" title="'+l_report+'">'+frm+'</div>');
	
	$('#report_description').html(l_loading_progress);
	$.post('index.php?mod=ajax',{'act':'get_report_desc','do':''+actdo+'','id':report_id},function(data) {
		$('#report_description').html(data);
	});
	
	$('#sendreport_'+report_id).dialog({
		modal: true,
		width: 540,
		closeText: 'hide'
	});
}

function doReport(report_id,actdo) {
	var rp_content = $('#rp_content').val();
	if (jQuery.trim(rp_content).length < 10) {
		alert(l_content_note);
		return false;	
	}
	
	$('#sendreport_'+report_id).dialog('close');
	showLoading();
	$.post('index.php?mod=ajax',{'act':'do_report','do':actdo,'id':report_id,'message':rp_content},function(xml){
		hideLoading();
		
		if ($("status",xml).text() == "1") {
			alert($("alert",xml).text());
			$('#sendreport_'+report_id).dialog('open');
			return;	
		}
		
		$('#sendreport_'+report_id).dialog('destroy');
		
	});
}



