/* 
 * @package WordPress
 * @subpackage Dereklam
 * @author: A. Kuzmanovski
 */
/*global dlam, JSVideo, JSLookbook, JSProfile, JSStore */
/* ------------------------------------------------------------------------ */

function isiPhone() {
    return navigator.userAgent.indexOf("iPhone") != -1;
}

/**
 *
 */
function isiPad() {
    return navigator.userAgent.indexOf("iPad") != -1;
}


var mobile = {};

    mobile.orientation = "portrait";
function updateOrientation() {
    
    if (dlam != undefined) {
        if (dlam.media != undefined) {
            dlam.media.on_resize(true);   
        }
            
  
    }
  
    if (window.orientation == 90
            || window.orientation == -90) {
            mobile.orientation = 'landscape';
        }
        else {
            mobile.orientation = 'portrait';
        }
  
//  alert("orientation " + mobile.orientation);
  
  switch (mobile.orientation) {
      case "portrait":
          if (isiPhone()) {
              //jQuery("").width("640px !important");
          }
          if (isiPad()) {
              //jQuery("body").width("6401px !important");
          }
          break;
      case "landscape":
          if (isiPhone()) {
              //jQuery(document).width("960px !important");
          }
          if (isiPad()) {
              //jQuery("body").width("6401px !important");
          }
          break;
  }//
  
  
}//

function MobileVideo() {
    var that = this;
    this.parent =extend(this, new JSVideo());
    this.ismobile = true;
    this.name = "MobileVideo";
    this.init = function() {
        //that.parent.init.call(this);
        var height = dlam.mobile.video.height;
        jQuery("#video-container object").css("opacity",0.0);
        jQuery("#video-cover").height(height);
        jQuery("#video-cover").css("top", -height +1).css("margin-bottom", -height + 1);
    };
    
    this.content_fadein_step = function(now) {
       jQuery("#video-container object").css("opacity", now);
        if (now === 1) {
           _l("init MobileVideo" + jQuery("#video-container object").height()+" "+jQuery("#video-container object").width());
        }
    };
}//constructor

function MobileLookbook() {
    var that = this, priv;
    this.parent =extend(this, new JSLookbook());
    priv = this.parent.get_priv();
    priv.canvas = jQuery("#cover-canvas");
    this.ismobile = true;
    this.name = "MobileLookbook";
    this.init_events = function() {
        that.parent.init_events.call(this, false);       
        jQuery("#cover-canvas").touchwipe({
             wipeLeft: function(dx, startX, startY) { that.touchwipe("left",dx,0, startX, startY); },
             wipeRight: function(dx, startX, startY) { that.touchwipe("right",dx,0, startX, startY); },
             wipeUp: function(dy, startX, startY) { that.touchwipe("up",0,dy, startX, startY); },
             wipeDown: function(dy, startX, startY) { that.touchwipe("down",0,dy, startX, startY); },
             min_move_x: 5, min_move_y: 5, preventDefaultEvents: true
        });
        document.getElementById("cover-canvas").ontouchmove = this.onTouchImage;
        document.getElementById("cover-canvas").ontouchstart = this.onTouchImage;
        jQuery("#cover-canvas").unbind("mousemove");
    };
    
    this.touchwipe = function(orient, dx ,dy, startX, startY) {
        var i = 0, max = 1, d = 0, f = null, temp,
            pos = jQuery("#cover-canvas").offset(),
            x = startX - pos.left, y = startY - pos.top;
        if (that.cover.is_on_image(x, y)) {
            return;
        }
        switch (orient) {
            case "left":
                d = dx;
                f = priv.on_next;
                break;
            case "right":
                d = dx;
                f = priv.on_prev;
                break;
        }
        
        if (f !== null) {
            max = Math.floor(Math.abs(d /10));
            i = 1;
            temp = dlam.lookbook.easing.duration;
            dlam.lookbook.easing.duration =  temp - 100 * (max);
            if (dlam.lookbook.easing.duration < 100) {
                dlam.lookbook.easing.duration = 100;
            }
            that.parent.set_image_animation_complete(function() {
                if (i < max) {
                    i++;
                    f();
                } else {
                    that.parent.set_image_animation_complete(function(){});
                    dlam.lookbook.easing.duration = temp;
                }
            });
            f();
        }
    };
    
    this.onTouchImage = function(e, x_, y_) {
        var pos, x, y, x0, y0, x1, src, nr, w, h, hi, wi;
        if (!that.parent.get_priv().zoom) {
            return;
        }
        if(e.touches.length == 1){
            pos = jQuery("#cover-canvas").offset();
            x = e.touches[0].pageX - pos.left; 
            y = e.touches[0].pageY - pos.top;
            if (that.cover.is_on_image(x, y)) {
                x1 = x - that.cover.get_zoom_mask_left();
                src = jQuery(".actual").parent().find(".item-zoom").html();
                nr = jQuery(".actual").parent().attr("item-nr");
                w = that.cover.get_zoom_mask_width(); 
                h = jQuery("#gallery ul").height();
                hi = dlam.data.items[nr].image_zoom.height; 
                wi = dlam.data.items[nr].image_zoom.width;

                x0 = (wi - w) * x1 / w;
                y0 = (hi - h) * y / h;
                if (x0 > (wi-w)) { x0 = wi-w;}
                if (y0 > (hi-h)) { y0 = hi-h;}
                that.cover.draw_image(src, x0, y0, true);
                //console.log("onTouchImage " + e.touches[0].pageX + "/" + e.touches[0].pageY);
            }
        }
    };
}//constructor

