/*
 * 
 * HOCHZWEI JavaScript
 * 
 * JQuery-Funktionen für Anderssen Yachting
 * 2009/2010
 * 
 * Content Slider / Carousel und Shop-Navigation
 * 
 */


this.formblur = function(obj, inactive, active)
{	
	var active = "active";	
	var focused = "focused";
	jQuery(obj).focus(function(){	
		jQuery(this).addClass(focused);
		jQuery(this).removeClass(active);								  
	});	
	jQuery(obj).blur(function(){	
														 
		if(jQuery(this).val() == "") {
			jQuery(this).removeClass(focused);
		} else {
			jQuery(this).addClass(active);		
		};				
	});				
	
};



 var carousel_1;
function carousel_1_initCallback(carousel)
{
	carousel.selected = 1
	
    carousel.buttonNext.bind('click', function()
    {
        carousel.startAuto();
        return false;
    });

    carousel.buttonPrev.bind('click', function()
    {
        carousel.startAuto();
        return false;
    });
   
    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });

};

function carousel_1_beforeAnimation(carousel,element,i,status)
{
    var idx = carousel.index(i, carousel.options.size);
	carousel.add(i,carousel.get(idx).html())
}
        	
function carousel_1_afterAnimation(carousel,element,index,status)
{
    var idx = carousel.index(index, carousel.options.size);
	carousel.selected = idx;
}

function carousel_1_itemVisibleOutCallback(carousel, item, i, state, evt)
{
   	if (i>carousel.options.size || i<0)
   	{
    	carousel.remove(i);
   	}
  	carousel.startAuto();
};


$(document).ready(function()
{
	// Content Slider mit Nicht-Standard-Animation
	jQuery(".content-slider ul").jcarousel({
        easing: 'easeOutElastic',
        animation: 700,
        scroll: 1,
        auto: 7,
        wrap: 'circular',
        initCallback: carousel_1_initCallback,
        itemVisibleInCallback: {
  					onBeforeAnimation: carousel_1_beforeAnimation,
  					onAfterAnimation: carousel_1_afterAnimation
						},
				itemVisibleOutCallback:carousel_1_itemVisibleOutCallback
    });
	
	
	formblur("#sb-login-user");
	formblur("#sb-login-pass");
	
	// Dropdowns
	var menus = jQuery(".magmenu");
	menus.each(function(i)
	{
		// Erste Ebene nicht bearbeiten - die soll immer ausgeklappt bleiben
		if ((i > 0) && (i < menus.length -1))
		{
			makeDropdown(this);
			jQuery(this).click(function() {expandDropdown(this)});
			jQuery(this).mouseleave(function() {collapseDropdown(this)});
		}
	});
	
	startup = false;
});

var startup = true;

function makeDropdown(menu)
{
	var active = jQuery(".active a", menu);
	if (active && (active.length > 0))
	{
		jQuery(".sbar-dropdown", menu).html(active.html()).fadeIn("slow");
		jQuery(".sbar-box5", menu).css("top", "-53px").css("margin-bottom", "-43px").dropup(); //.slideUp("slow");
	}
}

function expandDropdown(menu)
{
	if (!startup)
	{
		//jQuery(".sbar-dropdown", menu).fadeOut(100);
		jQuery(".sbar-box5", menu).dropdown(); //.slideDown("fast");		
	}
}

function collapseDropdown(menu)
{
	if (!startup)
	{
		//jQuery(".sbar-dropdown", menu).fadeIn(100);
		jQuery(".sbar-box5", menu).dropup(); //.slideUp(200);		
	}
}

/*
 * JQuery-Erweiterung, die etwas schöner als das eingebaute slideUp() und slideDown() aussieht
 */
(function($){
	$.fn.dropdown = function() {
		var obj = jQuery(this);
		
		var height = obj.data("originalHeight"), visible = obj.is(":visible");
		
		// if the current visiblilty is the same as the requested state, cancel
		if(visible ) return false;
		
		// get the original height
		if( !height ){
			// get original height
			height = obj.show().height();
			// update the height
			obj.data("originalHeight", height);
			// if the element was hidden, hide it again
			if( !visible ) obj.hide().css({height: 0});
		}
		//obj.fadeIn();
		obj.show().animate({height: height, opacity: 1}, {duration: 150});
		
	}
	$.fn.dropup = function () {
		var obj = jQuery(this);
		var height = obj.data("originalHeight"), visible = obj.is(":visible");
		
		// if the current visiblilty is the same as the requested state, cancel
		if( !visible ) return false;
		
		// get the original height
		if( !height ){
			// get original height
			height = obj.show().height();
			// update the height
			obj.data("originalHeight", height);
			// if the element was hidden, hide it again
			if( !visible ) obj.hide().css({height: 0});
		}
		//obj.fadeOut();
		obj.animate({height: 0, opacity: 0}, {duration: 150, complete:function (){ obj.hide(); } });
		
	}
})(jQuery);

/*
 * Easing-Funktion ähnlich der Animation in den Flashfilmen
 * Dieser Code stammt aus dem Easing-Plugin von http://gsgd.co.uk/sandbox/jquery/easing/
 * Vielen Dank
 */
/*
 * jQuery Easing v1.3 - http://gsgd.co.uk/sandbox/jquery/easing/
 *
 * Uses the built in easing capabilities added In jQuery 1.1
 * to offer multiple easing options
 *
 * TERMS OF USE - jQuery Easing
 * 
 * Open source under the BSD License. 
 * 
 * Copyright © 2008 George McGinley Smith
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this list of 
 * conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, this list 
 * of conditions and the following disclaimer in the documentation and/or other materials 
 * provided with the distribution.
 * 
 * Neither the name of the author nor the names of contributors may be used to endorse 
 * or promote products derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 *  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
 * OF THE POSSIBILITY OF SUCH DAMAGE. 
 *
*/
jQuery.extend( jQuery.easing,
		{
			easeOutElastic: function (x, t, b, c, d) {
				var s=1.70158;var p=0;var a=c;
				if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
				if (a < Math.abs(c)) { a=c; var s=p/4; }
				else var s = p/(2*Math.PI) * Math.asin (c/a);
				return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
			}
		}
);

