jQuery(document).ready(function() {

    function showImage(url) {
        jQuery(".tofancy").remove();
        var link = document.createElement("a");
        //tot test
        //url = url.replace(/images/,"img");
        link.href = url;
        link.className = "tofancy";
        jQuery("body").append(link);
        jQuery(".tofancy").fancybox();
        jQuery(".tofancy").click();
    }

    function download(file){
		var pageTracker = _gat._getTracker('UA-696292-3');
        pageTracker._trackPageview(file);
    }

    var jcontainer = jQuery(".content");
    jQuery(jcontainer)
        //.css({top:top,left:left})
        .fadeIn(3000,function () {
            jQuery(".home_link a").bind("click", function () {
                if(jQuery("#flipbox .slider_container").length == 0) {
                    jQuery("#flipbox").flip({
                        direction: "lr",
                        color: "#fff",
                        content: jQuery("#" + jQuery(this).attr("class")).html(),
                        onEnd: function () {
                            jQuery("#flipbox").css({backgroundColor:"transparent"});
                            jQuery(".slides").innerfade({
                                speed: 'slow',
                                timeout: 4000,
                                type: 'sequence',
                                containerheight: '100px'
                            });
                        }
                    });
                }
                return false;
            });

            jQuery("#flippad a").bind("click",function(){
                var $this = jQuery(this);
                if($this.attr("selected")) {
                    return false;
                }else{
                    jQuery("#flippad a").removeAttr("selected");
                    $this.attr("selected","selected");
                    jQuery("#flipbox").flip({
                        direction: $this.attr("rel"),
                        color: $this.attr("rev"),
                        content: jQuery("#" + $this.attr("class")).html(),                        
                        /*
                        onBefore: function(){
                            if($this.attr("class")=="models") jQuery("#flipbox").html('');
                        },                        
                        onAnimation: function(){
                                console.log('in the middle of the animation');
                        },
                        */
                        onEnd: function(){
                            
                            jQuery(".logo_flickr").mouseover(function() {
                               jQuery(this).fadeTo("slow",1); 
                            }).mouseout(function () {
                                jQuery(this).fadeTo("slow",0.7);
                            });

                            switch($this.attr("class")) {
                                case "models":
                                    var lastflow = jQuery(".imageflow:last")
                                    lastflow.attr("id","reflection_2");
                                    lastflow.prevAll(".imageflow").attr("id","reflection");
                                    var reflection_2 = new ImageFlow();
                                    var reflection = new ImageFlow();
                                    reflection_2.init({
                                        ImageFlowID: 'reflection_2', 
                                        reflections: false,
                                        slider: false,
                                        circular: true,
                                        opacity: true,
                                        imageCursor: "pointer",
                                        onClick: function() {
                                            showImage(this.url);
                                        }
                                    });
                                    reflection.init({
                                        ImageFlowID: 'reflection', 
                                        reflections: false,
                                        circular: true,
                                        opacity: true,
                                        captions: false,
                                        imageCursor: "pointer",
                                        slider: false,
                                        onClick: function() {
                                            showImage(this.url);
                                        }
                                    });
                                    break;
                                case "manual":

                                    jQuery(".download").fancybox({
                                        type: "iframe",
                                        width: 900,
                                        height: 800,
                                        onComplete: function () {
                                            var file = jQuery(this).attr("rev");
                                            download(file);      
                                        }
                                    })

                                    jQuery(".ficha_dummy").click(function () {
                                        jQuery("#flippad a.ficha").click();        
                                    });
                                    break;
                                case "ficha":
                                    jQuery(".table:last").jScrollPane();
                                    break;
                                case "fotos":
                                    jQuery(".poster").fancybox({
                                        titlePosition: "over",
                                        'onComplete':	function() {
                                            jQuery("#fancybox-wrap").hover(function() {
                                                jQuery("#fancybox-title").slideDown();
                                            }, function() {
                                                jQuery("#fancybox-title").slideUp();
                                            });
                                        }
                                    });
                                    break;
                            }
                        }
                    })
                    return false;
                }
            });            
        });

        jQuery(".slides").innerfade({
            speed: 'slow',
            timeout: 4000,
            type: 'sequence',
            containerheight: '100px'
        });
});

