jQuery(function($) {
   //scroll to anchor 
	//$('.alignleft').click(function(){$('#top').ScrollTo(400);return false});
  //$('.buttons').click(function(){$('#bottom').ScrollTo(400);return false});
   
   /*
	  $('a[@href*=#]').click(function() {
      if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
         var $target = $(this.hash);
         $target = $target.length && $target || $('[@name=' + this.hash.slice(1) +']');
         if ($target.length) {
           $target.ScrollTo(400);
           return false;
         }
      }
    });	
   */
    
	$(".btn-slide").click(function(){
      $.ajax({
      type: "GET",
      url: "friendfeed.php",
      data: "friend",
      success: function(msg){
       $('#friendfeed').html(msg); 
       $("#tabborder").slideToggle("slow");
   }
   });
		$(this).toggleClass("active"); return false;
	});
  
  $('a#friend_toggle').click(function() {
    $('#friend').slideToggle(300);
    return false;
  });
    
    /*
	$(".informationen .delete").click(function(){
	  $(this).parents(".informationen").animate({ opacity: "hide" }, "slow");
	});
    */
	$('a.delete').click(function(){
      var pmid = $(this).attr('id');
      $.ajax({
      type: "GET",
      url: "delete.php",
      data: "pmid=" + pmid,
      success: function(msg){
        $('#pmid_' + pmid).fadeOut('slow');
      }    
      });
	});

  $('a.p_friend').click(function() {

    var vid = $(this).attr('id');
    
    $.ajax({
    type: "GET",
    url: "save.php",
    data: "homie=" + vid,
    success: function(msg){   
		  $('#Freund').html(msg);  
    }    
    });
    return false;
  });

  jQuery(document).ready(function($) {
    $('a[rel*=facebox]').facebox()
  });
  
  $(".menu a").hover(function() {
	  $(this).next("em").animate({opacity: "show", top: "-75"}, "slow");
	}, function() {
	  $(this).next("em").animate({opacity: "hide", top: "-85"}, "fast");
	});

	//Configuration Options
	var max_width = 660; 	//Sets the max width, in pixels, for every image
	var selector = 'img'; 	//Sets the syntax for finding the images.  Defaults to all images.
				//For images inside of a particular element (id="abc") or class (class="abc"),
				//use '.abc img' or '#abc img' respectively.  Don't leave off the img tag!!!
	//End configuration options.  You don't need to change anything below here.
	
	$(selector).each(function(){
		var width = $(this).width();
		var height = $(this).height();
		if (width > max_width) {
			//Set variables	for manipulation
			var ratio = (height / width );
			var new_width = max_width;
			var new_height = (new_width * ratio);
			
			//Shrink the image and add link to full-sized image
			$(this).height(new_height).width(new_width);
			$(this).hover(function(){
				$(this).attr("title", "Dieses Bild wurde verkleinert. Klicke um es zu vergrssern.")
				$(this).css("cursor","pointer");
				});
			$(this).click(function(){
				window.location = $(this).attr("src");
				});
		} //ends if statement
	} //ends each function
	);

  $.fn.HoverImageText.defaults.AnimShow = {height: "show"}; 
  $.fn.HoverImageText.defaults.HoverCheck = 100;
  
  $('.imageLibrary a').HoverImageText();
    
  $('a#machtsinn_toggle').click(function() {
    $('#machtsinn').slideToggle("slow");

    z();
    return false;
  });
  
  $('a#f_schnellantwort').click(function() {
    $('#fast_answ').slideToggle("slow");
    z();
    return false;
  });  

  $('a.f_vote').click(function() {

    var vid = $(this).attr('id');
    
    $.ajax({
    type: "GET",
    url: "save.php",
    data: "ausz=" + vid,
    success: function(msg){   

      $('#ausz_' + vid).fadeIn('slow');
      $('#zahl_' + vid).html(msg);
    }    
    });
    return false;
  });
 
  $('img.g_del').click(function() {
   var name = confirm("Bist du sicher, dass du dieses Foto löschen willst?")
		if (name == true)
		{
      var pid = $(this).attr('id');
      
      $.ajax({
      type: "GET",
      url: "delete.php",
      data: "picid=" + pid,
      success: function(msg){    
        $('#picid_' + pid).fadeOut('slow');
      }    
      });
    }
    return false;
  });

   $('.okay').hover(function() {
      $(this).fadeOut('slow');
    });

  $('img.g_edit').click(function() {
    $.facebox('<iframe src ="edit_photo.php?id=' + $(this).attr('id') +'" width="450" height="350"> </iframe>')      
    return false;
  });

  $("a#menu-call").mouseover( function() {
      $("span#gmenu").slideToggle("slow"); 
   });
  
}); //close doc ready

