/* 
 * @package WordPress
 * @subpackage Dereklam
 * @author: A. Kuzmanovski
 */

/*global MediaObject, dlam, GetScrollY, utils */
jQuery.noConflict();
var dl_actual_product_url = "", dl_actual_product_descr = ""; 

function JSCover2(options) {
    var that = this, coverAnimIdLeft = null, coverAnimIdRight = null, ctx,
        m = jQuery(window).width()/2,
        opt = options || {}, adjust = jQuery(opt.adjust_to),
        c = document.getElementById("cover-canvas");
        opt.fillStyle = opt.fillStyle || "rgba(255,255,255,0.8)";
    opt.correct_top = opt.correct_top || function() {return 0;};
    opt.fillStyle = opt.fillStyle;
    opt.inited = false;
    opt.cover_is_open = false;
    opt.lastActualWidth = -333;
    opt.zoom_mask_width = 0;
    opt.image = {src:null, x:0, y:0, x0:0, y0:0, draw: false};
    opt.x = jQuery("#gallery-nav").position().left + jQuery("#gallery-nav").width()/2;
    opt.step = jQuery("#gallery ul .actual").width() * 0.4;
    opt.dx = jQuery("#gallery ul .actual").width()/2;
    opt.animated = false;
    this.get_zoom_mask_width = function() {
        return opt.zoom_mask_width;
    };
    
    this.get_zoom_mask_left = function() {
        return opt.image.x;
    };
    
    
    this.is_on_image = function(mx, my) {
        var y, cp1x, cp1y, cp2x, cp2y, x,dx, step, cp1x2, cp1y2, cp2x2, cp2y2;
        x = opt.x;
        dx = opt.dx;
        step = opt.step;
        
        y = opt.height;
        cp1x = x-step -dx;
        cp1y = opt.height/2;
        cp2x = x-step - dx;
        cp2y = opt.height - opt.height/8;
        opt.zoom_mask_width = cp2x;
        cp1x2 = x+step+dx;
        cp1y2 = opt.height/2;
        cp2x2 = x+step+dx;
        cp2y2 = opt.height/8;
        opt.zoom_mask_width = cp2x2 - opt.zoom_mask_width;
        ctx.save();
        ctx.beginPath();
        ctx.globalCompositeOperation = "xor";
        ctx.fillStyle = "rgba(255,255,255,0.0)";
        opt.x = x;
        ctx.moveTo(x-dx, 0);
        ctx.bezierCurveTo(cp1x,cp1y,cp2x,cp2y,x-dx,y);
        ctx.lineTo(x+dx, opt.height);
        ctx.bezierCurveTo(cp1x2,cp1y2,cp2x2,cp2y2, x+dx, 0);
        ctx.clip();
        var result = ctx.isPointInPath(mx, my);
        ctx.restore();
        
        return result;
    };
    
    var draw = function(x, step, dx) {
        ctx.clearRect(0, 0, c.width, c.height);
        var y, cp1x, cp1y, cp2x, cp2y,cp1x2,cp1y2,cp2x2,cp2y2, sh,sw, image = new Image();
        ctx.fillStyle = opt.fillStyle;
        y = opt.height;
        cp1x = x-step -dx;
        cp1y = opt.height/2;
        cp2x = x-step - dx;
        cp2y = opt.height - opt.height/8;
        opt.zoom_mask_width = cp2x;
        cp1x2 = x+step+dx;
        cp1y2 = opt.height/2;
        cp2x2 = x+step+dx;
        cp2y2 = opt.height/8;
        opt.zoom_mask_width = cp2x2 - opt.zoom_mask_width;
        if (opt.image.draw) {
            ctx.save();
            ctx.beginPath();
            ctx.globalCompositeOperation = "xor";
            ctx.fillStyle = "rgba(255,255,255,0.0)";
            opt.x = x;
            ctx.moveTo(x-dx, 0);
            ctx.bezierCurveTo(cp1x,cp1y,cp2x,cp2y,x-dx,y);
            ctx.lineTo(x+dx, opt.height);
            ctx.bezierCurveTo(cp1x2,cp1y2,cp2x2,cp2y2, x+dx, 0);
            ctx.clip();
            image.src = opt.image.src;
            sh = opt.height;
            sw = opt.zoom_mask_width;
            //console.log("draw:"+opt.image.x0 + " " + opt.image.y0 );
            ctx.drawImage(image,opt.image.x0, opt.image.y0, sw,sh, opt.image.x, opt.image.y, sw, sh);
            //console.log(sw + " " + sh);
            //console.dir(opt.image);
            ctx.restore();
            
        }
        ctx.beginPath();
        ctx.fillStyle = opt.fillStyle;
        ctx.moveTo(x-dx, 0);
        ctx.bezierCurveTo(cp1x,cp1y,cp2x,cp2y,x-dx,y);
        ctx.lineTo(0,opt.height);
        ctx.lineTo(0,0);
        ctx.lineTo(jQuery(adjust).width(), 0);
        ctx.lineTo(jQuery(adjust).width(), opt.height);
        ctx.lineTo(x+dx, opt.height);
        ctx.bezierCurveTo(cp1x2,cp1y2,cp2x2,cp2y2, x+dx, 0);
        ctx.fill();
        
        jQuery("#gallery-store-adress").css("left", x + dx).width("200px");
        
        opt.x = x;
        opt.step = step;
        opt.dx = dx;
    };
    
    this.draw_image = function(src,x,y, drawIt) {
        opt.image.src = src;
        opt.image.draw = drawIt;
        opt.image.x = opt.x - this.get_zoom_mask_width()/2;
        opt.image.y = 0;
        opt.image.y0 = y;
        opt.image.x0 = x;      
        draw(opt.x, opt.step, opt.dx);
    };
    
    
    
    var setParams = function() {
        opt.height = jQuery(adjust).height();
        jQuery("#cover-container")
                .css("top", - opt.height - opt.correct_top())
                .css("left", dlam.data.left_width);
        jQuery("#cover-canvas")
                .attr("height", opt.height)
                .attr("width", jQuery(adjust).width() +  "px");
    };
        
    this.init = function() {        
        var step, dx, x;
        setParams();
        var actualWidth = jQuery("#gallery ul .actual").width(),
            pos = jQuery("#gallery-nav").position(),
            navWidth = jQuery("#gallery-nav").width();
        ctx = c.getContext("2d");
        x = pos.left + navWidth/2;
        step = jQuery("#gallery ul .actual").width() * 0.4;
        dx = jQuery("#gallery ul .actual").width()/2;
        dx = step = 0;
        draw(x, step, dx);
        
        opt.cover_is_open = false;
        opt.inited = true;
    };
    
    this.on_resize = function(options) {
        if (opt.inited) {
            setParams();
            ctx = c.getContext("2d");
            var actualWidth = jQuery("#gallery ul .actual").width(),
                pos = jQuery("#gallery-nav").position(),
                navWidth = jQuery("#gallery-nav").width(),
                step = actualWidth * 0.4,
                dx = actualWidth/2,
                x = pos.left + navWidth/2;
            draw(x, step, dx);    
        }
    };
    
    this.animate = function(options) {
        if (opt.animated == true) {
           // return;
        }
        opt.animated = true;
        options = options || {};
        var step, x, d, dx, steps= 0, maxSteps = 10, width = jQuery(adjust).width(),
            actualWidth = jQuery("#gallery ul .actual").width(),
            pos = jQuery("#gallery-nav").position(),
            that0 = this,
            navWidth = jQuery("#gallery-nav").width();
            
        if (opt.lastActualWidth !== actualWidth) {    
            d = Math.abs((actualWidth/2) - (opt.lastActualWidth/2));
            x = pos.left + navWidth/2;
                
//            coverAnimIdLeft = window.setInterval(function(){
//                jQuery("#cover-canvas")
//                    .attr("height", opt.height)
//                    .attr("width", width);
//                step = 0.4 * steps * actualWidth / maxSteps;
//                if (opt.lastActualWidth < actualWidth) {
//                    dx = opt.lastActualWidth/2 + steps * d / maxSteps;
//                } else {
//                    dx = opt.lastActualWidth/2 - steps * d / maxSteps;
//                }
//                draw(x, step, dx);
//                steps += 1;
//                if (steps > maxSteps) {
//                        window.clearInterval(coverAnimIdLeft);
//                        opt.cover_is_open = true;
//                        opt.lastActualWidth = actualWidth;
//                        opt.dx = dx;
//                        opt.x = x;
//                        opt.step = step;
//                        if (is_defined(options.complete_fun)) {
//                            options.complete_fun();
//                        }   
//                    }
//            }, 50);
            
            
            this.anime = function(){
                jQuery("#cover-canvas")
                    .attr("height", opt.height)
                    .attr("width", width);
                step = 0.4 * steps * actualWidth / maxSteps;
                if (opt.lastActualWidth < actualWidth) {
                    dx = opt.lastActualWidth/2 + steps * d / maxSteps;
                } else {
                    dx = opt.lastActualWidth/2 - steps * d / maxSteps;
                }
                draw(x, step, dx);
                steps += 1;
                if (steps > maxSteps) {
                        window.clearInterval(coverAnimIdLeft);
                        opt.cover_is_open = true;
                        opt.lastActualWidth = actualWidth;
                        opt.dx = dx;
                        opt.x = x;
                        opt.step = step;
                        if (is_defined(options.complete_fun)) {
                            options.complete_fun();
                            opt.animated = false;
                        }   
                    } else {
                        setTimeout(that0.anime, 60);
                    }
            };
            
            this.anime();
        }
    };
 
 
    this.get_left2_width = function() {
        return jQuery("#canvas-left").width();
    };
}



