/**
 * 
 * 
 * 
 */

var lastPlayed = null;
var opened = null;

Object.extend(Prototype.Browser, {
     WebKit419: Prototype.Browser.WebKit && (Prototype.BrowserFeatures.XPath),
     WebKit420: Prototype.Browser.WebKit && (!Prototype.BrowserFeatures.XPath),
     IE6:      Prototype.Browser.IE && (typeof window.XMLHttpRequest == "undefined"),
     IE7:      Prototype.Browser.IE && (typeof window.XMLHttpRequest == "object")
});

document.observe("dom:loaded", function() {
    if(Prototype.Browser.IE6) {
        $('main').innerHTML ='<div id="intro"><h1>Crappy Browser</h1><p>You are using Internet Explorer 6 or lower. This site does not support Browsers older than 8 years. Well, it would work, but I don\'t want it to work. You are (hopefully) owning an amazing workstation - get a modern browser. Update to Internet Explorer 8 or use alternatives like Firefox, Opera, Safari or Chrome. If it isn\'t your fault - blame your admins.</p><p>If you are having problems viewing the site or dont agree with me, read <a href="http://iedeathmarch.org/">http://iedeathmarch.org/</a> or write me an email to info at navarin dot de</div>';
    }
});

/**
 * toggles login container
 * 
 * @return
 */
function toggleLogin() {
	$("login").toggle();
	$("login-user").focus();
}

/**
 * toggles description. Requested via ajax.
 * 
 * @param patch
 * @return
 */
function toggleDesc(id) {
	if(opened && opened != id) {
		$('entry-'+ opened).removeClassName('selected');
		$('desc-'+ opened).toggle();
	} else {
		opened = null;
	}
	
	if($('entry-'+ id).hasClassName('selected')) {
		$('entry-'+ id).removeClassName('selected');
		$('desc-'+ id).toggle();
	} else {
		$('loader-'+ id).innerHTML = '<img src="/img/loader.gif" style="vertical-align:middle" />';
		
		new Ajax.Request('/patches/details/'+ id, {
			method:'post',
			onSuccess: function(transport){
				var response = transport.responseText || "no response text";
				$('entry-'+ id).addClassName('selected')
				$('desc-'+ id).innerHTML = response;
				$('desc-'+ id).toggle();
				opened = id;
				$('loader-'+ id).innerHTML = "";
			},
			onFailure: function(){  }
		});
	}
}

/**
 * embeds a player in a hidden container and autostarts playing the given file
 * 
 * @param id
 * @return
 */
function playFile(id) {
	if(lastPlayed) {
		stopFile(lastPlayed);
	}
	lastPlayed = id;
	
    $('img-'+ id).src = "/img/stop.png";
    $('a-'+id).href = 'javascript:stopFile("'+ id +'")';
    
	var so = new SWFObject("/player.swf", "main", "1", "1", "9", "#FFFFFF");
	so.addVariable("musicPath", "/data/mp3/"+ id);
    so.write("flashcontent");
}

/**
 * stops playing a file
 * 
 * @param id
 * @return
 */
function stopFile(id) {
    $('img-'+ id).src = "/img/play.png";
    $('a-'+id).href = 'javascript:playFile("'+ id +'")';
    $('flashcontent').innerHTML = "";
}


/**
 * posts a commment to a patch
 * @param formId
 */
function postComment(formId) {
	toggleDesc(formId);
	$('form-'+ formId).request({
		parameters: {fileId:formId},
		onComplete: function(transport) { 
			toggleDesc(formId);
		}
	})	
}

/**
 * change the length marker when writing a comment.
 * disable submit button when comment is too long.
 * 
 * @param len
 * @return
 */
function change(len) {
	if(255 -len < 0) {
		$('submitButton').disable();
	} else {
		$('submitButton').enable();
	}
	$('chars').innerHTML = 255 -len;
}



function reportLink(link) {
	new Ajax.Request('/patches/report/'+ link, {
		method:'post',
		onSuccess: function(transport){
			$('reportLink').innerHTML = transport.responseText;
		},
		onFailure: function(){  }
	});
}