$(function()
{
    var hideDelay = 300;
    var currentID;
    var hideTimer = null;
    var ajax = null;
    var hideFunction = function()
    {
        if (hideTimer)
            clearTimeout(hideTimer);
        hideTimer = setTimeout(function()
        {
            currentPosition = { left: '0px', top: '0px' };
            container.css('display', 'none');
        }, hideDelay);
    };

    var currentPosition = { left: '0px', top: '0px' };

    // One instance that's reused to show info for the current person
    var container = $('<div id="personPopupContainer">'
        + '<table width="" border="0" cellspacing="0" cellpadding="0" align="center" class="personPopupPopup">'
        + '<tr>'
        + '   <td class="corner topLeft"></td>'
        + '   <td class="top"></td>'
        + '   <td class="corner topRight"></td>'
        + '</tr>'
        + '<tr>'
        + '   <td class="left">&nbsp;</td>'
        + '   <td><div id="personPopupContent"></div></td>'
        + '   <td class="right">&nbsp;</td>'
        + '</tr>'
        + '<tr>'
        + '   <td class="corner bottomLeft">&nbsp;</td>'
        + '   <td class="bottom">&nbsp;</td>'
        + '   <td class="corner bottomRight"></td>'
        + '</tr>'
        + '</table>'
        + '</div>');

    $('body').append(container);

    $('.personPopupTrigger').live('mouseover', function()
    {
        if (!$(this).data('hoverIntentAttached'))
        {
            $(this).data('hoverIntentAttached', true);
            $(this).hoverIntent
            (
                // hoverIntent mouseOver
                function()
                {
                    if (hideTimer)
                        clearTimeout(hideTimer);

                    // format of 'rel' tag: pageid,personguid
                    var settings = $(this).attr('rel').split(',');
                    var pageID = settings[0];
                    currentID = settings[1];

                    // If no guid in url rel tag, don't popup blank
                    if (currentID == '')
                        return;

                    var pos = $(this).offset();
                    var width = $(this).width();
                    var reposition = { left: (pos.left + width) + 'px', top: pos.top - 5 + 'px' };

                    // If the same popup is already shown, then don't requery
                    if (currentPosition.left == reposition.left &&
                        currentPosition.top == reposition.top)
                        return;

                    container.css({
                        left: reposition.left,
                        top: reposition.top
                    });

                    currentPosition = reposition;

                    $('#personPopupContent').html('&nbsp;');

                    if (ajax)
                    {
                        ajax.abort();
                        ajax = null;
                    }

                  $.ajax({
                     type: 'GET',
                     url: 'user_clue.php',
                     data: 'anzeige=' + currentID + '&id=' + pageID, 
                     success: function(data)
                     {
                        var text = $(data).html();
                        $('#personPopupContent').html(text);
                     }
                    });

                    container.css('display', 'block');
                },
                // hoverIntent mouseOut
                hideFunction
            );
            // Fire mouseover so hoverIntent can start doing its magic
            $(this).trigger('mouseover');
        }
    });

    // Allow mouse over of details without hiding details
    $('#personPopupContainer').mouseover(function()
    {
        if (hideTimer)
            clearTimeout(hideTimer);
    });

    // Hide after mouseout
    $('#personPopupContainer').mouseout(hideFunction);
});

function smcheck() {

 img2 = null;
 ids = null;
 initajax();
 if(xmlhttp){

  xmlhttp.open("GET", "show_chat.php");
  xmlhttp.onreadystatechange = function() {

   if (xmlhttp.readyState == 4 && xmlhttp.status == 200 && xmlhttp.responseText != "null") {
    var id = null;
    ids = xmlhttp.responseText;

    id=ids.split("<asdasdasdasdasd>");
    var posx = null;
    var posy = null;
    for (var i = 0; i < id.length-1; i+=2) {
      //initsm(id[i]);
      id[i] = parseInt(id[i]);
      var newsmdiv =null;
      var img2=document.getElementById('sm'+id[i]);

      var newbodytitle = "Chat Einladung";
      document.title='Chat Einladung - realone.ch';

      if(img2 == null)
      {

       var newsmdiv =document.createElement('div');
       newsmdiv.setAttribute("class", "sm");
       newsmdiv.setAttribute("className", "sm");
       newsmdiv.setAttribute("id", "sm"+id[i]);
       document.getElementsByTagName('body')[0].appendChild(newsmdiv);

       //var zufallsz = null;
       //zufallsz = 1 + 150*(Math.random());
       //zufallsz = Math.round(zufallsz);

       var nx=(screen.width-620)/2+posx;
       var ny=(screen.height-450)/2-150+posy;

       var img2=document.getElementById('sm'+id[i]);
       img2.style.top = ny+"px";
       img2.style.left = nx+"px";
       if(posx <= 150)
       {
        posx = posx +15;
        posy = posy +15;
       }
       else
       {
        posx = 0;
        posy = 0;
       }
      }

      document.getElementById('sm'+id[i]).innerHTML=id[i+1];

      img2.style.display="block";

     }
   }
  }
  xmlhttp.send(null);
 }
 window.setTimeout("smcheck()", 100000);
}