/**
 * 
 */
function JSCover(options) {    
    var that = this, opt = options || {}, adjust = jQuery(opt.adjust_to); 
    opt.left2 = {};
    opt.right1 = {};
    opt.right = {};
    opt.left = {};
    opt.speed = opt.speed || 2000;
    opt.correct_top = opt.correct_top || function() {return 0;};
    
    if (is_defined(options.get_animation_left)) {
        opt.get_animation_left2 = function () {
            var w = options.get_animation_left() - opt.left2.width;
            return (w < 0)? 0 : w;
        };
    }else {
        opt.get_animation_left2 = function () {return 0;};
    }
    
    opt.get_animation_right =   opt.get_animation_right || 
                                function () {return opt.width - opt.right1.width;};
                            
    this.init = function() {
        
       
        
        
        this.set_params();
        
        
        jQuery("#lb-cover-left").width(opt.middle + opt.left2.width);
        jQuery("#lb-cover-right").width(opt.middle + opt.right1.width);    
        if (is_defined(opt.start_left)) {
            jQuery("#lb-cover-left1").width(opt.start_left);
        } else {
            //alert(opt.middle);
            //jQuery("#lb-cover-left1").width(opt.middle -1);
            jQuery("#lb-cover-left1").width(opt.get_animation_left2());
        }
        jQuery("#lb-cover-right2").width(opt.middle -20);
        opt.right.left = opt.middle - opt.right1.width;
        if (is_defined(opt.start_right)) {
            jQuery("#lb-cover-right").css("left", opt.start_right - opt.right1.width);
        } else {
            jQuery("#lb-cover-right").css("left", opt.get_animation_right());
        }
    };
    
    this.get_left2_width = function() {
        return jQuery("#lb-cover-left2").width();
    };
    
    this.set_params = function() {
        opt.height = jQuery(adjust).height();
        opt.width = jQuery(adjust).width();
        opt.middle = Math.ceil(opt.width/2);
        jQuery("#lb-cover-left").height(opt.height)
            .css("top", -opt.height - opt.correct_top());    
        jQuery("#lb-cover-right").height(opt.height)
            .css("top", -2*opt.height - opt.correct_top());
        jQuery("#lb-cover-left2").height(opt.height);
        jQuery("#lb-cover-right1").height(opt.height);       
        opt.right1.width = jQuery("#lb-cover-right1").width();
        opt.left2.width = jQuery("#lb-cover-left2").width();
        opt.right1.width = jQuery("#lb-cover-right1").width();
    };
    
    this.on_resize = function() {
        this.set_params();
        opt.right.left = opt.width - opt.right1.width;
        jQuery("#lb-cover-right").css("left", opt.get_animation_right());
        jQuery("#lb-cover-left1").width(opt.get_animation_left2());
        alert(jQuery("#lb-cover-right").css("top"));
    };
    
    this.animate = function(options) {
        options = options || {};
        
        that.set_params();    
        var w = jQuery("#lb-cover-left2").width() + opt.get_animation_left2();
        if (w > jQuery("#lb-cover-left").width()) {
            jQuery("#lb-cover-left").width(w);
        }
        
        
        
        setTimeout(function() {
            that.set_params();  
                        
            jQuery("#lb-cover-left1").animate({width: opt.get_animation_left2()},opt.speed, function() {
                //var w = jQuery("#lb-cover-left1").width() + jQuery("#lb-cover-left2").width();
                //jQuery("#lb-cover-left").width(w);
                
                
                
                
            });
            jQuery("#lb-cover-right").animate({left: opt.get_animation_right()}, opt.speed, function(){
                if (is_defined(options.complete_fun)) {
                    options.complete_fun();
                }
            });
        }, dlam.cover.timeout);
    };
    this.on_zoom_hover = function() {
        
    };
}//constructor