function MobileStore() {
    var that = this, priv;
    this.parent =extend(this, new JSStore());
    this.ismobile = true;
    this.name = "MobileStore";
    priv = this.parent.get_priv();
    this.init_events = function() {
        that.parent.init_events.call(this, false);
        
        jQuery("#cover-canvas").touchwipe({
             wipeLeft: function(dx, startX, startY) { that.touchwipe("left",dx,0, startX, startY); },
             wipeRight: function(dx, startX, startY) { that.touchwipe("right",dx,0, startX, startY); },
             wipeUp: function(dy, startX, startY) { that.touchwipe("up",0,dy, startX, startY); },
             wipeDown: function(dy, startX, startY) { that.touchwipe("down",0,dy, startX, startY); },
             min_move_x: 5,
             min_move_y: 5,
             preventDefaultEvents: true
        });
        
        
    };
    
    
    this.touchwipe = function(orient, dx ,dy, startX, startY) {
        var temp, i = 0, max = 1, d = 0, f = null,
            pos = jQuery("#cover-canvas").offset(),
            x = startX - pos.left, y = startY - pos.top;
        //console.log("POSITION:"+ x + " " + y);
        if (that.cover.is_on_image(x, y)) {
            return;
        }
        switch (orient) {
            case "left":
                d = dx;
                f = priv.on_next;
                break;
            case "right":
                d = dx;
                f = priv.on_prev;
                break;
        }
        
        if (f !== null) {
            max = Math.floor(Math.abs(d /10));
            //max = 1;
            //console.log(orient + " " + dx+ " "+dy + " " + max);
            i = 1;
            temp = dlam.lookbook.easing.duration;
//            dlam.lookbook.easing.duration =  temp - 50 * (max);
//            if (dlam.lookbook.easing.duration < 200) {
//                dlam.lookbook.easing.duration = 200;
//            }
            that.parent.set_image_animation_complete(function() {
                if (i < max) {
                    i++;
                    f();
                    //alert("sd");
                } else {
                    that.parent.set_image_animation_complete(function(){});
                    dlam.lookbook.easing.duration = temp;
                }
            });
            f();
        }
    };
    
}//constructor

function MobileProfile() {
    this.parent =extend(this, new JSProfile());
    this.name = "MobileProfile";
    this.ismobile = true;
    this.init_events = function() {
        // Keine Events von JSProfile
    };
}//constructor

function MobileInspiration() {
    this.parent =extend(this, new JSInspiration());
    this.name = "MobileInspiration";
    this.ismobile = true;    
}//constructor

jQuery(document).ready(function(){
    jQuery(document).onorientationchange = updateOrientation();
});

