$(function(){
    $("#languages a").click(function(){
        $(this).attr("href", $(this).attr("href") +"?return="+document.location.href);
    });

    $.fixSidebars = function(obj){
        $("#content").oneTime(60,"fix",function(){
            var _max = 0;
            obj.each(function(i,v){
                var _h = parseInt($(this).find("div:not(.title):not(.footer)").height());
                _h += parseInt($(this).find("div:not(.title):not(.footer)").css("padding-top"));
                _h += parseInt($(this).find("div:not(.title):not(.footer)").css("padding-bottom"));
                //console.log( _h + " - " + _max + " -> "+(_h > _max ? _h : _max) );
                _max = _h > _max ? _h : _max ;
            });

            obj.each(function(i,v){
                var _p = parseInt($(this).find("div:not(.title):not(.footer)").css("padding-top"));
                    _p += parseInt($(this).find("div:not(.title):not(.footer)").css("padding-bottom"));
                $(this).children("div:not(.title):not(.footer)").css("height",_max-_p);
            });
        });
    };

    $("#q").click(function(){$(this).val("");});

    $.showcase = function(){
        $("#showcase #showcaseContent ul").css("width",$("#showcase #showcaseContent ul li").length*175);
        var _inAnimation = false;
        $("#showcase #showcaseGoRight").click(function(){
            if(_inAnimation) return false;
            else _inAnimation = true;
            $("#showcase #showcaseContent ul").animate({marginLeft:'-=175'}, 1000,function(){
                $("#showcase #showcaseContent ul").append($("#showcase #showcaseContent ul li:first").clone());
                $("#showcase #showcaseContent ul").css("marginLeft", "0px");
                $("#showcase #showcaseContent ul li:first").remove();
                _inAnimation = false;
            });
        });
        $("#showcase #showcaseGoLeft").click(function(){
            if(_inAnimation) return false;
            else _inAnimation = true;
            $("#showcase #showcaseContent ul").css("marginLeft", "-175px");
            $("#showcase #showcaseContent ul").prepend($("#showcase #showcaseContent ul li:last").clone());

            $("#showcase #showcaseContent ul").animate({marginLeft:'+=175'}, 1000,function(){
                $("#showcase #showcaseContent ul li:last").remove();
                _inAnimation = false;
            });

        });
    };

    $.showcaseDemo = function(){
        $("#showcase #showcaseContent ul").css("width", ( $("#showcase #showcaseContent ul li").length*175 ) / 2);
        var _inAnimation = false;
        $("#showcase #showcaseGoRight").click(function(){
            if(_inAnimation) return false;
            else _inAnimation = true;
            $("#showcase #showcaseContent ul").animate({marginLeft:'-=175'}, 500,function(){
                $("#showcase #showcaseContent ul").append($("#showcase #showcaseContent ul li:first").clone());
                $("#showcase #showcaseContent ul").css("marginLeft", "0px");
                $("#showcase #showcaseContent ul li:first").remove();
                _inAnimation = false;
            });
        });
        $("#showcase #showcaseGoLeft").click(function(){
            if(_inAnimation) return false;
            else _inAnimation = true;
            $("#showcase #showcaseContent ul").css("marginLeft", "-175px");
            $("#showcase #showcaseContent ul").prepend($("#showcase #showcaseContent ul li:last").clone());

            $("#showcase #showcaseContent ul").animate({marginLeft:'+=175'}, 500,function(){
                $("#showcase #showcaseContent ul li:last").remove();
                _inAnimation = false;
            });

        });
    };

    $.news = {
        timing:5000,
        init:function(slide){
            this.slide = slide;
            $("#homeNewsContent>div>ul").css({
                height:$("#teaser>div>ul>li").length*90,
                padding:0,
                margin:0
            });
            return this;
        },
        prev:function(){
            $("#homeNewsContent>div>ul").css("marginTop", "-98px");
            $("#homeNewsContent>div>ul").prepend($("#homeNewsContent>div>ul>li:last").clone());

            $("#homeNewsContent>div>ul").animate({marginTop:'+=98'}, 1000,function(){
                $("#homeNewsContent>div>ul>li:last").remove();
            });
        },
        next:function(){
            $("#homeNewsContent>div>ul").animate({marginTop:'-=98'}, 1000,function(){
                $("#homeNewsContent>div>ul").append($("#homeNewsContent>div>ul>li:first").clone());
                $("#homeNewsContent>div>ul>li:first").remove();
                $("#homeNewsContent>div>ul").css("margin", "0px");
            });
        },
        animate:function(){
            this.next();
        },
        start:function(){
            $("#homeNewsContent").everyTime(this.timing,'news',function() {
                $.news.animate();
            });
        },
        stop:function(){
            $("#homeNewsContent").stopTime('news');
        }
    };

    $.teaser = {
        timing:5000,
        init:function(slide){
            this.slide = slide;
            $("#teaser>div ul").css({
                width:$("#teaser>div ul li").length*820,
                padding:0,
                margin:0
            });
            return this;
        },        
        prev:function(){
            $("#teaser>div ul").css("marginLeft", "-820px");
            $("#teaser>div ul").prepend($("#teaser>div ul li:last").clone());

            $("#teaser>div ul").animate({marginLeft:'+=820'}, 2000,function(){
                $("#teaser>div ul li:last").remove();
            });
        },
        next:function(){
            $("#teaser>div ul").animate({marginLeft:'-=820'}, 2000,function(){
                $("#teaser>div ul").append($("#teaser>div ul li:first").clone());
                $("#teaser>div ul").css("margin", "0px");
                $("#teaser>div ul li:first").remove();
            });
        },
        animate:function(){
            this.next();
        },
        start:function(){
            $("#teaser").everyTime(this.timing,'teaser',function() {
                $.teaser.animate();
            });
        },
        stop:function(){
            $("#teaser").stopTime('teaser');
        }
    };

    $.museumImages = {
        timing:4000,
        init:function(){
            $("#museumImagesThumbs>div ul").css({
                width:($("#museumImagesThumbs>div ul li").length+1)*75,
                margin:0
            });
            return this;
        },
        prev:function(){
            $("#museumImagesThumbs>div ul").css("marginLeft", "-75px");
            $("#museumImagesThumbs>div ul").prepend($("#museumImagesThumbs>div ul li:last").clone());
            $("#museumImagesThumbs>div ul").animate({marginLeft:'+=75'}, 1000,function(){
                $("#museumImagesThumbs>div ul li:last").remove();
            });
            return this;
        },
        next:function(){
            $("#museumImagesThumbs>div ul").append($("#museumImagesThumbs>div ul li:first").clone());

            $("#museumImagesThumbs").oneTime(250,'xxx',function() {
                $("#museumImagesThumbs>div ul").animate({marginLeft:'-=75'}, 1000,function(){
                    $("#museumImagesThumbs>div ul").css("margin", "0px");
                    $("#museumImagesThumbs>div ul li:first").remove();
                });
            });
            return this;
        },
        animate:function(){
            this.next();
            return this;
        },
        start:function(){
            $("#museumImagesThumbs").everyTime(this.timing,'museumImagesThumbs',function() {
                $.museumImages.animate();
            });
            return this;
        },
        stop:function(){
            $("#museumImagesThumbs").stopTime('museumImagesThumbs');
            return this;
        }
    };

    $.museumNav = {
        init:function(){
            var maxLi=0;
            var open;
            $(".museumNavCityMuseums").each(function(){
                if( $(this).find("li").length > maxLi ){
                    maxLi = $(this).find("li").length;
                    open = $(this);
                }
            });
        },
        changeArrow:function(){
            var img = $(".open").prev("div").find("img");
            var alt = img.attr("src");
            img.attr("src", img.attr("alt")).attr("alt",alt);
        },
        selected:function(){
            $(".museumNavCityMuseums.open").removeClass("open").hide();
            $(".museumNavCityMuseums li.selected").parents(".museumNavCityMuseums").addClass("open").show();
            this.changeArrow();
        },
        open:function(id){
            this.changeArrow();
            $(".museumNavCityMuseums.open").removeClass("open").hide();
            $("#content"+id).addClass("open").show();
            this.changeArrow();
        }
    };

    $.openAlert = {
        closeBtn:function(haveClose,closeFunc){
            if(closeFunc)
                $("#warn #warnClose").click(closeFunc);
            else
                $("#warn #warnClose").click(function(){$.openAlert.close();});
            
            if(!haveClose)
                $("#warnClose").hide();
            else
                $("#warnClose").show();
        },
        continueBtn:function(haveContinue,continueFunc){
            if(continueFunc)
                $("#warn #warnContinue").click(continueFunc);
            if(!haveContinue)
                $("#warnContinue").hide();
            else
                $("#warnContinue").show();
        },
        continueBtnText:function(text){
            if(text != "")
                $("#warnContinue .btn-wrap-main").html(text);
        },
        close:function(){
            $("#warn").hide(300,function(){
                $("#warnLayout").hide(300,function(){$("#warnContinue").show();}).css({top:"0px",left:"0px",width:"0px",height:"0px"});
            });
        },
        settext:function(text){
            $("#warnContent").html(text);
        },
        setDefaultCount:function(sec){
            $("#warn").oneTime(sec,"setDefaultCount",function(){
                $("#warn #warnContinue").trigger("click");
            });
        },
        open: function(haveContinue,continueFunc){
            $("#warnClose").show();
            var _g = $("#content").width();
            var _s = $("#content").offset().left;
            $("#warnLayout").css( {"width": $("body").width()+"px", "height": $("body").height()+"px", "opacity": "0.85"} ).show(500);
            $("#warn").prependTo("#content").css( {"width": Math.ceil(_g * 0.6)+"px", "left": Math.ceil(_s + (_g * 0.2))+"px"} ).show(500);
            $(window).resize(function() {
                var _g = $("#content").width();
                var _s = $("#content").offset().left;
                $("#warnLayout").css( {"width": $("body").width()+"px", "height": $("body").height()+"px"} );
                $("#warn").css( {"width": Math.ceil(_g * 0.6)+"px", "left": Math.ceil(_s + (_g * 0.2))+"px"} );
            });
            $("#warn #warnClose").click(function(){
                $.openAlert.close();
            });
            if(continueFunc)
                $("#warn #warnContinue").click(continueFunc);
            if(!haveContinue)
                $("#warnContinue").hide();
        }
    };

    $.myCartAjax = {
        openCart:function(productId){
            var position = $("div.addTocart").position();
            $("#myCartAjax").css({top:position.top,left:position.left-90}).show(300);
            $.ajax({
                type: "post",
                url:"/shopping/addToCart/",
                data:"productId="+productId,
                dataType:"json",
                beforeSend:function(){
                    $("#myCartAjax img.loading").show();
                },
                success:function(r){
                    if(r.success){
                        $("#myCartAjax #myCartAjaxTotal span").html(r.cart.totalNoShip);
                        $("#myCartAjax #myCartAjaxDetails ul").html("");
                        $.each(r.cart.item,function(i,v){
                            $("#myCartAjax #myCartAjaxDetails ul").append('<li><img src="'+v.media+'" alt="'+v.title+'" /><span><a href="'+v.link+'">'+v.title+'</a></span>'+(v.quantity > 1 ? "<br />"+v.quantity+" Adet" : "")+'<br />'+v.price+'</li>');
                        });
                    }
                    $("#myCartAjax img.loading").hide();
                },
                timeout: 5000,
                error: function (xhr, ajaxOptions, thrownError){}
            });
        },
        closeCart:function(){
            $("#myCartAjax").hide(300);
        },
        bindClose:function(){
            $(document).bind("click",function(event){
                if( !$(event.target).is('#myCartAjax *') && !$(event.target).is('#myCartAjax') ){
                    $.myCartAjax.closeCart();
                    $(document).unbind("click");
                }
            });
        },
        bindOpen:function(){
            $(".addTocart a").bind("click",function(){
                $.myCartAjax.openCart($(this).siblings("input").val());
                $.myCartAjax.bindClose();
                return false;
            });
        }
    };    
    $.myCartAjax.bindOpen();
    
});


