$(function() {
    
    $("#content_floater .columns#column3 iframe").eq(0).show();
    $("#switchc1").click(function() {
        $("#content_floater .columns#column3 iframe").eq(1).hide();    
        $("#content_floater .columns#column3 iframe").eq(0).show();    
    });
    $("#switchc2").click(function() {
        $("#content_floater .columns#column3 iframe").eq(0).hide();    
        $("#content_floater .columns#column3 iframe").eq(1).show();    
    });
    $(".scroll-pane").jScrollPane({ showArrows: true });
    
    Cufon.replace("#menu ul li a, #content_floater .content h1, #content_floater .content h2, #catalog, #accessories, .news_item h2", {
        hover: true
    });

    Carousel.setItemHeights();
    Carousel.initialize();
    
    $("#catalog ul li a, #accessories ul li a").mouseover(function() {
        $(".menuOverlay").hide();
        var o = $(this).parent().offset();
        if(o.left + 280 > $("body").width()) {
            $(".menuOverlay#overlay_" + $(this).attr("rel")).css({ left: o.left - 180, top: o.top - 10 }).show();
        } else {
            $(".menuOverlay#overlay_" + $(this).attr("rel")).css({ left: o.left + 100, top: o.top - 10 }).show();
        }
    }).mouseout(function() {
        $(".menuOverlay").hide();
    });
        
});
$(window).resize(function() {
    $("#container").height($(window).attr("innerHeight"));
    Carousel.initialize();
});

Carousel = function() {
    
    var initialized = false;
    var maxWidth = 0;
    
    return {
        initialize: function() {
            if(!initialized) {
                $("#carousel").css("width", $("#container").width()).jcarousel({
                    scroll: 2,
                    size: $(".catalog_item").length,
                    itemFallbackDimension: maxWidth
                });
                initialized = true;
            }
            if(this.getItemsWidth() < $(".jcarousel-container").width() - 210) {
                $(".jcarousel-next").addClass("jcarousel-next-disabled");
            } else {
                $(".jcarousel-next").removeClass("jcarousel-next-disabled");
            }
            $("#carousel").css("left", 0);       
        },
        setItemHeights: function() {
            $(".catalog_item").each(function() {
                if(maxWidth < $(this).width()) {
                    maxWidth = $(this).width();        
                }
            });    
            $(".catalog_item").width(maxWidth);
        },
        getItemsWidth: function() {
            var w = 0;
            $(".catalog_item").each(function() {
                w += $(this).width();     
            });
            return w;
        }
    };
        
}();

//Old
function ajax(file, cel, query, mit){
    if(query.match('id:')){
        var keres = query.split("id:");
        query = keres[1]+'='+document.getElementById(keres[1]).value;
    }
    //$("#" + cel).html("<div class=\"loader\"><img src=\"Static/Images/ajax-loader.gif\" alt=\"\" /></div>");
    //$("#" + cel).append("<div class=\"loader\"><center><img src=\"Static/Images/ajax-loader.gif\" alt=\"\" /></center></div>");
    $('.' + cel).append("<div class=\"loader\"><center><img src=\"Static/Images/ajax-loader.gif\" alt=\"\" /></center></div>");
    $("#" + cel).load(file + "?" + query + mit, '', function(){ } );
}
    
//Old
function ajaxFileUpload(Fancy){
    var Fancy;
    var slash;
    
    if(Fancy){
        slash = '/';
    } else {
        slash = "";
    }

    $("#loading")
    .ajaxStart(function(){
        $(this).show();
    })
    .ajaxComplete(function(){
        $(this).hide();
    });

    $.ajaxFileUpload(
        {
            url:slash + 'Modules/Framework/AjaxFileupload.php?fancy='+Fancy+'&uploadtype='+$('#uploadtype').val()+'&filename='+$('#fileName').val()+'&filedesc='+$('#fileDesc').val(),
            secureuri:false,
            fileElementId:'fileToUpload',
            dataType: 'json',

            success: function (data, status){
                if(typeof(data.error) != 'undefined'){
                    if(data.error != ''){
                        //alert(data.error);
                        ajax(slash + 'Templates/Filesupload/Filesupload.php','files','','');
                    }
                    else{
                        //alert(data.msg);
                        ajax(slash + 'Templates/Filesupload/Filesupload.php','files','','');
                    }
                }
            },
            error: function (data, status, e){
                //alert(e);
                ajax(slash + 'Templates/Filesupload/Filesupload.php','files','','');
            }
        }
    )

    $('#fileName').attr('value', '');
    $('#fileDesc').attr('value', '');

    return false;
}