function JSMediaObject() {
    var that = this;
    this.parent = extend(this, new MediaObject());
    this.name = "JSMediaObject";
    this.mediatype = "javascript";
    this.init_events = function() {
        _l("JSMediabject: init_events");
        jQuery(window).resize(function() {
            that.on_resize();
        });
    };
}//constructor
/* ------------------------------------------------------------------------ */
function JSVideo() {
    this.parent =extend(this, new JSMediaObject());
    this.name = "JSVideo";
    
    this.objecttype = "video";
    this.init = function() {
        jQuery("#video-container").hide();
        this.parent.init();        
        var height = jQuery("#video-container object").height();
        jQuery("#video-cover").height(height);
        this.init_events();
    };
    this.content_fadein_step = function(now) {
        if (now === 1) {
            var height = jQuery("#video-container object").height();
            jQuery("#video-container").show();
            jQuery("#video-cover").css("top", -height);
            jQuery("#video-cover").css("margin-bottom", -height);
        }
    };
    this.on_resize = function() {
       //functioniert nicht
    };
    this.start = function() {
        jQuery("#video-cover").animate({"opacity": 0}, dlam.video.cover_animation_speed, function() {
            jQuery("#video-cover").remove();
        });
    };    
}//constructor
/* ------------------------------------------------------------------------ */

function show_share_email(ajax_form) {
   
    var id = "#share-email-form";
    if (jQuery(id).hasClass("active")) {
        jQuery(id).removeClass("active");
        jQuery(id).fadeOut();
    } else {
        jQuery(id).addClass("active");
        jQuery(id).fadeIn("slow", function(){
            jQuery('#share-form').ajaxForm({
                target:		'#share-email-form .message'
            }); 
        }); 
        
    }
}

/**
 * Shows a popup window
 * 
 * @param name Name of the function to popup ("Share This", "Personal Shopper", etc. see popup-functions.php)
 * @param index Index to a model in the lookbook (given by flash lookbook)
 */
function on_lookbook_function(name, index) {
    var close_fun = function() {
            jQuery(".gallery-popup").fadeOut();
            dlam.lookbook.functions.is_visible = "";
        },
        show_fun = function () {
            var descr= "", url = (window.location) + '', p = url.indexOf("#"), nr = url.substring(p + 2);
            if (index == undefined) {
                if (p !== -1) {
                    url = url.substring(0, p - 1);
                    url = url + "?nr=" + nr;
                } else {
                    url = url + "?nr=0";
                }    
            } else {
                url = url + "?nr=" + index;
            }
            if (dl_actual_product_url != "") {
                url = dl_actual_product_url;
            }
            if (dl_actual_product_descr != "") {
                descr = dl_actual_product_descr;
            }
            jQuery.ajax({
		type: "POST",
		url: "/dl-media/ajax/popup-functions.php",
		data: "name="+name+"&url=" + url + "&descr=" + descr + "&index=" + index,
		success: function(json_data){
                    var html = "", data = JSON.parse(json_data);
                    html += "<div id='close-popup'>X</div>";
                    html += "<p>" + data.title.toUpperCase() + "</p>";
                    html += "<p>" + data.html + "</p>";
                    jQuery(".gallery-popup").html(html);
                    jQuery("#close-popup").bind("click", function() {
                        on_lookbook_function(name);
                    });
                    var top = GetScrollY() + jQuery(window).height()/2 - jQuery(".gallery-popup").height()/2;
                    jQuery(".gallery-popup").css("top", top);
                    jQuery(".gallery-popup").fadeIn('slow', function() {});                     
                    dlam.lookbook.functions.is_visible = name;
                }});
        };
    if (name === "close" || (name === "Close Overlay")) {
        close_fun();
        return;
    }    
    if (dlam.lookbook.functions.is_visible !== "") {
        if (dlam.lookbook.functions.is_visible === name) {
            close_fun();
            return;
        }
        jQuery(".gallery-popup").fadeOut(100, show_fun);
        return;
    }
    show_fun();
}//fun

