function liveSearch ()
{
	var	event = false;
	var   me  = this;
	
	this.search = function(flag)
	{
		var val = $('#sfield').val();
	   if (flag === true) {
			this.reset ();		

			$('#list').stop().animate ({opacity:0.25},300);
			$("object").each(function(index) {$(this).hide ();});
			
			this.event = $.ajax({url: "/request.php", type: "POST", data: "search="+val, context: document.body, 
	         success: function(data){
	        		$("#live").html(data);
	        		me.reset ();
	         }
        });
		}
		else {
			$('#list').stop().animate ({opacity:1.0},300);
			$("object").each(function(index) {$(this).show ();});
			$('#sfield').val ('suchen');
	
			this.reset ();		
		}
	}
	
	this.reset = function ()
	{
		if (this.event) {
			this.event.abort();
			this.event=false;
		}
	}
}

function search () 
{
	$("#results").toggle();
	$("#sfield").focus();
}

function videolink (uid)
{
	$("#V"+uid).hide();
	
	$("#"+uid).show();
}

function hover (el, source)
{
	$(el).attr("src", "/uploads/"+source);
}

function unhover (el, source)
{
	$(el).attr ("src", "/request.php?image="+source);
}

function headerstory () 
{
var self    = this;
this.handle = false;
this.active = -1;

this.sprite = function (id,cx,cy,top,name)
{ 
  var sp = $("<div id='"+id+"' style='width:"+cx+"px;height:"+cy+"px;position:absolute;left:0;top:"+top+"px;'><img src='/assets/image/header/"+name+"'/></div>");
  $(".header").append (sp);
  sp.hide ();
  return sp;
}

this.ballon = this.sprite ("ballon",17,27,20,"ballon.gif");
this.sheep  = this.sprite ("sheep", 29,14, 86-15,"sheep.gif");
this.post   = this.sprite ("post", 26,17,86-17, "post_ri.gif");
this.car_ri = this.sprite ("carr", 26,16,86-16, "auto_ri.gif");
this.car_le = this.sprite ("carl", 26,16,86-16, "auto_le.gif");

$(".header").css("position","relative");

this.animBallon = function ()
{
  var time = 30000;
  
  this.ballon.show();
  this.ballon.coreFX({duration:time,from:0,to:400,unit:"px",property:"left"});
  setTimeout (function () {self.ballon.fadeOut(1000);},time - 1000);
}

this._perform = function ()
{
    var id = Math.floor (Math.random () * 4);
    if (id !== self.active) {
      switch (id) {
        case 0: self.animBallon();break;
        case 1: self.animSheep();break;
        case 2: self.animPost();break;
        case 3: self.animCarFromLeft();break;
        case 4: self.animCarFromRight ();break;
      }
      self.active = id;
    }
}

this.animator = function () 
{
  if (this.handle !== false) {
    clearInterval (this.handle);
  }
  //# first performance w/o delay
  this._perform ();
  this.handle = setInterval (function () {
    self._perform ();
  },20000);
}

this.animSheep = function ()
{
  var time = 3000;
  this.sheep.show();
  this.sheep.coreFX({duration:time,from:0,to:90,unit:"px",property:"left"});
  setTimeout (function () {self.sheep.fadeOut(1000);},time + 5000);
}

this.animPost = function ()
{
  var time = 3000;
  this.post.show();
  this.post.coreFX({duration:time,from:0,to:180,unit:"px",property:"left"});
  setTimeout (function(){
    self.post.find("img").attr("src","/assets/image/header/post_le.gif");
    self.post.coreFX({duration:time,from:180,to:0,unit:"px",property:"left"});
  },time+3000);
  setTimeout (function () {self.post.find("img").attr("src","/assets/image/header/post_ri.gif");self.post.hide();},(time*2)+3500);
  
}

this.animCarFromLeft = function ()
{
  var time = [4000,2000], wait = 3000;
  
  this.car_ri.show();
  this.car_ri.coreFX({duration:time[0],from:0,to:280,unit:"px",property:"left"});
  setTimeout (function(){self.car_ri.coreFX({duration:time[1],from:280,to:400,unit:"px",property:"left"});},time[0]+wait);
  setTimeout (function (){self.car_ri.fadeOut(1000);},time[0]+time[1]+wait - 1000);
}

this.animCarFromRight = function ()
{
  var time = 6000;
  
  this.car_ri.show();
  this.car_ri.coreFX({duration:time,from:374,to:0,unit:"px",property:"left"});
  setTimeout (function(){
    self.car_ri.fadeOut(1000);
  },time-1000);
}
};

var _input;
var _search;
var _hdr;

$(document).ready(function() 
{
	_input = new UserInput ();
	_search = new liveSearch ();
	_hdr = new headerstory ();
   _hdr.animator();
	
   headerstory();  

   //# navigation stuff	
	$("#left").click (function () {
	  _input.showPrevious ();
	});
	$("#right").click (function () {
	  _input.showNext ();
	});
	
	$(".fix_image").live ('click', function () {
		var src = $(this).attr ("src");
		window.open("http://corefault.de"+src,"image","");
	});
	
	//# detect iPhone and stuff
	if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
      /*$("#list").css ("margin","5px");  
      $("#left").css ("margin",0);
      $("#right").css ("margin",0);
      $("#left").css ("left",0);
      $("#right").css ("left",0);
      $("#right").css ("top","200px");
      
      $("#search").hide();
      
      $("#list").css ("width","100%");*/
   }
});


