﻿(function($){
    $.createHelpLink = function(options, elemBefore) 
    { 
        if (!elemBefore)
            elemBefore = 'table[class=sheet]';
        var ex = $.extend({
                caption_on: 'Посмотреть образец заполнения',
                caption_off: 'Очистить форму',
                attrName: 'code',
                linkSrc: '<a href="#" id="exlink" style="color: green; font-size: 12px">test</a><br /><br />',
                clicked: false
            }, options || {});
        
        $(ex.linkSrc)
            .filter('a[id=exlink]')
            .text(ex.caption_on)
            .click(function (e){
                if (ex.clicked){
                    $(e.target).text(ex.caption_on);
                    $('*[' + ex.attrName + ']').each(function(n){ 
                        $(this).val('');
                    })
                }
                else{
                    $(e.target).text(ex.caption_off);
                    $('*[' + ex.attrName + ']').each(function(n){ 
                        $(this).val(ex[$(this).attr(ex.attrName)]);
                    })
                }
                ex.clicked = !ex.clicked;
                return false;
            })
            .end()
            .insertBefore(elemBefore);
    }
    
    $.incCounter = function(url, type, counter){
        $.get(url, {'type': type, 'counter': counter}, null);
    }
    
    $.emailAccepted = function(source, args){
        args.IsValid = args.Value.match(/^([a-zA-Z0-9_\.\-+])+@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/)
    }
    
    $.dateAccepted = function(source, args){
        args.IsValid = args.Value.match(/^\d{2,4}$/)
    }
    
    $.fn.createFlashBanner = function(){
        return this.each(function(idx, item) {
            var banner = $(item);
            var url = banner.attr('href');
            var clickFun = banner.attr('onclick');
            banner.removeAttr('onclick');
            
            
            //banner.die('click');
                       
            banner.parent().css('z-index', '10000');
            var args = {
                flashParams: {
                    src: banner.attr('src'),
                    width: banner.attr('width'),
                    height: banner.attr('height'),
                    wmode: "transparent",
                    quality: "high",
                    id: banner.attr('id') + '_flash'
                }
            };
            var flash = banner.flash(args.flashParams);
            
            if (banner.hasClass('allowLink')){
                var a = jQuery('<a href="' + url + '" target="_blank" title="' + banner.attr('title')  + '" bid="' + banner.attr('bid') + '" style="cursor:pointer"></a>');

                var div = jQuery('<div style="background:url(png.fix); height: ' +
                    args.flashParams.height + 'px; width:' + 
                    args.flashParams.width + 'px; position: absolute; left:' + 
                    flash.offset().left + 'px; top:' + 
                    flash.offset().top + 'px; z-index:10; display:block"></div>');
                
                div.appendTo(a);
                    
                var obj = jQuery('object', banner);
                var embed = jQuery('embed', banner);

                if (clickFun != null)
                {
                    jQuery.extend(args, {
                        bannerElement: banner,
                        flashObject: flash,
                        objectElement: obj,
                        aElement: a,
                        url: url
                    });
                    a.bind('click', args, clickFun);
                }            
                a.insertAfter(obj);
            }
        });
        
    }    
    
})(jQuery)


jQuery(document).ready(function(){
    jQuery('.flash').createFlashBanner();
});

ReclamaHelper = function(elem, heightId, widthId, tableId){
    this._heightId = heightId;
    this._widthId = widthId;
    this.tableId = tableId;
    
    var self = this;
    elem.bind('change', function(){ self.changeReclameType(this.value) })
        .bind('keyup', function(){ self.changeReclameType(this.value) });
}

ReclamaHelper.prototype = {
    priorValue : null,
    
    changeReclameType : function (v){
        if (v != this.priorValue){
            var val = (v == 1 ? "none" : "");
            jQuery(this._heightId).css('display', val);
            jQuery(this._widthId).css('display', val);
            
            jQuery('.bannerValidator', this.tableId).each(function(idx, item){
                item.style.display = val;
                ValidatorEnable(item, (v == 2));
            });
            this.priorValue = v;
        }
    }
}

    
