$(document).ready(function() {
	hackgodInit();
});

var MediaID = 0;

var select_x;
var select_y;
var select_w;
var select_h;

function create_flv_area (file_id, area_name)
{
    var FU = {
        movie:"/static/11/swf/flvplayer.swf",
        width:"500",
        height:"400",
        majorversion:"7",
        build:"0",
        bgcolor:"#FFAA00",
        allowfullscreen:"true",
        flashvars:"file=/playlist/" + file_id + "/play2.xml&displayheight=380&lightcolor=0xFFFFFF&backcolor=0xFFAA00&frontcolor=0x000000&overstretch=false&shuffle=false&repeat=list"
    };
    UFO.create(FU, area_name);
}


function hackgodInit ()
{
    // Hide AJAX sign
    $('#aactive').hide();

    // Activate any DatePickers
    $('input.date-picker').datePicker( { startDate : "01/01/2001" } );

    // Activate any peopleTag divs
    $('#ThumbBoard').hover(
	function () {
		$('.peopleTag').addClass('peopleTagShow');
	},
	function () {
		$('.peopleTag').removeClass('peopleTagShow');
	}
    );

    // Add click for new comments
    $('#newCommentLink').click( function () {
					$(this).hide();
					$('#newComment form').slideDown();
					
					return false;
				});

    MediaID = $('input#hiddenMediaID').val();

    var link = ' <a href="#" class="smaller" onclick="fsShowDiv(this)">More Options</a>';
    $('.div_hidden').hide();
    $('.fs_hidden > legend').append(link);

    $('.picNote').hide();
    $('.picHover').hover( function() { $('.picNote').show(); }, function () { $('.picNote').hide(); });
    $('.menulink').each(function(i){ 
                                      $(this).click(
                                                    function() {
                                                                   MenuLink(this.innerHTML);
                                                                   return false;
                                                               }
                                                   );
                                   }
                       );
    $('.helplink').each(function(i){ 
                                      $(this).click(
                                                    function() {
                                                                   HelpLink(this.innerHTML);
                                                                   return false;
                                                               }
                                                   );
                                   }
                       );
	init_tabs();

    if ($('#ownercheck').val() == "yes") {
        $('#summary').append('<p><a class="SmallTitle" href="#" onclick="init_resize(); return false;">Selection Tool</a></p>');
    }
}
function init_tabs ()
{
	$('.tab').click( function() { 
				$('.tabDialog').hide();
				$('#tab_' + this.innerHTML).show();
				return false;
			 } );
}

function AddTag (tagName)
{
	$('#txtTags').val($('#txtTags').val() + " " + tagName);
}
function frmPersonSubmit ()
{
	var formData = $('#frmPerson input').serialize();
	
	$.ajax(
		{
			type		: 'POST',
			url		: '/tag.cgi',
			dataType	: 'script',
			data		: formData
		}
	);

	return false;
}
function frmPersonCancel ()
{
	$('#frmPerson').remove();
	$('#AdvTagging fieldset').show();
}
function init_resize ()
{
	select_x = 42; //100;
	select_y = 27; //150;
	select_w = 100;
	select_h = 100;

	$('#resizeMe').remove();
	$('#note').remove();
	$('#main').after('<div id="resizeMe"><div id="resizeSE"></div><div id="resizeNE"></div><div id="resizeNW"></div><div id="resizeSW"></div></div></div>');
//	$('#main').after('<div id="note"><form id="noteForm"></form></div>');

		$('#resizeMe').Resizable(
			{
				minWidth: 50,
				minHeight: 50,
				maxWidth: 500,
				maxHeight: 400,
				minTop: 123,
				minLeft: 58,
				maxRight: 500 + 58,
				maxBottom: 400 + 123,
				dragHandle: true,
				onDrag: function(x, y)
				{
					this.style.backgroundPosition = '-' + (x - 50) + 'px -' + (y - 50) + 'px';
					select_x = x - 58;
					select_y = y - 123;

//					show_coords();
					update_noteForm();
				},
				handlers: {
					se: '#resizeSE',
					ne: '#resizeNE',
					nw: '#resizeNW',
					sw: '#resizeSW'
				},
				onResize : function(size, position) {
					this.style.backgroundPosition = '-' + (position.left - 50) + 'px -' + (position.top - 50) + 'px';
					select_x = position.left - 58;
					select_y = position.top - 123;

					select_w = size.width;
					select_h = size.height;

//					show_coords();
					update_noteForm();
				}
			}
		)
	$('#AdvTagging fieldset').hide().after('<div id="TagPeople"></div>');
	$('#TagPeople').load('/static/snipets/people.html');

	update_noteForm();
}

