/**
 * author.name => "Vladimir Tkach"
 */
$.fn.opacity = function(amount) {
    if (amount > 1) amount = 1;
    if (amount < 0) amount = 0;
    if ($.browser.msie) {
        amount = (parseFloat(amount) * 100);
        this.css('filter', 'alpha(opacity='+amount+')');
    } else {
        this.css('opacity', amount);
        this.css('-moz-opacity', amount);
    }
    return this;
}
//------------------------------------------------------------------------------
    String.prototype.htmlEntities=function(){
        return this.replace(/&/g,'&amp;').
                    replace(/</g,'&lt;').
                    replace(/>/g,'&gt;').
                    replace(/'/g,'&#039;').
                    replace(/"/g,'&quot;')};
//------------------------------------------------------------------------------
    String.prototype.htmlDecode=function(){
        return this.replace(/\\&#039;/g, "'").
                    replace(/\\&quot;/g, '"').
                    replace(/\\\\/g, "\\")};
//------------------------------------------------------------------------------
    String.prototype.titleize=function(){
        var w=this.split(' ');
        for(var i=0;i<w.length;i++){w[i]=w[i].charAt(0).toUpperCase()+
                                              w[i].substring(1).toLowerCase()}
        return w.join(" ").replace("&amp;","&").
                           replace("&Amp;","&").
                           replace("&Reg;","&reg;").
                           replace("&Copy;","&copy;")}
//------------------------------------------------------------------------------
var base = {};
    base.form = {};
    base.html = {};
//------------------------------------------------------------------------------
base.form.add = function(id) {
    $("input[rel=add_product_button]").attr("disabled",true);
    $("#"+id).submit();
    return false;
}            
//------------------------------------------------------------------------------
base.ajax = function(opts){

    var _json;

    $.ajax({
        type: "POST",
        url: "ajax_controller.php",
        data: opts,
        dataType : "json",
        cache: false,
        timeout: 15000,
        error: function(e){},
        beforeSend: function(){},
        success: function(){},
        complete: function(ajaxData){
            _json = eval('('+$.trim(ajaxData.responseText)+')');
            switch(opts.action){
                case "render_gallery":
                    base.html.gallery.renderGallery(_json);
                    break;
                default: break;                    
            }
        }
    })
};
//------------------------------------------------------------------------------
base.html = {
//------------------------------------------------------------------------------
    moveTop : function(s){$("html,body").scrollTo($(".wrapper"),s)},
//------------------------------------------------------------------------------
    scrolls : function(c){
        var hide=c?"auto":"hidden";
        $('html').css({overflow:hide});
        $('body').css({overflow:hide});
    },
//------------------------------------------------------------------------------
    ie6selects : function(c){
        var hide=c?"visible":"hidden";
        $.each($("select"),function(){
            if ($.browser.msie &&
                $.browser.version == "6.0") { $(this).css("visibility",hide) } });
    },
//------------------------------------------------------------------------------
    setHTMLOpacity : function(bgColor) {
        var winWidth  = this.gallery.screenWidth,
            winHeight = this.gallery.screenHeight,
            shadow    = $('<div id="'+this.gallery.shadowID+'"></div>');

        this.scrolls(false);

        if (shadow.length>0) shadow.remove();

        shadow.css({zIndex         :1000,
                    backgroundColor:bgColor,
                    position       :"absolute",
                    left           :0,
                    top            :0,
                    width          :winWidth*2,
                    height         :winHeight*20}).opacity(this.gallery.transparency);

        shadow.appendTo('body');
    }
};
//------------------------------------------------------------------------------
base.html.gallery = {
    shadowID    : 'shadow_container',
    screenHeight: screen.height,
    screenWidth : screen.width,
    windowHeight: $(window).height(),
    windowWidth : $(window).width(),
    speed       : 500,
    margin      : 50,
    transparency: .7,
    resetOpacity: 1,
    params      : {},
//------------------------------------------------------------------------------
    setGalleryPosition : function(p){
        this.params.position=p;
        base.ajax(this.params);
        this.params.position=0;
    },
//------------------------------------------------------------------------------
    renderFlash : function(opts){
        var flashvars = {product_id: opts.product_id},
            params = {
                allowFullScreen:"false",
                scale:"noscale",
                menu:"false",
                bgcolor:opts.bgcolor,
                wmode:"transparent"
            },
            attributes = {
                id: "sotester",
                name: "sotester"
            };

        swfobject.embedSWF("/images/template_"+opts.template+"/preview.swf",
                           "flashcontent",
                           "725",
                           "544",
                           "9.0.0",
                           "expressInstall.swf",
                           flashvars,
                           params,
                           attributes);
    },
//------------------------------------------------------------------------------
    renderGallery : function(opts){

        if(opts.allowed==0) return;

        var gc        = $("#"+opts.container_id),
            margin    = this.margin,
            winHeight = this.screenHeight,
            html      = base.html;

        html.moveTop(300);

        if (gc.length>0) gc.remove();

        html.ie6selects(false);
        html.setHTMLOpacity(opts.bgcolor);

        $('body').append(opts.html);

        var obj          = $("#"+opts.container_id),
            objWidth     = obj.width(),
            objHeight    = obj.height();

        var offsetWidth  = this.windowWidth/2 - objWidth/2;
            offsetWidth  = offsetWidth <= margin ? margin : offsetWidth;
        var offsetHeight = winHeight/2 - objHeight/2 - margin;
            offsetHeight = offsetHeight <= margin ? margin : offsetHeight;

        obj.css({top     :offsetHeight-margin/2,
                 left    :offsetWidth,
                 position:'absolute',
                 zIndex  :parseInt($("#"+this.shadowID).css("zIndex"),10)+1}).opacity(this.resetOpacity);

        this.renderFlash(opts);
        $("#"+opts.container_id).fadeIn(this.speed);
    },
//------------------------------------------------------------------------------
    closeGallery : function(e){
        var html=base.html;
        html.ie6selects(true);
        html.scrolls(true);
        $("#"+this.shadowID).remove();
        $(e).parent().remove();
    }
//------------------------------------------------------------------------------
};
base.html.tip = {
//------------------------------------------------------------------------------
    init : function(e){
        $(e).bind("mouseenter", this.createTip);
	$(e).mouseleave(function(){$("#tip").remove()});
	$(e).click(function(){$("#tip").remove()});
        $(e).mousemove(function(e){$("#tip").css({left:e.pageX+30,
                                                  top:e.pageY-16})});
    },
//------------------------------------------------------------------------------
    createTip : function(e) {
        var obj=$(e.currentTarget),
            title=$.trim(obj.attr("helper"));
        if(title.length>0){
            return $("#tip").length === 0 ? $("<div>").html("<span>please enter<br />"+$.trim(obj.attr("helper"))+
                                                            "</span><span class='arrow'></span>").
                                                       attr("id","tip").
                                                       css({left:e.pageX+30,
                                                            top:e.pageY-16,
                                                            position:'absolute',
                                                            border:'1px solid #FFE222',
                                                            background:'#FFFBC2',
                                                            color:'#514721',
                                                            padding:'5px 10px',
                                                            textTransform:'lowercase',
                                                            fontVariant:'small-caps',
                                                            zIndex:9000}).
                                                       appendTo("body") : null;
        } else {return false}
    }
//------------------------------------------------------------------------------
};