donejs.rotex.cmaps = new Class({
    initialize: function(cont, lat, lng){
        this.lat = lat;
        this.lng = lng;

        var txt = $('cmapsdescr');

        if (txt) {
            this.txt = txt.innerHTML;
        }

        var map = $(cont);
        if (map) {// && google && GBrowserIsCompatible()) {

            /*
             var button = new Element('a',{
             'class':'button',
             'href':'#map',
             'opacity':0.1
             });
             button.set('title','Kliknij aby powiÄkszyÄ.');
             map.appendChild(button);
             */
            map.addEvent('click', this.show.bind(this));

            //this.gmaps(mapG, 14);

            this.dom = map;

            this.popup = new Element('div', {
                'id': 'cmaps_popup'
            });
            document.body.appendChild(this.popup);
            var _self = this;
            this.tween = new Fx.Morph(this.popup, {
                duration: 400,
                transition: Fx.Transitions.Sine.easeIn,
                onComplete: function(){
                    if (!_self.isShow) {
                        _self.isShow = true;
                        _self.showMap();
                    }
                    else {
                        _self.isShow = false;
                        _self.popup.style.display = 'none';
                    }
                }
            });

        }
    },
    gmaps: function(cont, zoom,big){
        var icon = new GIcon();
        icon.image = URL_THEME + "/img/cwin.png";
        icon.iconSize = new GSize(197, 88);
        icon.shadow = URL_THEME + "/rotex/themes/Default/img/cwins.png";
        icon.shadowSize = new GSize(238, 51);

        icon.iconAnchor = new GPoint(24, 88);
        icon.infoWindowAnchor = new GPoint(24, 88);


        var gmap = new GMap2(cont);
        gmap.clearOverlays();
        var point = new GLatLng(this.lat, this.lng);
        gmap.setCenter(point, zoom);
        var marker = new GMarker(point, icon);
        gmap.addOverlay(marker);
        // dla opcji teren itd.
        // gmap.addControl(new GMapTypeControl());

        if (big) {
            gmap.addControl(new GLargeMapControl());
        //            if (this.txt) {
        //                var txt = this.txt;
        //                GEvent.addListener(marker, "click", function(){
        //                    marker.openInfoWindowHtml(txt);
        //                });
        //            }
        }
        var _self = this;
        var isMove = false;
        this.maps = this.maps || [];
        this.maps.push(gmap);
    },
    show: function(){
        if (this.isShow) {
            return false;
        }
        Mask.show();

        this.initPopup();
        var pos = this.dom.getPosition();
        var size = this.dom.getSize();

        var w = 725;
        var h = 570;
        var ws = window.getSize();
        if (h > ws.y + 50) {
            h = ws.y - 50;
        }
        if (w > ws.x + 40) {
            w = ws.x - 40;
        }
        var y = ws.y / 2 + window.getScroll().y - h / 2;
        var x = ws.x / 2 + window.getScroll().x - w / 2;

        this.tween.start({
            'height': [size.y, h],
            'width': [size.x, w],
            'top': [pos.y, y],
            'left': [pos.x, x]
        });
        this.popup.style.display = 'block';
        this.h = h;
        this.w = w;
        this.x = x;
        this.y = y;
        return true;
    },
    showMap: function(){
        if (this.pgmap) {
            this.pgmap.style.display = 'block';
            this.pclose.style.display = 'block';
            return false;
        }
        //mak
        this.d = new Element('div', {
            'class': 'body',
            'html': ' <iframe width="725" height="550" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.pl/maps?f=q&amp;source=s_q&amp;hl=pl&amp;geocode=&amp;q=ROTEX+hometextile&amp;sll=51.609239,18.936493&amp;sspn=0.007662,0.016737&amp;ie=UTF8&amp;cid=6664045692137261675&amp;ll=51.614446,18.941374&amp;spn=0.029313,0.062227&amp;z=14&amp;iwloc=A&amp;output=embed"></iframe><br/><small><a href="http://maps.google.pl/maps?f=q&amp;source=embed&amp;hl=pl&amp;geocode=&amp;q=ROTEX+hometextile&amp;sll=51.609239,18.936493&amp;sspn=0.007662,0.016737&amp;ie=UTF8&amp;cid=6664045692137261675&amp;ll=51.614446,18.941374&amp;spn=0.029313,0.062227&amp;z=14&amp;iwloc=A&amp;source=embed" style="color:#333333;text-align:left;padding-left:5px" target="_blank">Wyświetl większą mapę</a></small>'
        });
        var d = this.d ;
        this.popup.appendChild(d);
        d.setStyles({
            'width': this.w,
            'height': this.h
        });
//        this.gmaps(d, 15, true);
//        this.pgmap = d;
        d = new Element('div', {
            'class': 'close'
        });
        d.innerHTML = lang.close;
        this.popup.appendChild(d);
        d.addListener('click', this.close.bind(this));        
        this.pclose = d;
        return true;
    },
    close: function(){
        Mask.hide();
        if (!this.isShow) {
            return false;
        }
        this.d.style.display = 'none';
        
        //this.pgmap.style.display = 'none';
        //this.pclose.style.display = 'none';

        var pos = this.dom.getPosition();
        var size = this.dom.getSize();

        this.tween.start({
            'height': [this.h, size.y],
            'width': [this.w = size.x],
            'top': [this.y, pos.y],
            'left': [this.x, pos.x]
        });                          
        return true;             
    },
    initPopup: function(){
        var pos = this.dom.getPosition();
        var size = this.dom.getSize();
        this.popup.setStyles({
            'left': pos.x,
            'top': pos.y,
            'width': size.x,
            'height': size.y
        });             

    }
});
donejs.rotex.contact = new Class({
    initialize:function(){
        this.isActive = false;
        var p = $$('.contactPage');
        if(p.length > 0){
            this.dom = p[0];
            this.fields = [
            {
                name:'name',
                id:'ctl0_Main_fname',
                type:['required']
            },

            {
                name:'firm',
                id:'ctl0_Main_ffirm',
                type:['required']
            },

            {
                name:'email',
                id:'ctl0_Main_femail',
                type:['required','email']
            },

            {
                name:'phone',
                id:'ctl0_Main_fphone',
                type:[]
            },

            {
                name:'topic',
                id:'ctl0_Main_ftopic',
                type:['required']
            },
            {
                name:'descr',
                id:'ctl0_Main_fdescr',
                type:['required']
            }
            ];
            this.submit = $('ctl0_Main_ctl14')
            if(this.submit){
                this.submit.addEvent('click',this.check.bindWithEvent(this));
                this.validator = new donejs.validator(this.fields);
                this.validator.reset();
                this.fields.each(function(inp){
                    var el = $(inp['id']);
                    if(el){
                        el.addEvent('keyup',this.active.bind(this));
                    }
                },this);
            }

            var dtxt = $('fdescr').getElement('textarea');
            if(dtxt){
                var s = dtxt.getSize();
                var txtScroll = new donejs.scroll({
                    top:false
                });
                txtScroll.use(dtxt,true);

                dtxt.addEvent('keydown',function(e){
                    if(e.code == 38){
                        txtScroll.up();
                    }else if(e.code == 40){
                        txtScroll.down();
                    }else{
                        dtxt.setStyle('height',s.y);
                        if(s.y <= dtxt.getScrollSize().y){
                            txtScroll.use(dtxt);
                            try{
                                dtxt.focus();
                            }catch(e){
                            }
                        }
                    }
                });
            }
        }
    },
    active:function(){
        var isEmpty = false;
        this.fields.each(function(el){
            if(el.name != 'phone' && !isEmpty){
                var el = $(el['id']);
                if(el){
                    var v = el.get('value');
                    if(!v || v.length == 0 ){
                        isEmpty = true;
                    }
                }
            }
        });

        if(isEmpty){
            this.submit.removeClass('active');
            this.isActive = false;
        }else{
            this.submit.addClass('active');
            this.isActive = true;
        }
    },

    check:function(e){
        e.stop();
        if(this.isActive){
            if(!this.validator.submit()){
                donejs.message.error(lang.contact, lang.errorform);
            }else{
                this.send('PageContentService.setContactData',this.validator.getAll());
            }
        }
    },
    send:function(act,request){
        if(!this.isSend){
            this.isSend = true;
            this.submit.addClass('loader');
            var _self = this;
            var jsonRequest = new Request.JSON({
                url: URL + "/?json="+act,
                onComplete:function(json){
                    _self.response(json,request);
                }
            }).post({
                json:JSON.encode(request)
            });
        }
    },
    response:function(data,request){
        this.submit.removeClass('loader');
        this.submit.removeClass('active');
        if(data){
            var s = data.status;
            var msg = data.msg || '';
            if(s==0){
                donejs.message.info(lang.contact,msg);
                this.validator.reset();
                this.isActive = false;

            }else{
                donejs.message.error(lang.contact,msg);
            }
        }
        this.isSend = false;
    }
});

var contactMap;

function showContactMap(){
    contactMap.show();
    
    
}

window.addEvent('domready', function(){
    contactMap = new donejs.rotex.cmaps('cmaps',51.609543,18.937447);
    new donejs.rotex.contact();
});



 