/**
 * Summary
 */
function JSLookbook() {
    var that = this, 
        priv = {
            gallery_ul: jQuery("#gallery ul"),
            gallery_nav: jQuery("#gallery-nav"),
            gallery_li: jQuery("#gallery ul li"),
            win: {width: 0, width2: 0},
            actual: {obj: null, width: 0, width2: 0, pos: null, height: 0},
            popup: false,
            info: false,
            zoom: false
            
    }, cover_opts = {adjust_to: "#gallery ul", 
                     correct_top: function() {
                         var h = jQuery(priv.gallery_nav).height() + 1;
                         return h;
                     },
                     speed: dlam.lookbook.cover_animation_speed,
//                     start_left: Math.ceil(jQuery(window).width()/2),
//                     start_right: Math.ceil(jQuery(window).width()/2),
                     get_animation_left: function() {
                        priv.calc_actual();
                        var left = dlam.data.left_width + priv.actual.pos.left;
                        return left;
                     },
                     get_animation_right: function() {
                        priv.calc_actual();
                        var right = dlam.data.left_width + priv.actual.pos.left + priv.actual.width;
                        return right;
                     }};
    
    this.cover = new JSCover2(cover_opts);
    
    this.parent =extend(this, new JSMediaObject());
    this.name = "JSLookbook";
    this.objecttype = "lookbook";
    
    
    this.get_priv = function() {return priv;};
    
    priv.calc_actual = function() {
        priv.gallery_li = jQuery("#gallery ul li");
        priv.win.width = jQuery(window).width();
        priv.win.width2 = Math.ceil(priv.win.width/2);
        priv.actual.obj = jQuery("#gallery ul .actual");
        priv.actual.pos = jQuery(priv.actual.obj).position();
        priv.actual.width = jQuery(priv.actual.obj).width();
        priv.actual.width2 = Math.ceil(priv.actual.width/2);
        priv.actual.height = jQuery(priv.actual.obj).height();
        dlam.data.left_width = - priv.actual.pos.left - priv.actual.width2 + priv.win.width2;
    };   
    
    this.on_resize = function(cover) {
        if (!is_defined(cover)) {
            cover = true;
        }
        priv.calc_actual();
        jQuery(priv.gallery_ul).css("left", dlam.data.left_width);
        jQuery("#gallery-nav-container").css("left", dlam.data.left_width);
        jQuery(priv.gallery_nav).width(priv.actual.width).css("left", priv.actual.pos.left);
        priv.show_info(false);
        if (cover) {
            this.cover.on_resize({middle: priv.actual.pos.left + priv.actual.width/2});
        }
        var width = priv.actual.width + 2*that.cover.get_left2_width();
        jQuery("#gallery-image-zoom").width(width);
        jQuery("#gallery-image-zoom").css("left", priv.win.width2 - Math.ceil(width/2));
    };
    
    this.init_events = function(with_parent) {
        if (with_parent !== false) {
            that.parent.init_events.call(this);
        }
        jQuery("#gallery-prev").click(function() {
            priv.on_prev();
        });
        jQuery("#gallery-next").click(function() {
            priv.on_next();
        });
        jQuery("#gallery-zoom").click(function() {
            priv.on_zoom();
        });
        jQuery("#gallery-info").click(function() {
            priv.on_info();
        });
        jQuery("#gallery-image-zoom-mask").mousemove(function() {
            priv.on_zoom_mousemove();
        });
        jQuery("#cover-canvas").mousemove(function(evt) {
            priv.on_zoom_mousemove(evt);
        });
    };
        
    this.parse_model_nr = function(url) {
        var nr = 0, i, j;
        url = url + '';
        i = url.indexOf("#");
        j = url.indexOf("?");
        if (i >= 0) {
            j = (j === -1)? url.length : j;
            nr = parseInt(url.substring(i+2,j), 10);
        }
        
        return nr;
    };
    this.init = function() {
        var url = window.location,nr = that.parse_model_nr(url);
        dlam.data.index = nr;
        image = jQuery("#item" + nr).attr("src");
        jQuery("#share_image").attr("content","http://dereklam.com" + image);
        //alert(nr);
        priv.calc_actual();
        this.on_resize(false);
        this.init_events();
        that.cover.init();
        jQuery("#gallery-info-field").width("100px")
            .height(priv.actual.height)
            .css("margin-bottom", -priv.actual.height);
        jQuery("#gallery-nav-container").css("left", dlam.data.left_width);
        jQuery(priv.gallery_nav).width(priv.actual.width).css("left", priv.actual.pos.left);
        
        dl_actual_product_descr = jQuery("#blog_title").val();
        
        
    };
    
    this.start = function(complete_fun) {
        this.on_resize(false);
        
        var complete = function() {
            var url = window.location, nr = that.parse_model_nr(url), i = 1, temp, image;
          
            temp = dlam.lookbook.easing.duration;
            dlam.lookbook.easing.duration = 0;
            that.set_image_animation_complete(function() {
                if (i < nr) {
                    i++;
                    priv.on_next();
                } else {
                    that.set_image_animation_complete(function(){});
                    dlam.lookbook.easing.duration = temp;
                    that.cover.animate();
                    
                }
            });
            if (nr>0) {
                priv.on_next();
            } else {
                dlam.lookbook.easing.duration = temp;
            }
            
          
          
            if (is_defined(complete_fun)) {
                complete_fun();
            }
            
        };
        
        this.cover.animate({complete_fun: complete});
       
    };
    
    priv.image_animation_complete = function() {};
    this.set_image_animation_complete = function(f) {
        priv.image_animation_complete = f;
    };
    priv.image_animation = false;
    priv.animate_image = function(side) {
        if (priv.image_animation) {
            return;
        }
        priv.image_animation = true;
        jQuery(priv.gallery_ul).stop(true, true);
        var anim = {"left": dlam.data.left_width},
            easing = dlam.lookbook.easing;
        easing.complete = function(){
            //priv.swap_image(side);
            priv.calc_actual();
            jQuery("#gallery ul").css("left", dlam.data.left_width);
            priv.image_animation = false;
            setTimeout(priv.image_animation_complete, 50);
            window.location = "#/" + jQuery(".actual").parent().attr("item-nr");
            dl_actual_product_descr = jQuery("#blog_title").val();
        };
        jQuery(priv.gallery_ul).animate(anim, easing); 
        that.cover.animate();
        
        if (priv.info) {
            on_lookbook_function("close");
            jQuery("#gallery-info-field .content").animate({opacity:0},dlam.lookbook.easing.duration/2, function() {
                jQuery("#gallery-info-field .content").html(priv.get_item_info())
                .animate({opacity:1}, dlam.lookbook.easing.duration/2);
            });
        }
    };
    
    priv.swap_image = function(side, new_actual) {
        var item = (side === "move_first_to_last") ? jQuery(priv.gallery_li).first() : jQuery(priv.gallery_li).last(),
            nr = jQuery(item).attr("item-nr"),
            width = jQuery(item).css("width"),
            width2 = Math.ceil(width/2),
            height = jQuery(item).css("height"),
            html = jQuery(item).html(), html_new = "";
        jQuery(item).hide();
        html_new = "<li item-nr='" + nr + "' id='item-container" + nr + "' "; 
        html_new += "style='width:"+width+ ";height:" + height + ";'>" + html + "</li>";
        jQuery(item).remove();
            
        if (side === "move_first_to_last") {
            priv.calc_actual();
            jQuery("#gallery ul").css("left", dlam.data.left_width);
            jQuery(html_new).insertAfter(jQuery(priv.gallery_li).last());           
        } else {
            jQuery(html_new).insertBefore(jQuery("#gallery ul li").first());
            priv.calc_actual();
            jQuery("#gallery ul").css("left", dlam.data.left_width); 
            
        }
        jQuery(priv.actual.obj).removeClass("actual");
        jQuery(new_actual).addClass("actual"); 
        priv.calc_actual();
        if (priv.zoom) {
            priv.on_zoom(true);
        }
        priv.animate_image(side);
    };
    
    priv.on_next = function() {
        if (!priv.image_animation) {
            var next_id = "#" + jQuery(priv.actual.obj).attr("next"),
                nr = jQuery(".actual").parent().attr("item-nr");
            priv.swap_image("move_first_to_last", next_id);
            
        }
    };
    
    priv.on_prev = function() {
        if (!priv.image_animation) {
            var prev_id = "#" + jQuery(priv.actual.obj).attr("prev"),
                nr = jQuery(".actual").parent().attr("item-nr");
            priv.swap_image("move_last_to_first", prev_id);
        }
    };
    
    priv.on_zoom_mousemove = function(evt) {
        if (!priv.zoom) {
            return;
        }
        var x1,x0,y0, pos = jQuery("#cover-canvas").offset(),
            x = utils.mouse.x - pos.left, y = utils.mouse.y - pos.top,
            w = that.cover.get_zoom_mask_width(), h = jQuery("#gallery ul").height(),
            nr = jQuery(".actual").parent().attr("item-nr"),
            src = jQuery(".actual").parent().find(".item-zoom").html(),
            hi = dlam.data.items[nr].image_zoom.height, wi = dlam.data.items[nr].image_zoom.width;
        //console.log("In?" + that.cover.is_on_image(x,y));
        //console.log(x + " " + y + ", " + w + " " + h);
        if (that.cover.is_on_image(x,y)) {
            // Abbilden auf grosses Bild
            x1 = x - that.cover.get_zoom_mask_left();
            x0 = (wi - w) * x1 / w;
            y0 = (hi - h) * y / h;
            if (x0 > (wi-w)) { x0 = wi-w;}
            if (y0 > (hi-h)) { y0 = hi-h;}
            //console.log(x0 + " " + y0 + ", " + wi + " " + hi);
            that.cover.draw_image(src, x0, y0, true);
        }
    };
    
    priv.on_zoom = function(close) {
        var x,y, img = "", nr, src, h, w, height,         mask_width;
       if (is_defined(close) && close === true) {
           priv.zoom = true;
       }
       
       nr = jQuery(".actual").parent().attr("item-nr");
       //img = jQuery(".actual").parent().find(".item-zoom").html();
       src = jQuery(".actual").parent().find(".item-zoom").html();
       h = dlam.data.items[nr].image_zoom.height;
       w = dlam.data.items[nr].image_zoom.width;
       mask_width = that.cover.get_zoom_mask_width();
       
       img = new Image();
       img.src = src;
       height = jQuery("#gallery ul").height();
       x = - mask_width/2 + w/2;
       y = - height/2  + h/2;
       //console.log (x + " " + y);
       
       if (priv.zoom) {
           jQuery("#gallery-image-zoom").fadeOut();
           jQuery("#gallery-image-zoom-mask").fadeOut();
           priv.zoom = false;
           that.cover.draw_image(src, x, y, false);
           return;
       }
       img.onload = function() {
            that.cover.draw_image(src, x, y, true);
            priv.zoom = true;
            priv.on_info(true);    
       };
    };
    
    priv.get_function = function() {
        
        var text = "", i=0, count = 0, title = "", name = "";
        text += "<div style='width: " + priv.actual.width + "px; position:absolute; bottom:0;vertical-align: bottom; padding:10px'>";
        if (dlam.data.functions == undefined) {
            return text + "</div>";
        }
        
        for (i = 0, count = dlam.data.functions.length; i < count; i++) {
            name = dlam.data.functions[i]["function"];
            title = dlam.data.functions[i]["label"];
            text += "<br><a class='lookbook-function' onclick='on_lookbook_function(\""+ name +"\");'>" + title + "</a>";
        }
        text += "</div>";
        return text;
    };
    
    priv.get_item_info = function() {
      var   text = "", style = "",
            nr = jQuery(".actual").parent().attr("item-nr"),
            title = jQuery("#item-title" + nr).html(),
            info = jQuery("#item-text" + nr).html();
      text = "<div class='content'>"; 
      style = "padding:10px;position: absolute; z-index: 50000; width: "+ priv.actual.width + "px";
      text += "<div style='" + style +"'>" + title + "<br><br>" + info + "</div>";
      text += priv.get_function();
      text += "</div>";
                
      return text;
    };
    
    priv.show_info = function(no_fadein) {
        jQuery("#gallery-info-field").css("left", priv.win.width2 + priv.actual.width2);
        jQuery("#gallery-info-field .content").html(priv.get_item_info());

        jQuery("#gallery-info-field .background").width(2 * priv.actual.width);
        jQuery("#gallery-info-field .background div").width(priv.actual.width)
            .height(priv.actual.height);
        jQuery("#gallery-info-field .background img").height(priv.actual.height);

        if (no_fadein !== false) {
            jQuery("#gallery-info-field").fadeIn(dlam.lookbook.easing.duration);
        }
    };
    
    priv.on_info = function(close) {
        if (is_defined(close) && close === true) {
            priv.info = true;
        }
        if (priv.info) {
            priv.info = false;
            jQuery("#gallery-info-field").fadeOut(dlam.lookbook.easing.duration);
            on_lookbook_function("close");
        } else {
            priv.show_info();
            priv.info = true;
            if (priv.zoom) {
                priv.on_zoom(true);
            }
        }
    };
    
    priv.on_popup = function() {
        if (priv.popup) {
            jQuery(".gallery-popup").fadeOut();
            priv.popup = false;
        } else {
            var text = "popup";
            jQuery(".gallery-popup").html(text).fadeIn();
            priv.popup = true;
        }
        
    };
    
}//constructor



