/*global _l, FlashLookbook, FlashStore, FlashProfile, FlashVideo, MobileLookbook, MobileStore, MobileProfile, 
    MobileVideo, JSLookbook, JSStore, JSProfile, JSVideo */

/* 
 * Es gilt folgende Fälle zu unterschieden:
 * 1. Flash + JS vorhanden  <= FlashMediaObject
 * 2. Flash ohne JS <= Ueber PHP/HTML
 * 3. Kein Flash, aber JS <= JSMediaObject
 * 4. Mobile Version <= MObileXY
 * 
 * 5. Kein Flash, kein JS   <= Wird über PHP/HTML/noscript gehandlet
 * 
 */



var 
    accordionListIds = accordionListIds || [],
    dg = { /* DEBUGGING */
      f: 0.5  // um die timeouts auszuschalten/zu verschnellern
    },
    dlam = {
        page_type: "",
        page_types: [],
        is_mobile: false,
        preload: {
            first: [],
            content: []
        },
        start: {
            fading_timeout: {
                logo: 400,
                menu: 100,
                content: 200,
                animation: 500
            },
            fading_speed: {
                logo:       300,
                menu:       300,
                content:    400
            },
            preloader_fadeout_speed: 500
        },
        cover: {
            timeout: 500
        },
        media: null,
        data: {
        },
        flash: {
            min_version: "10.1.0",
            path: "/dl-media/flash/"
        },
        profile: {
            cover_animation_speed: 900,
            scrolling_pixel: 100,
            scrolling_speed: 100
        },
        lookbook: {
          easing: {queue:false, duration:700, easing: "jswing"},
          cover_animation_speed: 400,
          functions: {
              is_visible: ""
          }
        },
        video: {
            cover_animation_speed: 2900
        }     
};


/**
 * Checks via swfobject if flash is present
 * 
 * @returns boolean
 */
 function flash_exists() {
    return swfobject && swfobject.hasFlashPlayerVersion(dlam.flash.min_version);
    
}//fun

/**
 *
 */
function MediaObject() {
    this.name = "MediaObject";
    this.mediatype = "";
    this.objecttype = "object";
    this.ismobile = false;
    this.init = function() {};
    this.on_resize = function() {};
    this.preloader_get_height = function() {
        return 220; //top= dl.data.gallery.height - 1;
    };
    this.preloader = function(img, loaded, total) {
        var p = Math.ceil(100 * loaded / total),
            line_width = Math.ceil(p * jQuery(window).width() / 100),
            html = "LOADING " + p + "% ...";
        jQuery("#preloader-message").html(html);
        jQuery("#preloader-line").width(line_width);
        jQuery("#preloader-line").css("margin-top", this.preloader_get_height());
    };
    this.preloader_fadeout = function(start) {
        jQuery("#preloader-message").fadeOut(start.preloader_fadeout_speed);
        jQuery("#preloader-line").fadeOut(start.preloader_fadeout_speed);
    };
    this.content_fadein_step = function(now) {};
    this.start = function() {}; //function() {alert("start");};   
}//Constructor

/**
 * 
 * @returns {MediaImage}
 */
function MediaImage() {
    var that = this;
    this.parent = extend(this, new MediaObject());
    this.name = "MediaImage";
    this.objecttype = "image";
    this.preloader = function(img, loaded, total) {};
    this.on_resize = function() { };
    this.init_events = function(with_parent) {
        if (with_parent !== false) {
            that.parent.init_events.call(this);
        }
    };
}//constructor

function MediaHtml() {
    this.parent = extend(this, new MediaObject());
    this.name = "MediaHtml";
    this.objecttype = "html";
    this.preloader = function(img, loaded, total) {};
}//constructor

/* ------------------------------------------------------------------------ */

function createMediaObject(is_mobile, page_type) {
    var media = new MediaObject(),
        media_type = is_mobile? "mobile":(flash_exists()? "flash":"javascript");
    
    _l("CreateMediaObject: " + media_type + " " + page_type);
    //alert("CreateMediaObject: " + media_type + " " + page_type);
    if (page_type === "image") {
        return new MediaImage();
    }
    if (page_type === "html") {
        return new MediaHtml();
    }
    switch (media_type) {
        case "flash":
            switch (page_type) {
                case "lookbook":media = new FlashLookbook();break;
                case "store":media = new FlashStore();break;
                case "profile":media = new FlashProfile();break;
                case "video":media = new FlashVideo();break;
            }
            break;
        case "mobile":
            switch (page_type) {
                case "lookbook":media = new MobileLookbook();break;
                case "store":media = new MobileStore();break;
                case "profile":
                    if (dlam.data.images !== undefined && dlam.data.images.length>0) {
                        media = new MobileInspiration();
                    } else {
                        media = new MobileProfile();
                    }
                    
                    break;
                case "video":media = new MobileVideo();break;
            }
            break;
       default: //Javascript
            jQuery("#gallery").html(dlam.data.gallery_html);
            switch (page_type) {
                case "lookbook":media = new JSLookbook();break;
                case "store":media = new JSStore();break;
                case "profile":
                    if (dlam.data.images !== undefined && dlam.data.images.length>0) {
                        media = new JSInspiration();
                    } else {
                        media = new JSProfile();
                    }
                    
                    
                    break;
                case "video":media = new JSVideo();break;
            }
            break;
    }
    return media;
}//fun

