jQuery.fn.mousehold = function(timeout, f) {
	if (timeout && typeof timeout == 'function') {
		f = timeout;
		timeout = 100;
	}
	if (f && typeof f == 'function') {
		var timer = 0;
		var fireStep = 0;
		return this.each(function() {
			jQuery(this).mousedown(function() {
				fireStep = 1;
				var ctr = 0;
				var t = this;
				timer = setInterval(function() {
					ctr++;
					f.call(t, ctr);
					fireStep = 2;
				}, timeout);
			})

			clearMousehold = function() {
				clearInterval(timer);
				if (fireStep == 1) f.call(this, 1);
				fireStep = 0;
			}
			
			jQuery(this).mouseout(clearMousehold);
			jQuery(this).mouseup(clearMousehold);
		})
	}
}


$(document).ready(function(){
  
  
  $('.close').live("click", function(){
	 $(this).parent().parent().remove();
	});
	
	
  $('.minimize').live("click", function(){
  	$(this).parent().parent().animate({ 
        opacity: 0
      }, 1500 );
    $(this).parent().parent().css('z-index','0');
    var html = '<div class="taskbarelement" id="taskbar'+$(this).parent().parent().attr('id')+'">'+$(this).attr('title')+'</div>';
    $('#taskbar').append(html);
    return false;
	});
	
	
	
	
	$('.scrollUp').live("mouseover", function(){
	 $(this).css("background-image","url('img/scrollup_over.png')");
	});
	
	$('.scrollUp').live("mouseout", function(){
	 $(this).css("background-image","url('img/scrollup.png')");
	});
	
		$('.scrollDown').live("mouseover", function(){
	 $(this).css("background-image","url('img/scrolldown_over.png')");
	});
	$('.scrollDown').live("mouseout", function(){
	 $(this).css("background-image","url('img/scrolldown.png')");
	});
	
	
	
	$('.taskbarelement').live("click", function(){
  var newid = $(this).attr('id').replace("taskbar", '');
  zindex = parseInt($("#lastzindex").attr('title'));
	zindex++;
	$('#'+newid).css('z-index',zindex);
	$("#lastzindex").attr('title',zindex);
      
   if($('#'+newid).hasClass('notransparency')) {
     $('#'+newid).animate({ 
        opacity: 1
      }, 1500 );
   } else {
     $('#'+newid).animate({ 
        opacity: 0.7
      }, 1500 );
   }
    $(this).remove();
	});	
	
	
  $('.window').live("click", function(){
  	  zindex = parseInt($("#lastzindex").attr('title'));
	  zindex++;
	  $(this).css('z-index',zindex);
	  $("#lastzindex").attr('title',zindex);
	});
	
	$('#dragNaviContainer').live("click", function(){
  	  zindex = parseInt($("#lastzindex").attr('title'));
	  zindex++;
	  $(this).css('z-index',zindex);
	  $("#lastzindex").attr('title',zindex);
	});	

	
	$('#playlist_toggle').live("click", function(){
	 $('#playlist_list').toggle();
	});
	
	
});

$(document).ready(function(){
	var size = GetDocumentSize();
	$('#mother').css('width',size[0]+'px');
	$('#mother').css('height',size[1]+'px');
});
function GetAjaxCall(url, functionName) {
  url = url;
  $.ajax({
     url: url,
     processData: false,
     dataType:  'json',
     success: functionName,
     type: 'POST'
   });
  }
  
  function donothing(e,i) {
  $('#murkykontakt').remove();
  return false;
  }

function GetWindowByAjax(id, qstring) {
  GetAjaxCall('windows.php?id='+id+qstring,insertWindow);
  pageTracker._trackPageview("/windowID"+id );
}

function ReplaceWindowContentByAjax(id, name, qstring) {
  GetAjaxCall('loader.php?name='+name+'&amp;windowid='+id+qstring,replaceWindowContent);
}

function insertWindow(data,responseMessage) {
  $('#windowInsertion').append(data.sWindowCode);
  var size = GetDocumentSize();
  
  if(data.sHeightPercent=='yes') {
  	var height = size[1] * (data.sHeight/100);
  } else {
  	var height = data.sHeight;
  }
  if(data.sWidthPercent=='yes') {
  	var width = size[0] * (data.sWidth/100);
  } else {
  	var width = data.sWidth;
  }
  
  if(data.sTopPercent=='yes') {
  	var top = size[1] * (data.sTop/100);
  	top = top - (height * (data.sTop/100));
  } else {
  	var top = data.sTop;
  }
  if(data.sLeftPercent=='yes') {
  	var left = size[0] * (data.sLeft/100);
  	left = left - (width * (data.sLeft/100));
  } else {
  	var left = data.sLeft;
  }
  
  $('#'+data.sId).css('top',top+'px');
  if(data.sLeftIsRight=='yes') {
  	left = size[0]-left-width;
  	$('#'+data.sId).css('left',left+'px');
  } else {
  	$('#'+data.sId).css('left',left+'px');
  }
  $('#'+data.sId).css('width',width+'px');
  margin = width-75;
  $('#'+data.sId+' .functions').css('margin-left',margin+'px');
  $('#'+data.sId).css('height',height+'px');
  zindex = parseInt($("#lastzindex").attr('title'));
  zindex++;
  $('#'+data.sId).css('z-index',zindex);
  $("#lastzindex").attr('title',zindex);
  $('#'+data.sId+' .functions').css('z-index',zindex);
  $('#'+data.sId+' .windowTitle').css('z-index',zindex);
  if(data.sResizeable=='yes') {
    $('#'+data.sId).resizable();
  }
   if(data.sScrollable=='yes') {
     $('#ContentInner'+data.sId+' .scrollContainer').html(data.sHTML);
   } else {
     $('#ContentInner'+data.sId).html(data.sHTML);
   }
   $('#'+data.sId).fadeIn('slow');
   if(data.sScrollable=='yes') {
   	 $('#Content'+data.sId).css('overflow-x','hidden');
   	 $('#Content'+data.sId).css('overflow-y','auto');
     $('#'+data.sId+' .scrollDown').mousehold(function(i) {pageScrollDown('#'+data.sId+' .scrollContainer');});
     $('#'+data.sId+' .scrollUp').mousehold(function(i) {pageScrollUp('#'+data.sId+' .scrollContainer');});
     $('#'+data.sId+' .scrollContainer')
        .bind('mousewheel', function(event, delta) {
            var dir = delta > 0 ? true : false;
            if(dir) {
              pageScrollUp('#'+data.sId+' .scrollContainer');
            } else {
              pageScrollDown('#'+data.sId+' .scrollContainer');
            }
            return false;
      });

     
   }
   if(data.sDraggable=='yes') {
    $('#'+data.sId).draggable({handle: '.moveable', containment: '#mother'});
   }
   $('#'+data.sId).css('position','absolute');
   
}