function JSStore() {
    var that = this;
    this.name = "JSStore";
    this.parent =extend(this, new JSLookbook());
    this.objecttype = "store";
    this.init = function() {
        that.parent.init.call(this);
        jQuery("#gallery-store-adress").animate({opacity:0});
    };
    this.start = function() {
        var complete_fun = function() {
            jQuery("#gallery-store-adress").animate({opacity: 1}, 2*dlam.lookbook.cover_animation_speed);
        };
        that.parent.start.call(this, complete_fun);        
        
    };    
}//constructor


function JSInspiration() {
   var that = this, 
        priv = {
            gallery_ul: jQuery("#gallery ul"),
            gallery_nav: jQuery("#gallery-nav"),
            gallery_li: jQuery("#gallery ul li"),
            win: {width: 0, width2: 0},
            actual: {obj: null, width: 0, width2: 0, pos: null, height: 0},
            popup: false,
            info: false,
            zoom: false
            
    }, cover_opts = {adjust_to: "#gallery ul", 
                     correct_top: function() {
                         var h = jQuery(priv.gallery_nav).height() + 1;
                         return h;
                     },
                     speed: dlam.lookbook.cover_animation_speed,
//                     start_left: Math.ceil(jQuery(window).width()/2),
//                     start_right: Math.ceil(jQuery(window).width()/2),
                     get_animation_left: function() {
                        priv.calc_actual();
                        var left = dlam.data.left_width + priv.actual.pos.left;
                        return left;
                     },
                     get_animation_right: function() {
                        priv.calc_actual();
                        var right = dlam.data.left_width + priv.actual.pos.left + priv.actual.width;
                        return right;
                     }};
    
    this.cover = new JSCover2(cover_opts);
    
    this.parent =extend(this, new JSMediaObject());
    this.name = "JSInspiration";
    this.objecttype = "inspiration";
    
    
    this.get_priv = function() {return priv;};
    
    priv.calc_actual = function() {
        priv.gallery_li = jQuery("#gallery ul li");
        priv.win.width = jQuery(window).width();
        priv.win.width2 = Math.ceil(priv.win.width/2);
        priv.actual.obj = jQuery("#gallery ul .actual");
        priv.actual.pos = jQuery(priv.actual.obj).position();
        priv.actual.width = jQuery(priv.actual.obj).width();
        priv.actual.width2 = Math.ceil(priv.actual.width/2);
        priv.actual.height = jQuery(priv.actual.obj).height();
        dlam.data.left_width = - priv.actual.pos.left - priv.actual.width2 + priv.win.width2;
    };   
    
    this.on_resize = function(cover) {
        if (!is_defined(cover)) {
            cover = true;
        }
        priv.calc_actual();
        jQuery(priv.gallery_ul).css("left", dlam.data.left_width);
        jQuery("#gallery-nav-container").css("left", dlam.data.left_width);
        jQuery(priv.gallery_nav).width(priv.actual.width).css("left", priv.actual.pos.left);
        priv.show_info(false);
        if (cover) {
            this.cover.on_resize({middle: priv.actual.pos.left + priv.actual.width/2});
        }
        var width = priv.actual.width + 2*that.cover.get_left2_width();
        jQuery("#gallery-image-zoom").width(width);
        jQuery("#gallery-image-zoom").css("left", priv.win.width2 - Math.ceil(width/2));
    };
    
    this.init_events = function(with_parent) {
        console.log("init events");
        if (with_parent !== false) {
            that.parent.init_events.call(this);
        }
        jQuery("#gallery-prev").click(function() {
            priv.on_prev();
        });
        jQuery("#gallery-next").click(function() {
            console.log("next");
            priv.on_next();
        });
        jQuery("#gallery-zoom").click(function() {
            priv.on_zoom();
        });
        jQuery("#gallery-info").click(function() {
            priv.on_info();
        });
        jQuery("#gallery-image-zoom-mask").mousemove(function() {
            priv.on_zoom_mousemove();
        });
        jQuery("#cover-canvas").mousemove(function(evt) {
            priv.on_zoom_mousemove(evt);
        });
    };
        
    this.init = function() {
        
        priv.calc_actual();
        this.on_resize(false);
        this.init_events();
        this.cover.init();
        jQuery("#gallery-info-field").width("100px")
            .height(priv.actual.height)
            .css("margin-bottom", -priv.actual.height);
        jQuery("#gallery-nav-container").css("left", dlam.data.left_width);
        jQuery(priv.gallery_nav).width(priv.actual.width).css("left", priv.actual.pos.left);
    };
    
    this.start = function(complete_fun) {
        this.on_resize(false);
        this.cover.animate({complete_fun: complete_fun});
        
    };
    
    priv.image_animation_complete = function() {};
    this.set_image_animation_complete = function(f) {
        priv.image_animation_complete = f;
    };
    priv.image_animation = false;
    priv.animate_image = function(side) {
        if (priv.image_animation) {
            return;
        }
        priv.image_animation = true;
        jQuery(priv.gallery_ul).stop(true, true);
        var anim = {"left": dlam.data.left_width},
            easing = dlam.lookbook.easing;
        easing.complete = function(){
            //priv.swap_image(side);
            priv.calc_actual();
            jQuery("#gallery ul").css("left", dlam.data.left_width);
            priv.image_animation = false;
            setTimeout(priv.image_animation_complete, 50);
        };
        jQuery(priv.gallery_ul).animate(anim, easing); 
        that.cover.animate();
        
        if (priv.info) {
            on_lookbook_function("close");
            jQuery("#gallery-info-field .content").animate({opacity:0},dlam.lookbook.easing.duration/2, function() {
                jQuery("#gallery-info-field .content").html(priv.get_item_info())
                .animate({opacity:1}, dlam.lookbook.easing.duration/2);
            });
        }
    };
    
    priv.swap_image = function(side, new_actual) {
        var item = (side === "move_first_to_last") ? jQuery(priv.gallery_li).first() : jQuery(priv.gallery_li).last(),
            nr = jQuery(item).attr("item-nr"),
            width = jQuery(item).css("width"),
            width2 = Math.ceil(width/2),
            height = jQuery(item).css("height"),
            html = jQuery(item).html(), html_new = "";
        jQuery(item).hide();
        html_new = "<li item-nr='" + nr + "' id='item-container" + nr + "' "; 
        html_new += "style='width:"+width+ ";height:" + height + ";'>" + html + "</li>";
        jQuery(item).remove();
            
        if (side === "move_first_to_last") {
            priv.calc_actual();
            jQuery("#gallery ul").css("left", dlam.data.left_width);
            jQuery(html_new).insertAfter(jQuery(priv.gallery_li).last());           
        } else {
            jQuery(html_new).insertBefore(jQuery("#gallery ul li").first());
            priv.calc_actual();
            jQuery("#gallery ul").css("left", dlam.data.left_width); 
            
        }
        jQuery(priv.actual.obj).removeClass("actual");
        jQuery(new_actual).addClass("actual"); 
        priv.calc_actual();
        if (priv.zoom) {
            priv.on_zoom(true);
        }
        priv.animate_image(side);
    };
    
    priv.on_next = function() {
        if (!priv.image_animation) {
            var next_id = "#" + jQuery(priv.actual.obj).attr("next");
            priv.swap_image("move_first_to_last", next_id);
        }
    };
    
    priv.on_prev = function() {
        if (!priv.image_animation) {
            var prev_id = "#" + jQuery(priv.actual.obj).attr("prev");
            priv.swap_image("move_last_to_first", prev_id);
        }
    };
    
    priv.on_zoom_mousemove = function(evt) {
        if (!priv.zoom) {
            return;
        }
        var x1,x0,y0, pos = jQuery("#cover-canvas").offset(),
            x = utils.mouse.x - pos.left, y = utils.mouse.y - pos.top,
            w = that.cover.get_zoom_mask_width(), h = jQuery("#gallery ul").height(),
            nr = jQuery(".actual").parent().attr("item-nr"),
            src = jQuery(".actual").parent().find(".item-zoom").html(),
            hi = dlam.data.items[nr].image_zoom.height, wi = dlam.data.items[nr].image_zoom.width;
        //console.log("In?" + that.cover.is_on_image(x,y));
        //console.log(x + " " + y + ", " + w + " " + h);
        if (that.cover.is_on_image(x,y)) {
            // Abbilden auf grosses Bild
            x1 = x - that.cover.get_zoom_mask_left();
            x0 = (wi - w) * x1 / w;
            y0 = (hi - h) * y / h;
            if (x0 > (wi-w)) { x0 = wi-w;}
            if (y0 > (hi-h)) { y0 = hi-h;}
            //console.log(x0 + " " + y0 + ", " + wi + " " + hi);
            that.cover.draw_image(src, x0, y0, true);
        }
    };
    
    priv.on_zoom = function(close) {
        var x,y, img = "", nr, src, h, w, height,         mask_width;
       if (is_defined(close) && close === true) {
           priv.zoom = true;
       }
       
       nr = jQuery(".actual").parent().attr("item-nr");
       //img = jQuery(".actual").parent().find(".item-zoom").html();
       src = jQuery(".actual").parent().find(".item-zoom").html();
       h = dlam.data.items[nr].image_zoom.height;
       w = dlam.data.items[nr].image_zoom.width;
       mask_width = that.cover.get_zoom_mask_width();
       
       img = new Image();
       img.src = src;
       height = jQuery("#gallery ul").height();
       x = - mask_width/2 + w/2;
       y = - height/2  + h/2;
       //console.log (x + " " + y);
       
       if (priv.zoom) {
           jQuery("#gallery-image-zoom").fadeOut();
           jQuery("#gallery-image-zoom-mask").fadeOut();
           priv.zoom = false;
           that.cover.draw_image(src, x, y, false);
           return;
       }
       img.onload = function() {
            that.cover.draw_image(src, x, y, true);
            priv.zoom = true;
            priv.on_info(true);    
       };
    };
    
    priv.get_function = function() {
        
        var text = "", i=0, count = 0, title = "", name = "";
        text += "<div style='width: " + priv.actual.width + "px; position:absolute; bottom:0;vertical-align: bottom; padding:10px'>";
        if (dlam.data.functions == undefined) {
            return text + "</div>";
        }
        
        for (i = 0, count = dlam.data.functions.length; i < count; i++) {
            name = dlam.data.functions[i]["function"];
            title = dlam.data.functions[i]["label"];
            text += "<br><a class='lookbook-function' onclick='on_lookbook_function(\""+ name +"\");'>" + title + "</a>";
        }
        text += "</div>";
        return text;
    };
    
    priv.get_item_info = function() {
      var   text = "", style = "",
            nr = jQuery(".actual").parent().attr("item-nr"),
            title = jQuery("#item-title" + nr).html(),
            info = jQuery("#item-text" + nr).html();
      text = "<div class='content'>"; 
      style = "padding:10px;position: absolute; z-index: 50000; width: "+ priv.actual.width + "px";
      text += "<div style='" + style +"'>" + title + "<br><br>" + info + "</div>";
      text += priv.get_function();
      text += "</div>";
                
      return text;
    };
    
    priv.show_info = function(no_fadein) {
        jQuery("#gallery-info-field").css("left", priv.win.width2 + priv.actual.width2);
        jQuery("#gallery-info-field .content").html(priv.get_item_info());

        jQuery("#gallery-info-field .background").width(2 * priv.actual.width);
        jQuery("#gallery-info-field .background div").width(priv.actual.width)
            .height(priv.actual.height);
        jQuery("#gallery-info-field .background img").height(priv.actual.height);

        if (no_fadein !== false) {
            jQuery("#gallery-info-field").fadeIn(dlam.lookbook.easing.duration);
        }
    };
    
    priv.on_info = function(close) {
        if (is_defined(close) && close === true) {
            priv.info = true;
        }
        if (priv.info) {
            priv.info = false;
            jQuery("#gallery-info-field").fadeOut(dlam.lookbook.easing.duration);
            on_lookbook_function("close");
        } else {
            priv.show_info();
            priv.info = true;
            if (priv.zoom) {
                priv.on_zoom(true);
            }
        }
    };
    
    priv.on_popup = function() {
        if (priv.popup) {
            jQuery(".gallery-popup").fadeOut();
            priv.popup = false;
        } else {
            var text = "popup";
            jQuery(".gallery-popup").html(text).fadeIn();
            priv.popup = true;
        }
        
    };
    
}//constructor