function z() {
  for (i=1; i<40; i++) {
    window.scrollBy(0, 5);
  }
}

//////////////////// scroll by jquery ////////////////////////////////////////////////////////////////////////////
jQuery.getPos = function (e)
{
	var l = 0;
	var t  = 0;
	var w = jQuery.intval(jQuery.css(e,'width'));
	var h = jQuery.intval(jQuery.css(e,'height'));
	var wb = e.offsetWidth;
	var hb = e.offsetHeight;
	while (e.offsetParent){
		l += e.offsetLeft + (e.currentStyle?jQuery.intval(e.currentStyle.borderLeftWidth):0);
		t += e.offsetTop  + (e.currentStyle?jQuery.intval(e.currentStyle.borderTopWidth):0);
		e = e.offsetParent;
	}
	l += e.offsetLeft + (e.currentStyle?jQuery.intval(e.currentStyle.borderLeftWidth):0);
	t  += e.offsetTop  + (e.currentStyle?jQuery.intval(e.currentStyle.borderTopWidth):0);
	return {x:l, y:t, w:w, h:h, wb:wb, hb:hb};
};
jQuery.getClient = function(e)
{
	if (e) {
		w = e.clientWidth;
		h = e.clientHeight;
	} else {
		w = (window.innerWidth) ? window.innerWidth : (document.documentElement && document.documentElement.clientWidth) ? document.documentElement.clientWidth : document.body.offsetWidth;
		h = (window.innerHeight) ? window.innerHeight : (document.documentElement && document.documentElement.clientHeight) ? document.documentElement.clientHeight : document.body.offsetHeight;
	}
	return {w:w,h:h};
};
jQuery.getScroll = function (e) 
{
	if (e) {
		t = e.scrollTop;
		l = e.scrollLeft;
		w = e.scrollWidth;
		h = e.scrollHeight;
	} else  {
		if (document.documentElement && document.documentElement.scrollTop) {
			t = document.documentElement.scrollTop;
			l = document.documentElement.scrollLeft;
			w = document.documentElement.scrollWidth;
			h = document.documentElement.scrollHeight;
		} else if (document.body) {
			t = document.body.scrollTop;
			l = document.body.scrollLeft;
			w = document.body.scrollWidth;
			h = document.body.scrollHeight;
		}
	}
	return { t: t, l: l, w: w, h: h };
};

jQuery.intval = function (v)
{
	v = parseInt(v);
	return isNaN(v) ? 0 : v;
};

jQuery.fn.ScrollTo = function(s) {
	o = jQuery.speed(s);
	return this.each(function(){
		new jQuery.fx.ScrollTo(this, o);
	});
};

jQuery.fx.ScrollTo = function (e, o)
{
	var z = this;
	z.o = o;
	z.e = e;
	z.p = jQuery.getPos(e);
	z.s = jQuery.getScroll();
	z.clear = function(){clearInterval(z.timer);z.timer=null};
	z.t=(new Date).getTime();
	z.step = function(){
		var t = (new Date).getTime();
		var p = (t - z.t) / z.o.duration;
		if (t >= z.o.duration+z.t) {
			z.clear();
			setTimeout(function(){z.scroll(z.p.y, z.p.x)},13);
		} else {
			st = ((-Math.cos(p*Math.PI)/2) + 0.5) * (z.p.y-z.s.t) + z.s.t;
			sl = ((-Math.cos(p*Math.PI)/2) + 0.5) * (z.p.x-z.s.l) + z.s.l;
			z.scroll(st, sl);
		}
	};
	z.scroll = function (t, l){window.scrollTo(l, t)};
	z.timer=setInterval(function(){z.step();},13);
};