function update_noteForm ()
{
	var x = select_x + 58;
	var y = select_y + select_h + 123 + 15;

	$('#wx').val(select_x);
	$('#wy').val(select_y);

	$('#ww').val(select_w);
	$('#wh').val(select_h);

	$('#wID').val(MediaID);
}
function show_coords ()
{
//	$('#coords').text('Hello');
	alert(select_w + "x" + select_h + " @ " + select_x + "," + select_y);
}

function fsShowDiv (link)
{
    $(link).hide();
    
    var fs = link.parentNode.parentNode;
    var div = fs.childNodes[1];
    
    $(div).slideDown();
}
function SubmitForm (form)
{
    var data = $('#' + form.id + ' input').serialize();

    $.ajax({ data: data, type: "GET", url: form.action, dataType: 'script' });

    return false;
}
function UpdateArea (area, html)
{
    $(area).empty();
    $(area).append(html);
    $(area).fadeIn();
}

//function SubmitFormSuccess (json)
//{
//    if (json.function == 'login') {
//        BlackScreen(json.more);
//    } else if (json.function == 'tags') {
//        UpdateArea('#TagsArea', json.more);
//    } else if (json.function == 'alert') {
//        alert (json.more);
//    } else {
//        alert ('Unknown');
//    }
//}

function StopAA ()
{
    $('#aactive').hide();
}

function UpdateMenuContent (html)
{
    $('#menubot').empty();
    $('#menubot').hide();
    $('#menubot').append(html);
    $('#menubot').fadeIn();
    $('#aactive').hide();
}
function BlackScreen (html)
{
    var BlackScreen = $('#main').append('<div id="BlackScreen"></div>');
    $('#BlackScreen').append(html);
    $('#BlackScreen').hide();
    $('#BlackScreen').fadeIn();
}
function HelpLink (topic)
{
    if (topic == 'null') {
    
    } else {
        alert('Help topic: ' + topic);
    }
}

function ExeConsole()
{
    eval(document.getElementById('TXTConsole').value);

    return false;
}
function MenuLink (command)
{
    if (command == 'Search') {
        var html = '<form action="/search" method="post"><label>Find: <input type="text" name="q" id="txtSearch" /></label> <input type="submit" name="act" value="Search" id="butSearch" /><p class="SmallNote"><a href="/search?ui=advanced">Advanced Interface</a></p></form>';
        UpdateMenuContent(html);
    } else if (command == 'Add Tags') {
        var id = document.getElementById('hiddenid').value;
        var form = '<form action="/edit" method="post" id="formTags" onsubmit="return SubmitForm(this);"><label>Tags: <input type="text" name="tags" id="txtTags" /><input type="text" name="id" value="' + id + '" /></label> <input type="submit" value="Save" name="act" /></form>';
        form = '<form action="/edit" method="post" id="formTags" onsubmit="return SubmitForm(this);"><input type="hidden" name="fileid" value="' + id + '" /><label>Tags: <input type="text" name="tags" /></label><input type="submit" value="Save" name="act" /></form>';
        $('#TagsArea').empty();
        $('#TagsArea').append(form);
        $('#TagsArea').fadeIn();
    } else if (command == 'C') {
        var html = '<form onsubmit="return ExeConsole()"><label>Console: <input type="text" name="console" id="TXTConsole" /><input type="submit" value="Execure" /></form>';
        UpdateMenuContent(html);
    } else if (command == 'Upload') {
        var html = '<form action="/upload" method="post" enctype="multipart/form-data"><label><input type="file" name="upfile" /><input type="submit" value="Upload" id="butUpload" name="act" /></form>';
        UpdateMenuContent(html);
        
        $('#butUpload').click(function() { $('#aactive').fadeIn(); alert('This may take a few minutes depending on how badly your upload speed sucks, please be patient.'); });
    } else if (command == 'Login') {
        var html = '<form action="/hgo.cgi" method="post" id="formLogin" onsubmit="return SubmitForm(this);"><label>Username: <input type="text" name="user" id="txtUser" /></label> <input type="submit" name="act" value="Login" id="butLogin" /><p class="SmallNote">Supports: hackgod.org, OpenID and Durpal</p></form>';
        UpdateMenuContent(html);
    } else if (command == 'Test') {
        // var Media = document.getElementById('sideMedia').childNodes[0];
        // $(Media).slideUp();
        
        FadeTopMediaOut();
    } else {
        $('#aactive').show();
        $.ajax({ type: "GET", url: "/ajax?menu=" + command, dataType: "script" })
    }
}

function FadeTopMediaOut ()
{
    var MediaArea = document.getElementById('sideMedia');
    $(MediaArea.childNodes[0]).slideUp();

//    setTimeout('FadeTopMediaOut();', 5000);
    setTimeout('RotateMedia();', 1000);
}

function RotateMedia ()
{
    var MediaArea = document.getElementById('sideMedia');
    var MediaThumb = MediaArea.childNodes[0];
    
    MediaArea.appendChild(MediaThumb);
    $(MediaThumb).fadeIn();
}

