// ------------------------------------
//  Highlight Comment - Form
// -------------------------------------
$(function() { 
    $("div.entry").bind("click keydown", function() { 

        $(this).expose({ 

            // custom mask settings with CSS 
            maskId: 'mask', 
			color: '#000', 
		    opacity: 0.7,

            onLoad: function() { 
				var cssObj = {
			        'background-color' : '#FFFFFF'
			     }
                this.getExposed().css(cssObj); 
            }, 

            onClose: function() { 
				var cssObj = {
			        'background-color' : 'transparent'
			     }
                this.getExposed().css(cssObj); 
            }, 

            api: true 
        }).load(); 
    }); 
});				
// ------------------------------------
//  EMBED - Videos - Extern
// -------------------------------------
$(document).ready(function() { 
	$("a.embed").click(function(){
		var supportedvideos = new Array("youtube", "sevenload", "metacafe", "vimeo", "myvideo", "clipfish", "google","collegehumor");
		var linkclass = $(this).attr('class');
		var linkclass = linkclass.replace("embed ", "");
		if ( jQuery.inArray( linkclass, supportedvideos ) != -1)
		{
			var id = $(this).attr('id');
			var objclass = "embed" + id;
			var obj = $(objclass);
			if ( $('div.' + objclass).length )
			{
				var iheight = $('div.'+ objclass).height() * -1;
				$('div.' + objclass).remove();
				cols_resize_col1(iheight);
			} else 
			{
				$(this).after(
					"<div class='comment_video " + objclass + "'></div>"
				);
				$("div." + objclass).load("http://" + document.domain + "/video/embed/" + linkclass + "/" + id + "/" + objclass +"/")
				cols_resize_col1($('div.'+objclass).height());
			}
			return false;
		} else 
		{
			return true;
		}
	});
});	
// ------------------------------------
//  Videos - bildschirmarbeiter
// -------------------------------------
$(document).ready(function() { 
	$("a.bildschirmarbeiter").click(function(){
		var id = $(this).attr('id');
		var id2 = id.replace(/----/g, ".");
		var objclass = "s" + id;
		
		var obj = $(objclass);
		
		
		if ( $('div.' + objclass).length )
		{
			var iheight = $('div.'+objclass).height() * -1;
			$('div.' + objclass).remove();
			//cols_resize_col1(iheight);
		} else 
		{
			$(this).after(
				"<div class='comment_video " + objclass + "'>" + 
				"<div class='bavideo" + objclass + "'></div>" + 
				"<p><a class='tu_iframe_660x380' href='/video/cvideo/" + id + "/'>Video vergrössern</a> | " + 
				"<a href='/video/" + id +"/' target='_blank'>Bildschirmarbeiter-Link</a>" +
				" | <a href='#' onclick='remove_video(\"" + objclass +  "\"); return false;'>Video ausblenden</a>" +
				"</p>" + 
				"</div>"
			);
			$("div.bavideo" + objclass).load("http://" + document.domain + "/video/cemb/" + id2 + "/")
			cols_resize_col1($('div.'+objclass).height());
		}
		return false;
	});
});




function remove_video(objclass)
{
	if ( $('div.' + objclass).length )
	{
		var iheight = $('div.'+objclass).height() * -1;
		$('div.' + objclass).remove();
	} 
	return false;
}


$(document).ready(function() {
    $("a.image_preview").tooltip({
        // a little tweaking of the position
        offset: [-40, -300],
        // use the built-in fadeIn/fadeOut effect
        effect: "fade",
        // custom opacity setting
        opacity: 0.9,
        // use this single tooltip element
        tip: '.image_tooltip',
        //
        onBeforeShow: function(test,pos) {
            var el = this.getTrigger();
            var rel = el.attr( 'rel' );
            var img = $("#" + rel);
            var src = img.attr( 'src' );
            var width = img.attr( 'width' );
            var height = img.attr( 'height' );
            var div = width / height;
            var newheight = 150 / div;
            $("div.image_tooltip").html("<img src=\"" + src + "\" width=\"" + 150 + "\" height=\"" + newheight + "\"/>");
        }
    });
});

