/* 
 jQuery Suckerfish Drop Downs
*/

jQuery(function( jQuery ){
//borrowed from jQuery easing plugin
//http://gsgd.co.uk/sandbox/jquery.easing.php
jQuery.scrollTo.defaults.axis = 'xy';
jQuery.easing.elasout = 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;
};
});

(function(jQuery) {
	jQuery.fn.customFadeIn = function(speed, callback) {
		jQuery(this).fadeIn(speed, function() {
			if(jQuery.browser.msie)
				jQuery(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
	jQuery.fn.customFadeOut = function(speed, callback) {
		jQuery(this).fadeOut(speed, function() {
			if(jQuery.browser.msie)
				jQuery(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
})(jQuery);

var scrollPane = "";		
var scrollContent = "";
var scrollbar = "";
var handleHelper = "";


function sizeScrollbar(){
	
	if(scrollPane.width() - scrollContent.width() >= 0 ) {
		scrollPane.css("display", "none");
	} else {
		scrollPane.css("display", "block");
	}
			var remainder = scrollContent.outerWidth() - scrollPane.width();
			var proportion = remainder / scrollContent.width();
			var handleSize = scrollPane.width() - (proportion * scrollPane.width());
			scrollbar.find('.ui-slider-handle').css({
				width: handleSize,
				'margin-left': -handleSize/2
			});
			
			jQuery('.ui-icon').css("margin-left", handleSize / 2 - jQuery('.ui-icon').width() / 2);
			if(jQuery.browser.msie) {
				jQuery('.scroll-bar').width(jQuery('.ui-handle-helper-parent').width());
				jQuery('.scroll-bar').css("margin-left", handleSize / 2);
			}
			handleHelper.width( scrollbar.width() - handleSize);	
		}

function resetValue(){
			var remainder = scrollPane.width() - scrollContent.width();
			var leftVal = scrollContent.css('margin-left') == 'auto' ? 0 : parseInt(scrollContent.css('margin-left'));
			var percentage = Math.round(leftVal / remainder * 100);
			scrollbar.slider("value", percentage);
		}
		//if the slider is 100% and window gets larger, reveal content
		function reflowContent(){
				var showing = scrollContent.width() + parseInt( scrollContent.css('margin-left') );
				var gap = scrollPane.width() - showing;
				if(gap > 0){
					scrollContent.css('margin-left', parseInt( scrollContent.css('margin-left') ) + gap);
				}
		}
		
if(window.location.hash) {
	jQuery('.content-text').css("display", "none");
}

var getCollectionFromHash = false;

function checkPosition(target) {
	jQuery("#scroll-pane").width(document.body.clientWidth);
	var elem = jQuery(target).children("#meta-navi");
	var minHeight = 35;
	
	elem.parent().height(jQuery(window).height() - 451);
	if(jQuery(window).height() - 451 <= jQuery(".menu-table").height() + minHeight + elem.height() - 5) {
		elem.parent().css("overflow", "auto");
		elem.css("top", jQuery(".menu-table").height() + minHeight - 5);
	} else {
		elem.parent().css("overflow", "");
		elem.css("top", "");
	}
}

function handler(newHash) {
	moveTo("#" + newHash);
}

jQuery(document).ready(function(){
	jQuery(".content-text").css("overflow", "hidden");
	jQuery("#scroll-pane").css("overflow-y", "hidden");
	jQuery("#scroll-pane").css("overflow-x", "auto");
	
	
	Hash.init(handler, 'js/blank.html');

	jQuery.Lightbox.relify("lightbox-all");
	var query = top.location.search;
	if(query.search(/contenido.+/) == -1) {
		
		FLIR.init( { path: 'http://www.bogner-leather.com/bogner/js/facelift/' } ); 
		FLIR.replace('h1.content strong');
		FLIR.replace('h1.content p');
		FLIR.replace('th.coll_headline'); 
		FLIR.replace('th.coll_headline a');
		
	}
	
	jQuery(".menu-area").everyTime(10, function() {
		checkPosition(this);
	});
	
	preloadCarousel();

	
	
	jQuery(".floatingImage").css("left", jQuery(".standard-text").width());
	jQuery(".floatingImage").css("display", "block");
	jQuery(".defaultSkin table.mceLayout").css("position", "relative");
	jQuery(".defaultSkin table.mceLayout table").css("margin", 0);

if(window.location.hash) {
	var query = window.location.hash;
	getCollectionFromHash = true;
	moveTo(query);
}

	scrollPane = jQuery('.scroll-bar-wrap');		
	scrollContent = jQuery('.content-text');
	
	/*
	scrollbar = jQuery(".scroll-bar").slider({
		slide:function(e, ui){
			if( scrollContent.width() > scrollPane.width() ){ scrollContent.css('margin-left', Math.round( ui.value / 100 * ( scrollPane.width() - scrollContent.width() )) + 'px'); }
			else { scrollContent.css('margin-left', 0); }
		},
		animate: true,
		change: function(e, ui){
			if( scrollContent.width() > scrollPane.width() ){ scrollContent.animate({ marginLeft : Math.round( ui.value / 100 * ( scrollPane.width() - scrollContent.width() )) + "px"}, 500 ); }
			else { scrollContent.css('margin-left', 0); }
		}
	});
	  
	
	//append icon to handle
		handleHelper = scrollbar.find('.ui-slider-handle')
				.mousedown(function(){
					scrollbar.width( handleHelper.width() );
				})
				.mouseup(function(){
					scrollbar.width( '100%' );
				})
				.append('<span class="ui-icon ui-icon-grip-dotted-vertical"></span>')
				.wrap('<div class="ui-handle-helper-parent"></div>').parent();
		
		
		//change overflow to hidden now that slider handles the scrolling
		jQuery('#scroll-pane').css('overflow','hidden');
		
		//size scrollbar and handle proportionally to scroll distance
		
		
		//reset slider value based on scroll content position
		
		
		//change handle position on window resize
		jQuery(window)
		.resize(function(){
			
			if(scrollPane.width() - scrollContent.width() < 0) {
				resetValue();
				sizeScrollbar();
				reflowContent();
			} else {
				scrollPane.css("display", "none");
			}
			
			
		});
		//init scrollbar size
		setTimeout(sizeScrollbar,100);//safari wants a timeout
	*/
	
	oldBack = "";
	
	


    jQuery("#collection_dropdown li").hover(
        function(){ jQuery(this).children("ul").css("display", "block");
		}, 
        function() { 
			jQuery(this).children("ul").css("display", "none");
		} 
    );
    if (document.all) {
        jQuery("#collection_dropdown li").hoverClass ("sfHover");
    }
	
	
	
	jQuery(".sec_navi li").hover(
		function() {
			jQuery(hoverLi).stopTime("hide");
			if(this != hoverLi) {
				resetCarousel(hoverLi);
				hoverLi = this;
				
				elem = jQuery(this).children("a");
				
				oldBack = elem.css("background-image") + " " + elem.css("background-repeat") + " 3px";
				
				jQuery(this).append("<div id='mycarousel'><div id='loader'><img src='upload/images/ajax-loader.gif' /></div><ul></ul></div>");
				var totalHeight = elem.height();
				totalHeight += parseInt(elem.css("padding-top"), 10) + parseInt(elem.css("padding-top"), 10); //Total Padding Width
				totalHeight += parseInt(elem.css("margin-bottom"), 10) + parseInt(elem.css("margin-top"), 10); //Total Margin Width
				totalHeight += parseInt(elem.css("borderBottomWidth"), 10) + parseInt(elem.css("borderTopWidth"), 10);
				factor = jQuery(this).parent().children().index(this);
				
				//elem.css("background", "url(upload/images/coll_hover_gradient.jpg) repeat 0 " + (-1 * totalHeight * factor) + "px");
				elem.css("background", "url(upload/images/coll_hover_gradient.jpg) repeat 0 " + (-1 * totalHeight * factor) + "px");
				loadCarousel(elem.attr("title").toLowerCase().replace(" ","-").replace("summer","sommer")); 
			}
		},
		function() {
			jQuery(this).oneTime(1000, "hide", function() {
				resetCarousel(this);
			});
		}
	);
	
	
});



var hoverLi;

function resetCarousel(target) {
	elem = jQuery(target).children("a");
	elem.css("background", oldBack);
	jQuery("#mycarousel").remove();
	hoverLi = false;
}

jQuery.fn.hoverClass = function(c) {
	return this.each(function(){
        jQuery(this).hover( 
            function() { jQuery(this).addClass(c);  },
            function() { jQuery(this).removeClass(c); }
        );
    });
};   

function moveTo(target) {
	/*
	if( jQuery(target).length > 0 ) {
		var marginValue = (-1 * (jQuery(target).offset().left - jQuery(target).parent().offset().left));
		var percentage = 100 * marginValue / ( jQuery('.scroll-bar-wrap').width() - jQuery('.content-text').width() );
		if(percentage > 100)
			percentage = 100;
		jQuery(".content-text").animate({marginLeft: Math.round( percentage / 100 * ( jQuery('.scroll-bar-wrap').width() - jQuery('.content-text').width() )) + 'px'}, 500);
		jQuery(".ui-slider-handle").animate({left: percentage + "%"}, 500);
		
		//Math.round( ui.value / 100 * ( scrollPane.width() - scrollContent.width() ))
		return false;
	} else {
		return true;
	}
	*/
	if(target == "#") {
		jQuery(".content-text table td.coll_product").each( function() {
			jQuery(this).css("display", "none");
		});
		jQuery("#choose_subCollection").css("display","block");
		/*
		if(scrollContent) {
			scrollContent.css("margin-left", "0");
			if(scrollPane.width() - scrollContent.width() < 0) {
				resetValue();
				sizeScrollbar();
				reflowContent();
			} else {
				scrollPane.css("display", "none");
			}
		}
		*/
	} else {
		var lightboxName = target.substring(1);
		
		jQuery(".activeListElement").removeClass();
		jQuery("#drop-"+lightboxName).addClass("activeListElement");
		jQuery("#choose_subCollection").css("display", "none");
		if( jQuery(target).length > 0 ) {
			
			jQuery.hash.go(lightboxName);
			jQuery('.content-text').css("display", "block");
			jQuery(".content-text table td.coll_product").each( function() {
				if( ("#" + jQuery(this).attr("id")) == target) {	
				var elem = jQuery(this).find("a");
					elem.attr("rel", "lightbox-" + lightboxName);
					jQuery(this).css("display", "block");
					jQuery(this).css("margin-left", "0");
				} else {
					jQuery(this).css("display", "none");
					//jQuery(this).css("float", "none");
				}
			});
			
			jQuery.Lightbox.relify("lightbox-" + lightboxName);
			/*
			if(scrollContent) {
				scrollContent.css("margin-left", "0");
				if(scrollPane.width() - scrollContent.width() < 0) {
					resetValue();
					sizeScrollbar();
					reflowContent();
				} else {
					scrollPane.css("display", "none");
				}
			}
			*/
			return false;
		} else {
			return true;
		}
	}
	
}

var myCarousel = null;
var carouselArray = new Array();

function preloadCarousel() {
	jQuery(".sec_navi li").each(function() {
		var elem = jQuery(this).children("a");
		var parent = elem.attr("title").toLowerCase().replace(" ","-").replace("summer","sommer");
		
		jQuery.get('http://www.bogner-leather.com/bogner/front_content.php?idart=24&dir=' + parent, function(data) {
			carouselArray[parent] = data;
		});
	});
}

function mycarousel_itemLoadCallback(carousel, state, parent)
{
    // Since we get all URLs in one file, we simply add all items
    // at once and set the size accordingly.
    if (state != 'init') 
        return;
	else
		myCarousel = carousel;
		

	if(carouselArray[parent] === undefined) {
		jQuery("#mycarousel").everyTime(500, "carouselVal-" + parent, function() { mycarousel_itemLoadCallback(carousel, state, parent)});
	} else {
		mycarousel_itemAddCallback(carousel, carousel.first, carousel.last, carouselArray[parent]);
		jQuery("#loader").remove();
		jQuery("#mycarousel").stopTime("carouselVal-" + parent);
	}
}

function mycarousel_itemAddCallback(carousel, first, last, data)
{
    
	
	// Simply add all items at once and set the size accordingly.
    var items = data.split('|');

    for (var i = 0; i < items.length; i++) {
		if(i < (items.length -1))
			carousel.add(i, mycarousel_getItemHTML(items[i], true));
		else
			carousel.add(i, mycarousel_getItemHTML(items[i], false));
    }

    carousel.size(items.length);
};

/**
 * Item html creation helper.
 */
function mycarousel_getItemHTML(url, spacer)
{
	parts = url.split("###");
    var output = '';
	var image = '<a href="http://www.bogner-leather.com/bogner/front_content.php?idcat=' + parts[2] +'#' + parts[1] +'" onclick="return moveTo(\'#' + parts[1] + '\');"><img src="' + parts[0] + '" alt="' + parts[1] + '" /></a>';
	if(spacer)
		output = image + '<img src="upload/images/coll_hover_trenn.png" alt="" />';
	else
		output = image;
	return output;
};

function loadCarousel(parent) {
	
	if(myCarousel) {
		myCarousel.reset(); 
		//jQuery('#mycarousel').removeClass("jcarousel-container");
		//jQuery('#mycarousel').removeClass("jcarousel-container-horizontal");
		//jQuery('#mycarousel').html("<ul></ul>");
	}
	jQuery('#mycarousel').css("top", 0);
	jQuery('#mycarousel').css("left", 105);
	jQuery('#mycarousel').css("width", 578);
	jQuery('#mycarousel').css("height", 103);
	
    jQuery('#mycarousel').jcarousel({
        itemLoadCallback: function (carousel, state) { mycarousel_itemLoadCallback(carousel, state, parent); },
        scroll: 3
    });
}
