$.fn.swap = function(b) {
	if($(b).length > 0) {
		b = jQuery(b)[0];
		var a = this[0],
		a2 = a.cloneNode(true),
		b2 = b.cloneNode(true),
		stack = this;

		a.parentNode.replaceChild(b2, a);
		b.parentNode.replaceChild(a2, b);

		stack[0] = a2;
		return this.pushStack( stack );
	}
	else return this.pushStack( this );
};

function GetCount(){
	dateFuture = new Date(2009,11,12,16,00,00);
	dateNow = new Date(); //grab current date
	amount = dateFuture.getTime() - dateNow.getTime(); //calc milliseconds between dates
	delete dateNow;

	if (amount < 0) {
		document.getElementById('startseite_countbox').innerHTML = "Now!";
	}
	else {
		days = 0;
		hours = 0;
		mins = 0;
		secs = 0;
		out = "";

		amount = Math.floor(amount / 1000);//kill the "milliseconds" so just secs
		days = Math.floor(amount / 86400);//days
		amount = amount % 86400;

		hours = Math.floor(amount / 3600);//hours
		amount = amount % 3600;

		mins = Math.floor(amount / 60);//minutes
		amount = amount % 60;

		secs = Math.floor(amount);//seconds
		out += days + " Tag" + ((days != 1) ? "e" : "") + " &nbsp;|&nbsp; ";
		if (hours < 10) {
			out += "0" + hours + " : ";
		}
		else {
			out += hours + ":";
		}
		if (mins < 10) {
			out += "0" + mins + ":";
		}
		else {
			out += mins + ":";
		}
		if (secs < 10) {
			out += "0" + secs;
		}
		else {
			out += secs;
		}
		document.getElementById('startseite_countbox').innerHTML = out;

		setTimeout("GetCount()", 1000);
	}
}
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

