$(document).ready(function() { 
/**********************************************************************/
// ENDE DOCUMENT START

/* PNGFIX */
$('.pngfix, .pngfix img').ifixpng();  

/* SITES LIST */
$('div.box h3.category').click(function () { 
	
	$(this).next().next().slideToggle(100);
	$(this).toggleClass("close");
	
	$('h3.category').each(function (i) {
		
		//alert('cat_'+i);
		if($('div.box').find('h3.category').eq(i).hasClass("close") == true) {
			$.session('cat_'+i, 'true');
		} else {
			$.session('cat_'+i, 'false');
		}
	});
		
});

// AUS DER SESSION HOLEN
$('div.box h3.category').each(function (i) {
	
	if($.session('cat_'+i) == 'true') {
		$('div.box').find('h3.category').eq(i).toggleClass("close");
		$('div.box').find('h3.category').eq(i).next().next().hide();
	}
	
});

// IMAGE POP
function viewLargerImage(href,image) {
	var src = href;
	var title = image.attr('alt');

	var img = $('<div><img alt="'+title+'" width="450" height="450" src="' + src +'" /></div>');

	setTimeout(function() {
		img.dialog({
			title: title,
			width: 450,
			height: 486, 
			modal: true
		});
	}, 1);
}


$('.image_pop').click(function() {
	var item = $(this).children('img');
	var href = $(this).attr('href');

	viewLargerImage(href,item);
	
	return false;
});

// VIDEO POP


$('.video_pop').click(function() {
	var video_src = $(this).attr('href');
	var title = $(this).attr('title');
	var video = $('<div><object width="450" height="278"><param name="movie" value="' + video_src + '" /><embed src="' + video_src + '" width="450" height="278" /></object></div>');
	$(video).dialog({
		title: title,
		width: 450,
		height: 314, 
		modal: true
	}); 
	
//	alert(video_src);
	
	return false;
});


// Add Feed
	$(function() {
		
		var url = $("#url"),
			tips = $(".validateTips");
			
		var allFields = $([]).add(url);
		
		var name = $("#name"),
			sub = $("#sub"),
			com = $("#com");
		
		var allComFields = $([]).add(name).add(sub).add(com);


		function updateTips(t) {
			tips.text(t).effect("highlight",{},1500);
		}

		function checkRegexp(o,regexp,n) {

			if ( !( regexp.test( o.val() ) ) ) {
				o.addClass('ui-state-error');
				updateTips(n);
				return false;
			} else {
				return true;
			}

		}
		
		function checkLength(o,n,min) {

			if ( o.val().length < min ) {
				o.addClass('ui-state-error');
		//		updateTips("Naja, wenigstens ein kurzes Feedback wäre nett.");
				return false;
			} else {
				return true;
			}

		}

		
		/* NEW COMMENT*/ 
		
		$("#new_comment").dialog({
			
			bgiframe: true,
			autoOpen: false,
			height: 330,
			modal: true,
			buttons: {
				Los: function() {
					var bValid = true;
					allFields.removeClass('ui-state-error');
					
					bValid = bValid && checkLength(com,"Kommentar",2);

					
					if (bValid) {
						
						var name_val = name.val();
						var sub_val = sub.val();
						var com_val = com.val();
						
						//Forular absenden
						$.ajax({
						type: "POST",
						url: "dialogs/process.php",
						data: "name=" + name_val + "&sub=" + sub_val + "&com=" + com_val + '&action=new_comment',
						success: 
						function(t) {
							$('.sug_com').removeClass('hidden');
							$('.sug_com').append('<p class="mb5">' + sub_val + ' <i class="fs10 c_green"> (gesendet)</i></p>'); 
						}, error:
						function() {
							$('.sug_com').removeClass('hidden');
							$('.sug_com').append('<p class="mb5">' + sub_val + ' <i class="fs10 c_red"> (fehler)</i></p>');
						}
						}); 
						
						
						$(this).dialog('close');
					}
				
				},
				Schließen: function() {
					$(this).dialog('close');
				}
			},
			close: function() {
				allComFields.val('').removeClass('ui-state-error');
			}
		});
		
		/* NEW URL */
		
		$("#new_url").dialog({
			
			bgiframe: true,
			autoOpen: false,
			height: 200,
			modal: true,
			buttons: {
				'Vorschlagen': function() {
					var bValid = true;
					allFields.removeClass('ui-state-error');
					
					bValid = bValid && checkRegexp(url,/(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/,"bsp.: http://www.diedomain.de");
					
					if (bValid) {
						
						var url_val = url.val();
						
						//Forular absenden
						$.ajax({
						type: "POST",
						url: "dialogs/process.php",
						data: "url=" + url_val + '&action=new_url',
						success: 
						function(t) {
							$('.sug').removeClass('hidden');
							$('.sug').append('<p class="mb5">' + url_val + ' <i class="fs10 c_green"> (gesendet)</i></p>'); 
						}, error:
						function() {
							$('.sug').append('<p class="mb5">' + url_val + ' <i class="fs10 c_red"> (fehler)</i></p>');
						}
						}); 
						
						
						$(this).dialog('close');
					}
				
				},
				Schließen: function() {
					$(this).dialog('close');
				}
			},
			close: function() {
				allFields.val('').removeClass('ui-state-error');
			}
		});
		
		
		
		$('.add_feed').click(function() {
			$('#new_url').dialog('open');
			return false;
		});
		
		$('.add_comment').click(function() {
			$('#new_comment').dialog('open');
			return false;
		});

	});
	
/* Check / Uncheck All Feeds */

	
	// Check ALL
	$('a.checkAll').click(function() {
			$.ajax({
						type: "POST",
						url: "dialogs/process.php",
						data: 'action=checkAll',
						success: 
						function(t) {
							location.reload();
						}, error:
						function() {
							
						}
			}); 
			return false;
	});
	
	// unCheck ALL
	$('a.unCheckAll').click(function() {
			$.ajax({
						type: "POST",
						url: "dialogs/process.php",
						data: 'action=unCheckAll',
						success: 
						function(t) {
							location.reload();
						}, error:
						function() {
							
						}
			}); 
			return false;
	});
	
	

/* Check Right Menu */
$('ul.sites_list li').rightMenu({
		opacity:0.8,
		query: "#myMenu"
});

/* Check Menu */
$('ul.sites_list li span').showMenu({
			opacity:0.9, 
			div: '#myMenu',
			query: 'dialogs/right_click_menu.php'
});


	



/**********************************************************************/
// ENDE DOCUMENT READY
}); 

/********************************************/
// Show Menu Function
(function($) {
	$.fn.rightMenu = function(options) {
		var opts = $.extend({}, $.fn.rightMenu.defaults, options);
		$(this).bind("contextmenu",function(e){

			$(opts.query).show().css({
				top:e.pageY+"px",
				left:e.pageX+"px",
				position:"absolute",
				opacity: opts.opacity,
				zIndex: opts.zindex
			}).load('dialogs/right_click_menu.php?id='+$('input',this).val());
			return false;
		});
		$(document).bind("click",function(e){
			$(opts.query).hide();
		});
	};
	
	$.fn.rightMenu.defaults = {
		zindex: 2000,
		query: document,
		opacity: 1.0
	};
})(jQuery);

/********************************************/
// Show Menu Function
(function($) {
	$.fn.showMenu = function(options) {
		var opts = $.extend({}, $.fn.showMenu.defaults, options);
		
		$(this).click(function(e){
			$(opts.div).show().load(opts.query + '?id='+$(this).prev('input').val()).css({
				top:(e.pageY-70)+"px",
				left:(e.pageX-70)+"px",
				position:"absolute",
				opacity: opts.opacity,
				zIndex: opts.zindex
			});
			return false;
		});
		$(document).bind("click",function(e){
			$(opts.div).hide();
		});
	};
	
	$.fn.showMenu.defaults = {
		zindex: 2000,
		div: document,
		query: document,
		opacity: 1.0
	};
})(jQuery);

// SONSTIGES JAVASCRIPT

function fbs_click() {
	u=location.href;
	t=document.title;
	
	window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
	return false;
}

function contact(pt1, pt2) {
window.location = "mailto:" + pt1 + "@" + pt2;
}