//Pluginfile for IMX.jQuery Plugins
//Author: Axel Güldner (gueldner@infomax-it.de)
//Version 2.0
//Date 08.12.2009

jQuery.fn.extend({

//accordion
  accordion: function(value,direction,speed,callback){
		if(direction == "v"){
			newHeight = value + "px";
			return this.animate({height: newHeight}, speed, callback);
		}else{
			newWidth = value + "px";
			return this.animate({width: newWidth}, speed, callback);
		}
	}
});

jQuery.fn.extend({

//horizontal slide
  hslide: function(pixel,speed,easing,callback){
		if(this.css("left") == "auto" || this.css("left") == "" || this.css("left") == "undefined"){
			this.css("left","0px");
		}
		if(this.width() - parseInt(pixel) + 10 <= -1 * (parseInt(this.css("left"))- parseInt(pixel))){
			return this.animate({left: "0px"}, speed, callback);
		}else{
			pixel = parseInt(this.css("left")) - pixel + "px";
			return this.animate({left: pixel}, speed, easing, callback);
		}
	}
});

//This Plugin will wiggle Objects. That means to shake/swing them around they given center point
//You can customize the wiggle over the params:
//  speed:          how fast should the animation be
//  strenght:       how many pixel can the object fly around
jQuery.fn.extend({
  wiggle: function(options){
        myOptions = options;
        this.each(function(){

            $(this).css({"position":"absolute"});

            var options = new Array();
            myOptions["speed"] ? options["speed"] = myOptions["speed"] : true;
            myOptions["strength"] ? options["strength"] = myOptions["strength"] : true;
            options["posX"] = $(this).css("left");
            options["posY"] = $(this).css("top");

            var randomInterval = Math.random() * 500 + 50;

            var element = $(this);

            $.fn.wiggleHelper(options, element)
        });
	},
  wiggleHelper: function(options, element){
    options["speed"] ? speed = options["speed"] : speed = 1000;
    options["strength"] ? strength = options["strength"] : strength = 10;
    options["posX"] ? posX = options["posX"] : posX = 0;
    options["posY"] ? posY = options["posY"] : posY = 0;

    newPosX = $.fn.wiggleRand({"start":posX, "end":strength}) + "px";
    newPosY = $.fn.wiggleRand({"start":posY, "end":strength}) + "px";

    element.animate({"left":newPosX, "top":newPosY},speed,"swing", function callFunc() {$.fn.wiggleHelper(options, element)});
  },
  wiggleRand: function(scope){
    if(scope){
      scope["start"] ? start = parseInt(scope["start"]) : start = 0;
      scope["end"] ? end = parseInt(scope["end"]) : end = 10;
    }else{
      start = 0;
      end = 0;
    }
    var response = Math.floor(Math.random() * (end + 1)) + start;
    return response
  }
});


//Blur/Focus-Behavior of Formelements
jQuery.fn.extend({
    inputPresets: function(){
        $(this).find('input[type="text"]:not(.dp-applied):not(.ignore), textarea').each(function(){
            $(this).focus(function(){
    	        if(!$(this).attr("rel"))$(this).attr("rel", $(this).val());
    	        if($(this).val() == $(this).attr("rel")){
    		        $(this).val('');
    	        }
            });
            $(this).blur(function(){
                if($(this).val() == '')$(this).val($(this).attr("rel"));
            });
        });
    }
});


//jQuery Function to compare two given dates and to increase the second if it is equal to or below the first
jQuery.fn.extend({
  compareDates: function(secondDate){
        //Erstellen eines Strings des neuen Anreisedatums mit umgekehrter Reihenfolge
        var newArrival = $(this).val().split("."); newArrival.reverse(); newArrival = newArrival.join("");
        //Erstellen eines Strings des aktuellen Abreisedatums mit umgekehrter Reihenfolge
        var currentDepart = $(secondDate).val().split("."); currentDepart.reverse(); currentDepart = currentDepart.join("");
        //Neues Abreisedatum erstellen, ist neues Anreisedatum plus fünf Tage
        var newDepart = parseInt(newArrival) + 1;
        //Umwandeln des neuen Abreisedatums in einen String
        newDepart += " ";
        //Prüfen ob aktuelles Abreisedatum kleiner oder gleich dem neuen Anreisedatums ist
        if(currentDepart <= newArrival){
            //Erstellen eines Strings des neuen Abreisedatums in normaler Reihenfolge
            currentDepart = newDepart.substr(6, 2) + "." + newDepart.substr(4, 2) + "." + newDepart.substr(0, 4);
            //Kalender mit neuem Abreisedatum und neuem kleinstmöglichen Abreisedatum aktualisieren
            //Aktuelles Abreisedatum ist dabei das neue Anreisedatum plus ein Tag
            $(secondDate).dpSetSelected(currentDepart).dpSetStartDate(currentDepart);
        }else{
            //Erstellen eines Strings des neuen Abreisedatums in normaler Reihenfolge
            currentDepart = newDepart.substr(6, 2) + "." + newDepart.substr(4, 2) + "." + newDepart.substr(0, 4);
            //Kalender mit neuem kleinstmöglichen Abreisedatum aktualisieren
            $(secondDate).dpSetStartDate(currentDepart);
        }
	}
});
