/* 
 * @package WordPress
 * @subpackage Dereklam
 * @author: A. Kuzmanovski
 */
/*global dlam, MediaObject,  */


/**
 *
 */
function embed_flash(swffile, data, id) {
    var div_id = "flashContent",
        style = 'min-width:960px; height: ' + data.height + '; width:100%; margin: 0 auto;z-index: 1000;position:relative;',
        html = "<div id='resizableContainer' style='" + style + "'><div id='" + div_id + "'></div></div>",
        swfVersionStr = dlam.flash.min_version,
        xiSwfUrlStr = dlam.flash.path + "playerProductInstall.swf",
        params = {}, attributes = {}, flashvars = {};
        
    //_l("embed_flash: " + id);
    jQuery("#gallery").html(html);
    params.quality = "high";
    params.bgcolor = "#ffffff";
    params.allowscriptaccess = "sameDomain";
    params.allowfullscreen = "true";
    params.wmode = "transparent";
    attributes.id = id;
    attributes.name = attributes.id;
    attributes.align = "left";
    
    flashvars.autostart = 'true';
    flashvars.hold = 'true';
    //data.image.url = "/" + data.image.url;
    flashvars.json = (encodeURIComponent(JSON.stringify(data)));
    swfobject.createCSS("#" + div_id, "display:block;text-align:left;");
    swfobject.embedSWF( swffile, div_id, "100%", "100%", swfVersionStr, xiSwfUrlStr, flashvars, params, attributes);
    //_l("embed_flash: " + id + ", ready");
}//func


var 
    init_media = null,
    count_checks = 0, 
    max_check_trials = 50;
/**
 * 
 */
function check_for_init() {
    var that = init_media, swf = document[that.id];//this.init_swf(this.id);
    count_checks += 1;
    //_l("check for init flash object");
    if (swf) {
      if(typeof swf.init === 'function') {
        //_l("flash init, id: ", that.id);
        swf.init();
        jQuery(window).trigger("resize");
        return;
      } else {
          _l("no flash init function found (trial:" + count_checks + ")");
      }
    }
    if (count_checks === max_check_trials) {
      _l("no flash object!!!!");
    } else {
      setTimeout(check_for_init, 700);
    }     
}//fun

/**
 * 
 */
function FlashMediaObject() {
    var that = this;
    this.parent = extend(this, new MediaObject());
    this.mediatype = "flash";
    this.objecttype = "object"; 
    this.preloader = function(img, loaded, total) {};
    this.data = dlam.data;
    this.on_resize = function() {
        var width = jQuery(window).width(),
            height = width*9/20;
        if (height > 508) {height = 508;}
        if (height < 408) {height = 408;}
        //console.log("flash resize (FlashMediaObject)");
        jQuery("#gallery div").height(height);
        
    };
    this.init = function() {
        
        that.parent.init.call(this); 
        
        this.init_events();
        this.data.option = {};
        this.data.option.zoom = false;
        this.data.option.arrows = false;
        delete this.data.left_width;
    };
    
    this.init_events = function() {
        jQuery(window).resize(function() {
            _l("window resize");
            that.on_resize();
        });
        
    };
        
    this.start = function() {
        init_media = this;
        check_for_init();
    };
//    
//    this.init_swf = function(movieName) {
//        if (navigator.appName.indexOf("Microsoft") !== -1) {
//                return window[movieName];
//        } else {
//                return document[movieName];
//	}
//    };
}//constructor


function FlashVideo() {
    var that = this;
    this.parent = extend(this, new FlashMediaObject());
    this.objecttype = "video";
    this.id = "FlashVideo";
    this.init = function() {
        that.parent.init.call(this); 
        
        embed_flash(dlam.flash.path + "video.swf", this.data, this.id);
    };
}//constructor

function FlashLookbook() {
    var that = this;
    this.parent = extend(this, new FlashMediaObject());
    this.objecttype = "lookbook";
    this.id = "FlashLookbook";
    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() {
        that.parent.init.call(this); 
        this.data.option.zoom = true;
        this.data.option.arrows = true;
        this.data.height = this.data.items[0].image.height;
        dlam.data.index = that.parse_model_nr(window.location);
        embed_flash(dlam.flash.path + "lookbook.swf", this.data, this.id);
        that.on_resize();
    };
}//constructor

function FlashStore() {
    var that = this;
    this.parent = extend(this, new FlashLookbook());
    this.objecttype = "store";
    this.id = "FlashStore";
    this.init = function() {
        that.parent.init.call(this); 
        this.data.option.zoom = false;
        this.data.option.arrows = true;
       // console.dir(this.data);
        embed_flash(dlam.flash.path + "lookbook.swf", this.data, this.id);
        that.on_resize();
    };
}//constructor

function FlashProfile() {
    var that = this;
    extend(this, new FlashMediaObject());
    //console.dir(this.data);
    
    this.objecttype = "profile";
    this.id = "FlashProfile";
    this.init = function() {
        that.parent.init.call(this);
        if (this.data.image === undefined) {
            this.data.height = this.data.images[0].height;
        } else {
            this.data.height = this.data.image.height;
        }
        embed_flash(dlam.flash.path + "profile.swf", this.data, this.id);
        that.on_resize();
        that.init_events();
    };
}//constructor


jQuery(document).ready(function(){ 
   //on_lookbook_function("Share This"); 
});