function JSProfile() {
    var that = this;
    this.parent =extend(this, new JSMediaObject());
    this.name = "JSProfile";
    this.objecttype = "profile";
    
    this.text_div_id = "#gallery-profile-text-content";
    this.cover = new JSCover({speed: dlam.profile.cover_animation_speed, adjust_to: "#gallery-profile-image img", animate:{}});
    
    this.init = function() {
      that.parent.init.call(this);  
      this.on_resize(false);
      //this.cover.init();
      jQuery("#gallery-profile-text-content p").css("overflow", "hidden");
      this.init_events();
    };
        
//    this.init_events = function () {
//        that.parent.init_events.call(this);
//    };
        
    this.on_resize = function(cover) {
        var img_height = jQuery("#gallery-profile-image img").height(),
            padding = parseInt(jQuery(this.text_div_id).css("padding-top"), 10),
            margin = parseInt(jQuery(this.text_div_id).css("margin-top"), 10);
        padding += parseInt(jQuery(this.text_div_id).css("padding-bottom"), 10);
        margin += parseInt(jQuery(this.text_div_id).css("margin-bottom"), 10);
        if (!is_defined(cover)) {
            cover = true;
        }
        jQuery(this.text_div_id).height(img_height - padding -margin );
        jQuery("#gallery-profile-text-scrolling").height(img_height);
        jQuery("#gallery-profile-text-space").height(img_height - padding - margin - 2* 36-30);
        jQuery("#gallery-profile").height(img_height);
        jQuery("#gallery-profile-text").height(img_height);
        if (cover === true) {
            //this.cover.on_resize();
        }
    };
    
      
    this.start = function () {
       // this.cover.animate();
    };
 
    this.on_scroll_up = function () {
        var top = parseInt(jQuery("#gallery-profile-text-content p").css("top"), 10);
        top += dlam.profile.scrolling_pixel;
        if (top <= 0) {
            jQuery("#gallery-profile-text-content p").stop(true,true)
                .animate({"top": top}, dlam.profile.scrolling_speed);
        }
    };
 
    this.on_scroll_down = function () {
        var top = parseInt(jQuery("#gallery-profile-text-content p").css("top"), 10),
            height = jQuery("#gallery-profile-text-content p").height(),
            h2 = jQuery("#gallery-profile-text-content").height();
        top -= dlam.profile.scrolling_pixel;
        if (h2 - height <= top + dlam.profile.scrolling_pixel ) {
            jQuery("#gallery-profile-text-content p").stop(true,true)
                .animate({"top": top}, dlam.profile.scrolling_speed);
        }
    };
    
   
    jQuery("#scroll-up").bind("click", function() {
           that.on_scroll_up(); 
    });
    jQuery("#scroll-down").bind("click", function() {
       that.on_scroll_down(); 
    });
}//constructor