//Favoriten
$().ready(function() {
	$('a.Favorites_Trigger').click (function() {
		$.get(this.href, function(data) {
			$('div.favoritesul').html(data);
		});
		return false;
	});
});

//Tags
$().ready(function() {
	$('a.tags_add').click (function() {
		if ( $(this).text() == "( neue Tags hinzufügen )")
		{
			$(this).text("( Tageingabe schliessen )");
			$('a.tags_add').after("<div class='ftags_add'</div>");
			$.get(this.href, function(data) {
				$('div.ftags_add').html(data);
				$('#my_tag_form').ajaxForm(function() {
					url = $(this).attr("url");
					url = url.replace("tags_add","tags_show");
					$.get(url, function(data) {
						$('div.tags').html(data);
					});
					$('div.ftags_add').remove();
					$(this).text("( Tageingabe schliessen )");
			    });
			});
			
		} else 
		{
			$(this).text("( neue Tags hinzufügen )");
			$('div.ftags_add').remove();
		}
		return false;
	});
});

//Kommentarschreiben
$().ready(function() {
	$('#writecomment').click (function() {
		if ( $("#commentform").length )
		{
			var obj = $("#commentform").clone();
			var prev = $('#commentform').prev();
			obj.click( $("#commentform").data("events").click );
			$('#commentform').remove();
			$(this).after(obj);
			var iheight = $("commentform2").height();
			cols_resize_col1(iheight);
		}
		return false;
	});
});

// ------------------------------------
//  Edit Comment Box
// -------------------------------------
function edit_comment(comment_id)
{
	var ourBox = document.getElementById('edit_comment_box_'+comment_id);
	
	if (ourBox.style.display == 'none') {
		ourBox.style.display = 'block';
		$('div#col1').height($("#col1").height() + $("div.editCommentBox").height());
		$('div#col2').height($("#col2").height() + $("div.editCommentBox").height());

	}
	else {
		ourBox.style.display = 'none';
		$('div#col1').height($("#col1").height() - $("div.editCommentBox").height());
		$('div#col2').height($("#col2").height() - $("div.editCommentBox").height());
	}
}

// ------------------------------------
//  Handles the AJAX Requests
// -------------------------------------    
function submit_edited_comment(comment_id)
{
	try
	{
		XMLHttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (e)
	{
		try
		{
			XMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (e2)
		{
			XMLHttp = null;
		}
	}
	
	if( ! XMLHttp && typeof XMLHttpRequest != "undefined")
	{
		XMLHttp = new XMLHttpRequest();
	}
	
	if ( ! XMLHttp)
	{
		alert('Browser Not Support'); return false;
	}
	
	var bleah = document.getElementById('edit_comment_box_'+comment_id);
	
	var content = bleah.getElementsByTagName('textarea')[0].value;
	
	var data =  "comment_id=" + encodeURIComponent(comment_id) + 
				"&comment=" + encodeURIComponent(content);
	
	XMLHttp.onreadystatechange = function () { result_of_comment_edit(comment_id); }
	XMLHttp.open("POST", "http://www.bildschirmarbeiter.com/global/edit_comment/", true);
	XMLHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	XMLHttp.setRequestHeader("Connection", "close");
	XMLHttp.send(data);
}


function result_of_comment_edit(comment_id)
{
	if (XMLHttp.readyState == 4)
	{
		document.getElementById('edit_comment_box_'+comment_id).style.display = 'none';
	
		if (XMLHttp.status != 200) 
		{
			alert("An error was encountered: " + XMLHttp.status);
			return;
		}
		
		var response = XMLHttp.responseText;
		
		if (response == 'null')
		{
			alert('Invalid Comment Edit');
			return;
		}
		
		document.getElementById('comment_box_' + comment_id).innerHTML = response;
	}
}