function pageScrollDown(toscrollid) {
   height = $(toscrollid).height();
   height = height-200;
   scrolldowntop =  $(toscrollid).css('margin-top').replace("px", "");
   scrolldowntop = parseInt(scrolldowntop);
   if(!scrolldowntop) scrolldowntop = 0;
   if((scrolldowntop*-1)>height) {} else {scrolldowntop = scrolldowntop-12;}
	$(toscrollid).css('margin-top',scrolldowntop+'px'); // horizontal and vertical scroll increments
}


function pageScrollUp(id) {
   scrolluptop =  $(id).css('margin-top').replace("px", "");
   scrolluptop = parseInt(scrolluptop);
   if(!scrolluptop) scrolluptop = 0;
   scrolluptop = scrolluptop+12;
   if(scrolluptop>0) {scrolluptop = 0;}
	$(id).css('margin-top',scrolluptop+'px'); // horizontal and vertical scroll increments
}


function ReplaceWindowContent(data,responseMessage) {
  $('#Content'+data.sId).fadeOut('fast',function(){$('#Content'+data.sId).html(data.html);$('#Content'+data.sId).fadeIn('fast');});
  if(data.isScrollable=='yes') {
     $('#Content'+data.sId).jScrollPane({scrollbarWidth:19, scrollbarMargin:10, dragMinHeight:41, dragMaxHeight:42});
  }
}

function GetDocumentSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  var returnSizes = new Array(myWidth, myHeight);
  return returnSizes;
}




function sendkontakt() {
    var getstring = '?sent=1';
    var hadError = false;
    $('.formkontakt').each(function(){
      if($(this).hasClass('mandatory')) {
        if($(this).val()) {
          $(this).removeClass('hasError');
        } else {
          $(this).addClass('hasError');
          hadError = true;
        }
      }
      getstring += '&'+$(this).attr('name')+'='+$(this).val();
    });
    if(!hadError) {
      var url = 'sendaform.php'+getstring;
      GetAjaxCall(url, donothing);
    } else {
      alert('Bitte Name und E-Mail eintragen.');
    }
    return false;
  }

  function toggleflashbg() {
    if($.browser.safari) {}
    else {
      if($('#flashbg').attr('id') == 'flashbg') {
        $('#flashbg').remove();
      } else {
        windowsize = GetDocumentSize();
    		newflashheight = windowsize[0] * 0.67;
    	  var so1 = new SWFObject("mm_bg.swf", "flashbg", windowsize[0], newflashheight, "8", "transparent");
    		so1.addParam("quality", "best");
    		so1.addParam("autoplay", "true");
    		so1.addParam("wmode", "transparent");
    		so1.write("bg");
      }
    }
  }
  
  function login() {
        var getstring = '?sent=1';
        $('.formlogin').each(function(){
          getstring += '&'+$(this).attr('name')+'='+$(this).val();
        });
        
          var url = 'login.php'+getstring;
          GetAjax(url, logincallback);
         
        return false;
      }
      
  function logincallback(data, e) {
    if(data.loggedIn == 'no') alert('Login fehlgeschlagen! Nochmal versuchen.'); else {
      top.loggedin = 'yes';
      top.loginname = data.alias;
      top.loginimage = data.image;
      top.loginid = data.userid;
      $('#login_form').fadeOut('slow');
      $('#infofeedformauthor').fadeOut('slow');
      $('#infofeedformauthor').remove();
      $('#login').html('<div id="loggedin">Hallo '+data.alias+'! <a onclick="window.location.href=\'index.php?logout=true\';return false;">Logout</a></div>');
    }
  }
  
  function register() {
        var getstring = '?sent=1';
        $('.registerform').each(function(){
          getstring += '&'+$(this).attr('name')+'='+$(this).val();
        });
        
          var url = 'register.php'+getstring;
          GetAjax(url, registercallback);
         
        return false;
      }
      
  function registercallback(data, e) {
    if(data.status == 'ok') {
      $('#windowregister').remove();
      alert('Dankeschön!');
    } else alert(data.error);
  }
