﻿coverFlow = function() { };

coverFlow.prototype.container_id = null;
coverFlow.prototype.container_obj = null;
coverFlow.prototype.coverflow_src = null;
coverFlow.prototype.coverflow_path = null;
coverFlow.prototype.is_large = null;
coverFlow.prototype.details_timer = null;
coverFlow.prototype.exp_title = null;
coverFlow.prototype.exp_link = null;

coverFlow.prototype.init = function(islarge, initial_flash_source) {
    this.container_id = 'coverflow_inner';
    this.is_large = islarge;

    this.set_source(initial_flash_source);
    this.set_social();
};

coverFlow.prototype.set_source = function() {
    this.coverflow_src = arguments[0];
    if (this.is_large) {
        this.coverflow_path = $g.flash_root + 'coverflow966_v8.swf?src=';
        this.coverflow_path += this.coverflow_src;
        this.coverflow_path += '&f=cf.get_details';
    }
    else {
        this.coverflow_path = $g.flash_root + 'coverflow794_v8.swf?src=';
        this.coverflow_path += this.coverflow_src;
        this.coverflow_path += '&f=cf.get_details';
    }
};

coverFlow.prototype.set_social = function() {
    var s = this;
    $('.exp_social > a').each(function() {
        $(this).click(function() {
            var cn = $(this).attr('className');
            var url = '';
            switch (cn) {
                case 'facebook':
                    url = 'http://www.facebook.com/sharer.php?u=' + s.exp_link;
                    break;
                case 'twitter':
                    url = 'http://twitter.com/home?status=' + s.exp_link;
                    break;
                case 'myspace':
                    url = 'http://www.myspace.com/Modules/PostTo/Pages/?u=' + s.exp_link + '&t=' + s.exp_title + '&c=' + s.exp_title;
                    break;
                case 'bebo':
                    url = 'http://www.bebo.com/c/share?Url=' + s.exp_link + '&Title=' + s.exp_title;
                    break;
            }
            window.open(url, null, 'scrollbars=yes,directories=0,height=420,location=0,menubar=0,status=0,titlebar=0,toolbar=0,width=840', false);
        });
    });
};

coverFlow.prototype.render_coverflow = function() {
    if ($g.use_swfobject || true == true) {
        var cf_parameters = {
            flashvars: {},
            params: { "wmode": "transparent" },
            attributes: {}
        };

        swfobject.embedSWF(
            this.coverflow_path,
            this.container_id,
            (this.is_large ? '966' : '794'),
            '366',
            '9.0.0',
            false,
            cf_parameters.flashvars,
            cf_parameters.params,
            cf_parameters.attributes,
            null
        );
    }
    else {
    }
};

coverFlow.prototype.get_details = function() {
    var s = this, id = arguments[0], lk = arguments[3], categoryId = arguments[5];
    var cobj = eval("(" + window['coverflow_category_desc'] + ")");

    if (categoryId == undefined)
        categoryId = 0;

    this.exp_link = 'http://' + window.location.hostname + lk;
    this.exp_title = arguments[1];

    if (categoryId == 0) {
        if (this.details_timer != null) window.clearTimeout(this.details_timer);
        this.details_timer = window.setTimeout(function() {
            s.find_details(id, lk);
        }, 500);
    }
    else {
        $('.details_info_category').css({ display: 'block' });
    }
};

coverFlow.prototype.find_details = function() {
    var sb = new StringBuilder(); sb.clear();
    var c = 0, lk = arguments[1], hb = false, bd = null;

    $.getJSON('/ajax/cover_flow_detail.ashx?experienceId=' + arguments[0], function(d) {
    
        $.each(d.variants, function(i, item) {
            c++;

            if (item.title.length > 34) {
                item.title = item.title.substr(0, 34);
                item.title += '..';
            }

            sb.append('<div class="detail">');
            sb.append('<div class="title"><wbr>');
            sb.append(item.title);
            sb.append('</wbr> - ');
            sb.append(item.price);
            sb.append('</div>');
            sb.append('<div class="button_basket">');
            sb.append('<a href="');
            sb.append('javascript:cf.setbasketVariant(\'');
            sb.append(item.variantId);
            sb.append('\', \'basket\');">')
            sb.append('ADD TO BASKET</a>');
            sb.append('</div>');
            sb.append('<div class="clear"></div>');
            sb.append('</div>');

            if (item.blogs) {
                hb = true;
                bd = item.blogs;
            }
        });

        $('#cf_details_info').html(sb.toString());

        $('#details_view_a').removeClass();
        $('#details_view_a').addClass('rows' + c);
        $('#details_view_a').attr('href', lk);
        $('.details_view').css('visibility', 'visible');

        sb.clear();

        if (hb) {
            $.each(bd, function(i, item) {
                if (i < 3) {
                    sb.append('<div class="entry">');
                    sb.append('<h2>');
                    sb.append(item.title);
                    sb.append('</h2>');
                    sb.append('<h3>');
                    sb.append(item.author);
                    sb.append('</h3>');
                    sb.append('</div>');
                }
            });
        }
        else {
            sb.append('');
        }

        $('#exp_blogs_inner').html(sb.toString());
    });
};

coverFlow.prototype.setbasketVariant = function() {
    master.setbasketVariant(arguments[0]);
    if (arguments[1] == 'basket') master.addtoBasket();
    else master.addtoDreamlist(arguments[0]);
};
