// JavaScript Document

function bookmarkMe() {
   if(navigator.appName!='Microsoft Internet Explorer') {
        window.sidebar.addPanel(unescape("%22Sowas will ich auch%22 Gadgets%2C Spiele %26 Spielzeug Geschenksideen %26 Verr%FCcktes f%FCr einen coolen Lifestyle"),"http://www.sowaswillichauch.de","");
   }	    
   else {
       window.external.AddFavorite("http://www.sowaswillichauch.de",unescape("%22Sowas will ich auch%22 Gadgets%2C Spiele %26 Spielzeug Geschenksideen %26 Verr%FCcktes f%FCr einen coolen Lifestyle"));
   }	   
}

$(document).ready(function() {
    
    $("#content").localScroll();
    
    $("#featuredProducts li.product").css("cursor", "pointer").click(function() {
        window.location.href = $(this).find("a").attr("href");
    });
    
    $("#giftBox").css("cursor","pointer").click(function() {
        window.location.href = $(this).attr("rel");
    });
	
	$("#pressBox").css("cursor","pointer").click(function() {
        window.location.href = $(this).attr("rel");
    });
            
    /* Top-Banner SlideShow */
    
    function cycleTopBanners() {
            $($topBannerThumbs[current]).fadeTo(transitionSpeed, thumbTransparency);
            $($topBannerThumbs[next]).fadeTo(transitionSpeed, 1);
            $($topBanners[current]).fadeOut(transitionSpeed);        
            $($topBanners[next]).fadeIn(transitionSpeed, function() {
    	        current = current + 1;
    	        if ( current > $topBanners.length - 1 ) {
    	            current = 0;
    	        } else {    
    	        }
    	        next = next + 1;
    	        if ( next > $topBanners.length - 1 ) {
    	            next = 0;
    	        }
    	        var link = $($topBanners[current]).find("a").attr("href");
    	        $topBannerLink.attr("href", link);
            });
        }
    
    /* Variables */
    if ( $("#topBannersContainer").length > 0 ) {
        var $topBanners = $("#topBanners li")    
        var current = 0;
        var next = 1;
        var speed = 10000
        var transitionSpeed = 3000;
        var $topBannerThumbs = $("#topBannerThumbs li");
        var thumbTransparency = 0.3;
        var $topBannerLink = $("#topBannerMoreInfo");
        $("#topBannerThumbs li:not(:first)").fadeTo("700", thumbTransparency);
            
        
        var topBannerInterval = setInterval(cycleTopBanners, speed);                
    
        $topBannerThumbs.mouseover(function() {
            clearInterval(topBannerInterval);
            var thisElem = $(this);
            $("#topBannerThumbs li:not(this)").stop().animate({
                opacity: 0.3
            });
            thisElem.stop().animate({
                opacity: 1
            });
            var slideIndex =  thisElem.attr("rel");
            if ( slideIndex != current ) {
    	        $($topBanners[current]).stop().animate({ opacity: 0 }, function() {
    	            $(this).hide();
    	        });
                $($topBanners[slideIndex]).stop().css({ display: "block", opacity: 0 }).animate({ opacity: 1 });
                var link = $($topBanners[slideIndex]).find("a").attr("href");
                $topBannerLink.attr("href", link);
            }
            current = slideIndex;
        });
    }
    /* End Top-Banner SlideShow */
    
    /* Main Navigation */
    $("#mainNav > li").hover(
    function() {
        var dropDown = $(this).children("ul");
        var dropDownHeight = dropDown.innerHeight();
        var cssOptions = {
            display: "block",
            opacity: 1,
            height: 0
        };
        var animateOptions = {
            height: dropDownHeight + "px"
        };
        dropDown.css(cssOptions).stop().animate(animateOptions, 300, "swing");
    },
    function() {
        var cssOptions = {
            height: ""
        };
        $(this).children("ul").css(cssOptions).stop().hide();
    });
    /* End Main Navigation */         
    
    /* Micro Cart */
    
    $("#microCart").click(function(el) {        
        var thisElem = $(el.target);        
        if(el.target.id == "toggleBasket") {
            var text = thisElem.text();
            if ( text == "Warenkorb anzeigen" ) {
                thisElem.attr("class", "hide");
                showMicroCart();
            } else if ( text == "Warenkorb ausblenden" ) {
                thisElem.attr("class", "show");
                hideMicroCart();
            }
            return false;
        }                        
    });
    /* End Micro Cart */


    function hideMicroCart() {
        $("#microCartItems").slideUp(300);
        $("#toggleBasket").text("Warenkorb anzeigen");
    }
    
    function showMicroCart() {
        $("#microCartItems").slideDown(300);
        $("#toggleBasket").text("Warenkorb ausblenden");
    }
    
    $("#productAltImagesRotator").innerfade({
        speed: 1000,
        timeout: 3000,
        containerheight: "171px"
    }).show().append("<li id='productAltImagesRotatorLink'><a href='#'>Weitere Bilder</a></li>");    

    /* Short Description */
    $(".shortDescription").css("opacity", 0.9).click(function() {
        window.location.href = $(this).find("a").attr("href");
    });
    
    $(".titleImageContainer").hover(function() {
       if ( $(this).children(".shortDescription").is(":animated") == false ) {
           $(this).children(".shortDescription").slideDown();           
       }
    }, function() {
       $(this).children(".shortDescription").fadeOut();
    });



    function loadModalWindow(activeMenuItem) {                
        $("#wrapper object").css("visibility", "hidden");
        $("#overlay").css({ opacity: 0.7 }).show();
        if ( $.bro ) {
            $("#overlay").bgiframe();
        }
        $("#productModalWindow").animate({ width: "480px", marginLeft: "-260px" }, function() {
                $(this).animate({ height: "325px", marginTop: "-219px" }, function() {                
                $(activeMenuItem).addClass("active").fadeIn();
                $("#productModalWindowClose").fadeIn();                                
            });
        });
    }

    function unloadModalWindow() {
        $("#productModalWindowClose").fadeOut();
        $("#productModaWindowNav").fadeOut();
        $("#productModalWindow > .active").removeClass("active").fadeOut();
        
        $("#productModalWindow").animate({
            height: 0,
            marginTop: 0 
        }, function() {
            $(this).animate({
                width: 0,
                marginLeft: 0  
            }, function() {
                    $("#overlay").hide();
                    $("#wrapper object").css("visibility", "visible");
                    $(this).removeAttr("style");
                });
        });
    }
    
    function switchTab(activeMenuItem) {
        $("#productModalWindow > .active").removeClass("active").hide();
        $(activeMenuItem).addClass("active").show();
        if ( activeMenuItem == "#productVideoContainer" ) {
            loadProductVideo();
        } else {
            unloadProductVideo();
        }
    }
    
    $("#productModalWindowClose").click(function() {
        unloadModalWindow();
    });
    
    $("#productShowcase a").click(function() {
        var thisElem = $(this);
        
        if ( thisElem.attr("rel") != '#productMoreViews' ) {
            return true;
        }
        
        if ( thisElem.attr("rel") ) {
            var activeMenuItem = thisElem.attr("rel");            
            loadModalWindow(activeMenuItem);
            return false;
        }
    });
    
    $("#productModaWindowNav li a").click(function() {
        var thisElem = $(this)
        
        if ( thisElem.attr("rel") ) {
            var activeMenuItem = thisElem.attr("rel");
            switchTab(activeMenuItem);
            return false;
        }
    });
    
    $("#productThumbnails a").click(function() {    
        if ( $("#productMoreViewLargeIMG").is(":animated") ) {
            return false;
        } else {
            var thisElem = $(this);
            $("#productThumbnails a").removeClass("active");
            thisElem.addClass("active");
            $("#productMoreViewLarge").attr("rel", thisElem.attr("rel"));
            $("#productMoreViewLargeIMG").attr("src", thisElem.attr("href"));
            if ( $("#productMoreViews").hasClass("zoom") === true ) {
               activateZoom();
            }
            return false;
        }
    });
    
    $("#productAltImagesRotator").click(function() {
        var thisElem = $(this);
        var activeMenuItem = thisElem.attr("rel");
        loadModalWindow(activeMenuItem);
        return false;
    });
    
    /* Thumbnail Navigation */
    var $productThumbnails = $("#productThumbnails");
    var thumbnailWidth = 64;
    var thumbnailCount = $productThumbnails.children().length;
    if ( thumbnailCount > 5 ) {
        var thumbnailContainerWidth = thumbnailWidth * thumbnailCount;
        $("#productThumbnails").css("width", thumbnailContainerWidth + "px");
        var nextButton = $("<a href='#' id='thumbnailNext'>Next</a>");
        var prevButton = $("<a href='#' id='thumbnailPrev' class='disabled'>Previous</a>");

        var moreViewContainer = $("#productMoreViews");
        moreViewContainer.append(prevButton);
        moreViewContainer.append(nextButton);
        
        $("#thumbnailNext").click(function() {
            moveThumbnails("left");
            return false;
        });
        
        $("#thumbnailPrev").click(function() {
            moveThumbnails("right");
            return false;
        });      
        
        function moveThumbnails(direction) {
            var $productThumbnails = $("#productThumbnails");
            if ( $productThumbnails.is(":animated") === false ) {
                var currentXPosition = $productThumbnails.position().left;
                if ( direction == "left" ) {
                    var xpos = thumbnailWidth - thumbnailWidth - thumbnailWidth + currentXPosition;
                } else if ( direction == "right" ) {
                    var xpos = thumbnailWidth + currentXPosition;
                }
                if ( currentXPosition == 0 && direction == "right" ) {
                    //Don't know why I can't say the reverse...
                } else if ( thumbnailContainerWidth + currentXPosition <= 320 && direction == "left" ) {
                   //Doing the same to be safe...
                } else {
                    $("#productThumbnails").animate({
                        left: xpos
                    }, function() {
                        if ( $productThumbnails.position().left == 0 ) {
                            $("#thumbnailPrev").addClass("disabled");
                        } else {
                            $("#thumbnailPrev").removeClass("disabled");
                        }
                        if ( thumbnailContainerWidth + $productThumbnails.position().left <= 320 ) {
                            $("#thumbnailNext").addClass("disabled");
                        } else {
                            $("#thumbnailNext").removeClass("disabled");
                        }
                    });                
                }
            }
        }
        
    }
    
});



jQuery(document).ready(function(){
  jQuery(".giftWrapBoxContainer img").mouseover(function() {
    jQuery("#giftWrapImage").attr("src",jQuery(this).attr("src"));
  });
});

function showMessageBox(checkboxId,containerId) {
  jQuery("#"+containerId).attr("style","display:block");
  jQuery("#"+checkboxId).attr("disabled","disabled");
}

function toggleGiftWrapBox() {
  jQuery("#giftWrappingBox").toggle();
}

function changeWrapping(id) {
  var el = jQuery("#"+id);
  jQuery(el).html("");    
}

function messageLimit(field,cntfield,maxlimit) {

  //var messTextArea = document.getElementById(field);
  //var messCounter = document.getElementById(cntfield);
  var messTextArea = $('#'+field);
  var messCounter = $('#'+cntfield);
                
  if (messTextArea.val().length > maxlimit)
    messTextArea.val(messTextArea.val().substring(0, maxlimit)) ;
  else
    messCounter.val( maxlimit - messTextArea.val().length);
}