/* ----------------------------------------------------------------------- */
/**
 *
 */
dlam.content_faded_in = false;
function fadein_content(start, media) {
    //_l("fadein_content");
    media.preloader_fadeout(start);
    jQuery("#content").animate({opacity: 1},{
        duration: start.fading_speed.content * dg.f,
        step: function( now, fx ){
            media.content_fadein_step(now);
            if (now === 1  && dlam.content_faded_in===false) {
                var fun_anim = function() {media.start();};
                dlam.content_faded_in = true;
                setTimeout(fun_anim, start.fading_timeout.animation );     
            }
        }
    });
}//fun

/**
 *
 */
function fadein_preloader(start, media) {
   _l("fadein_preloader");
   if (dlam.preload.content.length === 0 || media.mediatype === "flash") {
        fadein_content(start, media);
        return;
    }
    jQuery.preload(dlam.preload.content, {
        init: function(loaded, total) {},
        loaded: function(img, loaded, total) {
            //_l("fadein_preloader:" + loaded + "/" +total + " loaded: " + img);
            media.preloader(img, loaded, total);    
        },
        loaded_all: function(loaded, total) {
            var fun_fadein_content = function() {
                fadein_content(start, media);
            };
            setTimeout(fun_fadein_content, start.fading_timeout.content);            
        }
    });
}//fun

/**
 *
 */
function fadein_navigation(start, media) {
    //_l("fadein_navigation");
    jQuery.preload([], {
        init: function(loaded, total) {
            if (media.mediatype !== 'flash') {
                jQuery("#preloader-message").html("LOADING ...");
            }else {
                jQuery("#preloader-message").html("");
                jQuery("#content").css("opacity",1).show();
            }
        },
        loaded_all: function(loaded, total) {
            var fun_fadein_logo = function() {
                jQuery("#branding").fadeIn(start.fading_speed.logo  * dg.f, function() {
                    var fun_fadein_nav = function() {
                        jQuery("#access").fadeIn(start.fading_speed.menu  * dg.f, function() {
                            fadein_preloader(start, media);
                            //Magento  Right Menü
                            jQuery("#dl-top-right-nav").fadeIn(start.fading_speed.menu  * dg.f, function() {
                                
                            });
                        });
                    };
                    setTimeout(fun_fadein_nav, start.fading_timeout.menu * dg.f);
                });
            };
            setTimeout(fun_fadein_logo, start.fading_timeout.logo * dg.f);
        }
    });
}//fun

/**
 *
 */
function init_accordion_menu() {
    var i;
    for (i in accordionListIds) {
        if (accordionListIds.hasOwnProperty(i)) {
            jQuery('#' + accordionListIds[i]).accordion({
                    active: "current_page_item",
                    header: '.acc-nav-header',
                    collapsible: true,
                    navigation: true,
                    event: 'mouseover',
                    fillSpace: false,
                    animated: 'easeslide',
                    autoheight: false
            });
        }
    }
}//fun
/* ------------------------------------------------------------------------ */
jQuery(document).ready(function(){
    _l("Begin dl.js");
    //_l(JSON.stringify(dlam.preload.first));
    jQuery.preload(dlam.preload.first, {
        init: function(loaded, total) {
           _l("init");
            dlam = is_defined(dlam)? dlam : {};
            dlam.data = is_defined(dlam.data)? dlam.data : {};
        },
        loaded: function(img, loaded, total) {
            //_l("preload:" + img);
        },
        loaded_all: function(loaded, total) {
           //alert(dlam.is_mobile + " " + dlam.page_type);
            dlam.media = createMediaObject(dlam.is_mobile, dlam.page_type);
            dlam.media.init();
            
            fadein_navigation(dlam.start, dlam.media);
        }});
    //_l("init accordion");
    init_accordion_menu();
//    alert("Win:"+jQuery(window).width()+ ", Doc:" + jQuery(document).width());
});