$(function(){

	$("#userbar_left_wmdg, #flipster_userbar_mybox_wmdg").bind("click", function(){
		$('#userbar_left_wmdg_sub1, #flipster_userbar_mybox_wmdg_sub1').toggle();
		$('#update_mood1').toggle();

	$("#update_mood1 textarea").val($(this).find('p.mood').text().trim());
		$(this).unbind();
		return false;
	});

	$("#userbar_right_activity_buttons a").click(function() {
		var actions = activities_selected.split("%")[0] || "";
		var names = activities_selected.split("%")[1] || "";

		var what = $(this).attr("rel").split(':')[1];
		var where = $(this).attr("rel").split(':')[0];

		if(where == "a") {
			if(actions.search(what) == -1) {
				if(actions!="")
					actions += ",";
				actions += what;
				$(this).parent().addClass("selected");
			} else {
				if(actions == what)
					actions = "";
				else if(actions.search("," + what) == -1)
					actions = actions.replace(what + "," , "");
				else
					actions = actions.replace("," + what, "");

				$(this).parent().removeClass("selected");
			}
		} else {
			if(names.search(what) == -1) {
				if(names!="")
					names += ",";
				names += what;
				$(this).parent().addClass("selected");
			} else {
				if(names == what)
					names = "";
				else if(names.search("," + what) == -1)
					names = names.replace(what + "," , "");
				else
					names = names.replace("," + what, "");

				$(this).parent().removeClass("selected");
			}
		}

		activities_selected = actions + "%" + names;
		newloc = "http://" + loc + "/?act=" + encodeURI(activities_selected);

		window.location = newloc;

		return false;
	});
	
	
	/** **********************************************************************/
	$("body").append('<div id="fotoalben_widget_mini_box"></div>');
	$("#fotoalben_widget_mini_box").css('z-index', '9999').css('position', 'absolute').hide();

	$(".fotoalben_widget_albuminfo").css('z-index', '9999').css('position', 'absolute').hide();
	
	jQuery(".widget_bp_album_startseite_widget img").livequery('mouseenter', function() {
		jQuery("#fotoalben_widget_mini_box").html(jQuery("#box_"+jQuery(this).attr("id")).html()).show();
	}).livequery('mouseleave', function() {
		jQuery("#fotoalben_widget_mini_box").html("").hide();
	});
	
	/** **********************************************************************/

	$("body").append('<div id="banner_preview"></div>');
	$("#banner_preview").css('z-index', '9999');
	$("#banner_preview").css('position', 'absolute');
	$("#banner_preview").hide();

	$().mousemove(function(e) {
		if (e.pageY - $("#fotoalben_widget_mini_box").height() > 0) {
			$("#fotoalben_widget_mini_box").css('top', (e.pageY + 20).toString() + "px");
		}else{
			$("#fotoalben_widget_mini_box").css('top', "0px");
		}
		$("#fotoalben_widget_mini_box").css('left', (e.pageX + 20).toString() + "px");
		
		if (e.pageY - $("#banner_preview").height() > 0) {
			$("#banner_preview").css('top', (e.pageY - $("#banner_preview").height()).toString() + "px");
		}else{
			$("#banner_preview").css('top', "0px");
		}
		$("#banner_preview").css('left', (e.pageX -220).toString() + "px");
	});

	jQuery("#banner3 img").livequery('mouseenter', function() {
		jQuery("#banner_preview").html(jQuery(this).clone().css("width", "200px").addClass("big_banner")).show();
	}).livequery('mouseleave', function() {
		jQuery("#banner_preview").html("").hide();
	});

	jQuery("#banner2 img").livequery('mouseenter', function() {
		jQuery("#banner_preview").html(jQuery(this).clone().css("width", "200px").addClass("big_banner")).show();
	}).livequery('mouseleave', function() {
		jQuery("#banner_preview").html("").hide();
	});

	jQuery("div#events-banner-options div.btn").livequery('click',
		function() {
			jQuery('#ajax-loader-events-banner').toggle();

			jQuery.post( ajaxurl, {
				action: 'widget_events_banner',
				'cookie': encodeURIComponent(document.cookie),
				'_wpnonce': jQuery("input#_wpnonce-events-banner").val(),
				'timeframe': jQuery(this).attr('id')
			},
			function(response)
			{
				events_banner_wiget_response(response);
			});

			return false;
		});
	jQuery("div#user_info_box #tabs li:first-child").livequery('click', function() {
			jQuery("div#user_info_box #tabs li:last-child").removeClass("selected");
			jQuery("div#user_info_box #tabs li:first-child").addClass("selected");
			jQuery("div#user_info_box #tabs #tabs-2").css("display","block");
			jQuery("div#user_info_box #tabs #tabs-1").css("display","none");
	});
	jQuery("div#user_info_box #tabs li:last-child").livequery('click', function() {
			jQuery("div#user_info_box #tabs li:first-child").removeClass("selected");
			jQuery("div#user_info_box #tabs li:last-child").addClass("selected");
			jQuery("div#user_info_box #tabs #tabs-2").css("display","none");
			jQuery("div#user_info_box #tabs #tabs-1").css("display","block");
	});

	round_selector = Array("#submenu-album li",".border5",".big_banner");

	for(i=0; i<round_selector.length; i++)
	  $(round_selector[i]).css("-moz-border-radius", "5px")
						  .css("-webkit-border-radius", "5px")
						  .css("border-radius", "5px");


	jQuery("#sortable").sortable({
		stop: function(event, ui) {
			$.get("/save_album.php?" + jQuery("#sortable").sortable( 'serialize') + "&pg=" + album_pg);
		}
	});
	jQuery("#sortable").disableSelection();

	$("a.move_album_pos_next").live('click', function() {
		$(this).parent().parent().parent().parent().swap($(this).parent().parent().parent().parent().next("li"));
		$.get("/save_album.php?" + jQuery("#sortable").sortable( 'serialize') + "&pg=" + album_pg);
		return false;
	});

	$("a.move_album_pos_prev").live('click', function() {
		$(this).parent().parent().parent().parent().swap($(this).parent().parent().parent().parent().prev("li"));
		$.get("/save_album.php?" + jQuery("#sortable").sortable( 'serialize') + "&pg=" + album_pg);
		return false;
	});

	$("a.delete_album").live('click', function() {
		$.get($(this).attr("href"), {}, function() {

		});
		$(this).parent().parent().parent().parent().fadeOut(600);
		return false;
	});

	$("a.edit_album_td").live('click', function() {
		var $li = $(this).parent().parent().parent().parent();
		$li.find("input, textarea").toggle();
		$li.find(".the_description, .the_title").toggle();


		$li.find("form").submit(function() {
			$form = $li.find("form");
			$.post($(this).attr("action"), $(this).serialize(), function() {
				$form.find(".the_title").text($form.find("input[name='title']").val());
				$form.find(".the_title").toggle();
				$form.find("input[name='title']").val('');
				$form.find("input").toggle();
				$form.find(".the_description").text($form.find("textarea").val());
				$form.find(".the_description").toggle();
				$form.find("textarea").val('').toggle();
			});
			return false;
		});

		return false;
	});

	jQuery("#sortable_img").sortable({
		stop: function(event, ui) {
			$.get("/save_img.php?" + jQuery("#sortable_img").sortable( 'serialize') + "&pg=" + album_pg);
		}
	});
	jQuery("#sortable_img").disableSelection();

	$("a.move_img_pos_next").live('click', function() {
		$(this).parent().parent().parent().swap($(this).parent().parent().parent().next("li"));
		$.get("/save_img.php?" + jQuery("#sortable_img").sortable( 'serialize') + "&pg=" + album_pg);
		return false;
	});

	$("a.move_img_pos_prev").live('click', function() {
		$(this).parent().parent().parent().swap($(this).parent().parent().parent().prev("li"));
		$.get("/save_img.php?" + jQuery("#sortable_img").sortable( 'serialize') + "&pg=" + album_pg);
		return false;
	});

	$("a.delete_img").live('click', function() {
		$.get($(this).attr("href"), function(data) {


		});
		$(this).parent().parent().parent().fadeOut(600);
		return false;
	});

	$("a.edit_img_td").live('click', function() {
		var $li = $(this).parent().parent();
// 		console.log($li);
		$li.find("input").toggle();
		$li.find(".the_title").toggle();


		$li.find("form").submit(function() {
			$form = $li.find("form");
			$.post($(this).attr("action"), $(this).serialize(), function() {
				$form.find(".the_title").text($form.find("input[name='title']").val());
				$form.find(".the_title").toggle();
				$form.find("input[name='title']").val('');
				$form.find("input").toggle();
			});
			return false;
		});

		return false;
	});

	// $("a.change_pic").live('click', function() {
	// 	$("#the_pic").attr("src", $(this).attr("rel"));
	// 	$("#the_pic").attr("name", $(this).attr("name"));
	// 	$(this).parent().find('a').removeClass('current-picture');
	// 	$(this).addClass('current-picture');

        //         $('#friend-link-list').empty();
        //         $('#friend-link-list').append('<li>Verlinkte Personen: </li>');

        //         if (!friend_links[$(this).attr("name")])
        //           return false;

        //         for (var n = 0; n < friend_links[$(this).attr("name")].length; ++n)
        //           $('#friend-link-list')
        //             .append('<li><a href="'
        //                     + friend_links[$(this).attr("name")][n].href + '">'
        //                     + friend_links[$(this).attr("name")][n].name
        //                     + '</a></li>');

	// 	// update_friend_link_list($(this).attr("name"));

	// 	return false;
	// });

	$("#save_event_location").click(function() {
		$.get('/save_event_location.php', {
			event: $('#event :selected').val(),
			loc: 0, /*$('#location :selected').val()*/
			id: album_id
		}, function() {
			$('#zuweisen').hide();
		});
	});
	
	$("#create_new_album_form").live("submit", function() {
		if($(this).find("input[type='text']").val().trim() == "Profilbilder") {
			alert("Es darf nur ein Album mit dem Namen profilbilder geben");
			return false;
		}
	});
	
	
	var config = {    
		sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)    
		interval: 200, // number = milliseconds for onMouseOver polling interval    
		over: function() {
// 			$("#notify-me").slideDown(300);
			$("#notify-me").show();
		},
		timeout: 500, // number = milliseconds delay before onMouseOut    
		out: function() {
// 			$("#notify-me").slideUp(300);
			$("#notify-me").hide();
		}
	};
	
	jQuery("#notifications_menu").hoverIntent(config);
	
// 	if($.browser.opera) {
// 		$("#notifications_menu").addClass("opera_fix");
// 	}
	
// 	jQuery("#notifications_menu").hoverIntent(function() {
// 		$(this).parent().find("ul").slideDown(300);
// 	}, function() {
// 		$t = $(this).parent().find("ul");
// 		$(this).parent().find("ul").slideUp(300);
// 		setTimeout("$t.slideUp(300)", 300);
// 	});
	
	$("#eBann").bind("keypress", function(e) {
		if (e.keyCode == 13) {
			$(this).parent().submit();
			return false;
		}

		return true;
	});

});


function getObject(obj) {
  var theObj;
  if(document.all) {
    if(typeof obj=="string") {
      return document.all(obj);
    } else {
      return obj.style;
    }
  }
  if(document.getElementById) {
    if(typeof obj=="string") {
      return document.getElementById(obj);
    } else {
      return obj.style;
    }
  }
  return null;
}

function toCount(entrance,exit,text,characters) {
  var entranceObj=getObject(entrance);
  var exitObj=getObject(exit);
  var length=characters - entranceObj.value.length;
  if(length <= 0) {
    length=0;
    text='<span class="disable"> '+text+' </span>';
    entranceObj.value=entranceObj.value.substr(0,characters);
  }
  exitObj.innerHTML = text.replace("{CHAR}",length);
}


function entsub(event,ourform) {
  if (event && event.which == 13) {
    ourform.submit();
	return false;
  }
  else
    return true;}
