window.i3 = {};

window.i3.std = {};
window.i3.plugins = {};

window.i3.std.def = function()
{
    for (var i=0; i<arguments.length; i++)
        if (arguments[i]!=null && String(arguments[i])!='undefined' && String(arguments[i]).length!=0)
            return arguments[i];
    return null;
};

window.i3.std.find = function(object, selector)
{
    return $.extend($(object).is(selector) ? $(object) : {}, $(object).find(selector));
}

// DEPRECATED - LEAVING FOR COMPATIBILITY PURPOSES
// PLASE USE $.extend
window.i3.std.copy = function(object1, object2)
{
    if (object1 && object2)
    for (var index in object2)
        object1[index] = object2[index];

    return object1;
}

window.i3.std.nodespace = function(type)
{
    var object = null;
    if (type)
    {
        if (type.prototype)
            switch(type.prototype.toString)
            {
                case 'String':
                    if ($(type).length > 0)
                        object = $(type);
                    else
                        object = $('#'+type);
                break;
                
                case 'Object':
                    object = $(type);
                break;
            }
        else
            if ($(type).length > 0)
                object = $(type);
            else
                object = $('#'+type);
    }
    else
        object = $(document.body);
        
    return object;
};

window.i3.std.globalInherit = function(type)
{
    var object = i3.std.nodespace(type);
        
    if (i3.popup)
    {
        if (i3.popup.assignPopup)
            i3.popup.assignPopup(i3.std.find(object, ".displayPopup"));
        
        if (i3.popup.confirmPopup)
        i3.std.find(object, 'a.confirm').each(function()
        {
            if (!($(this).data('url')))
            {
                $(this).click(function(e)
                {
                    e.preventDefault();
                    i3.popup.confirmPopup(e, this);
                });
                
                $(this).data('url', $(this).attr('href'));
                $(this).attr('href','#');
            }
        });
    }

    if (i3.cart)
    {
        i3.std.find(object, ".productDetails .addToCart").click(function(ev)
        {
            ev.preventDefault();
            i3.cart.addToCart(this);
        });

        i3.std.find(object, ".productDetails .optionSelect").change(function(ev)
        {
            ev.preventDefault();
            i3.cart.changeOption(this);
        });
        
        if (i3.std.find(object, ".productDetails .optionSelect").is("select"))
            i3.std.find(object, ".productDetails .optionSelect:first").change();

        i3.std.find(object, ".productDetails .itemsNo").change(function(ev)
        {
            ev.preventDefault();
            i3.cart.changeQuantity(this);
        });

        i3.cart.changeElement(i3.std.find(object, ".action"));
    }

    if (i3.autocomplete)
    {
        i3.autocomplete.apply(i3.std.find(object, ".autocomplete_counties"), '/pages_actions/std/system_counties.php');
        i3.autocomplete.apply(i3.std.find(object, ".autocomplete_countries"), '/pages_actions/std/system_countries.php');
    }
    
    i3.std.find(object, 'div.banner, div.advert').click(function(){
        $.post('/pages_actions/std/cms_banner_click.php',{id: $(this).attr('rel')});  
    });

    if ($.datepicker)
        i3.std.find(object, '.date-pick').datepicker({dateFormat:'dd/mm/yy',  yearRange: "-90:+15"});
        
    if ($.datepicker)
        i3.std.find(object, '.date-pick-dob').datepicker({dateFormat:'dd/mm/yy',  yearRange: "-90:+0", changeMonth: true, changeYear: true});

    if (window.ZeroClipboard)
    {
        i3.std.find(object, '.copyable').each(function()
        {
            var target = this;
            if($(this).attr('target'))
            {
                var nodes = $($(this).attr('target'));
                if (nodes.length > 0)
                    target = nodes[0];
            }
            
            var clip = new ZeroClipboard.Client(this);
            clip.addEventListener('mouseOver', function()
            {
                window.confirm(target);
                clip.setText( $(target).is('input') ? $(target).val() : $(target).text());
            });
        });
    }
    
    if (window.tinyMCE && i3.std.find(object, '.tinyEditor').length > 0)
    {
        window.setTimeout(function()
        {
            i3.std.find(object, '.tinyEditor').each(function(index)
            {            
                var timestamp = Math.round(new Date().getTime() / 1000);
                var id = i3.std.def($(this).attr('id'), "wysiwyg"+timestamp+index);

                if (!$(this).attr('id'))
                    $(this).attr('id', id);

                tinyMCE.init({
                    mode : "exact",
                    theme : "advanced",
                    theme_advanced_buttons1 : "formatselect,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull",
                    theme_advanced_blockformats : "p,div,h1,h2,h3,h4,h5,h6,blockquote,dt,dd,code,samp",
                    theme_advanced_buttons2 : ",bullist,numlist,|,undo,redo,|,link,unlink,|,outdent,indent,|,anchor,|,paste,|,code",
                    theme_advanced_buttons3 : "",
                    elements: id
                });
            });
        }, 500);
    }
    
    i3.std.find(object, "input:text, textarea, input:password").each(function(){
        if($(this).val() == '')
            $(this).val(this.title);
    });
    
    i3.std.find(object, "input:text, textarea, input:password").click(function(){
        if($(this).val() == this.title)
            $(this).val('');
    });
    
    i3.std.find(object, "input:text, textarea, input:password").blur(function(){
        if($(this).val() == '')
            $(this).val(this.title);
    });
    
    i3.std.find(object, "input:image, input:button, button:submit, input[type=submit]").click(function(){
        if (this.form)
        for (var i=0; i<this.form.elements.length; i++)
        {
            var element = this.form.elements[i];
            if(($(element).attr('type') =='text' || $(element).attr('type') =='textarea' || $(element).attr('type') =='password') && $(element).val() == $(element).attr('title') && $(element).attr('title') != '')
                $(element).val('');
        };
    });
    
    i3.std.find(object, '.simpleTabs').click(function(e)
    {
        e.preventDefault();
        $('.simpleTabs').each(function()
        {
            $($(this).attr('href')).hide();
        });
        
        $($(this).attr('href')).show();
    });

    i3.std.find(object, "form").submit(function()
    {
        for (var i=0; i<this.elements.length; i++)
        {
            var element = this.elements[i];
            if(($(element).attr('type') =='text' || $(element).attr('type') =='textarea' || $(element).attr('type') =='password') && $(element).val() == $(element).attr('title') && $(element).attr('title') != '')
                $(element).val('');
        };
    });
    
    i3.std.find(object, ".smarty_debugging.start").hide().each(function()
    {        
        var content = $(this).next(".smarty_debugging.content");        
        
        if ($(content).length > 0)
            $(this).css('top', $(content).position().top - 10);
    });
    
    i3.std.find(object, ".smarty_debugging.end").hide().each(function()
    {
        var content = $(this).prev(".smarty_debugging.content");
        
        if ($(content).length > 0)
            $(this).css('top', $(content).position().top + $(content).height());
    });
    
    i3.std.find(object, ".smarty_debugging.content").mouseover(function(ev)
    {
        ev.stopPropagation();
        
        $(this).prev('.smarty_debugging.start:first').show();
        $(this).next('.smarty_debugging.end:first').show();
    }).mouseout(function(ev)
    {
        $(this).prev('.smarty_debugging.start:first').hide();
        $(this).next('.smarty_debugging.end:first').hide();
    });
    
    if ($.fn.serialScroll)
        i3.std.find(object, '.i3-slider').each(function()
        {
            var params = {target: this};
            var options = i3.std.string.toObject($(this).attr('rel'));
            
            $.extend(params, options);
            
            $(this).serialScroll(params);
        })
        
    if ($.fn.draggable)
        i3.std.find(object, '.i3-draggable').each(function()
        {
            var self = this;
            var params = {};
            var options = i3.std.string.toObject($(this).attr('rel'));
            
            if (options.helper && !(options.helper == 'clone' || options.helper == 'original'))
            {
                var selector = options.helper;
                options.helper = function()
                {
                    var node = $(self).find(selector);
                    return $(node.length > 0 ? node : self).clone();
                }
            }
            
            
            $.extend(params, options);
            
            $(this).draggable(params);
        });
        
    if ($.fn.droppable)
        i3.std.find(object, '.i3-droppable').each(function()
        {
            var params = {};
            var options = i3.std.string.toObject($(this).attr('rel'));
            
            $.extend(params, options);
            
            if (params.drop && window[params.drop])
                params.drop = window[params.drop];
            
            $(this).droppable(params);
        })
    
    if (window.DD_belatedPNG)
        window.DD_belatedPNG.fix('*');

    if (window.Cufon)
        window.Cufon.replace('.typeface-js');

    if (window.i3.std.dist)
        i3.std.dist.globalInherit(type);
}
