// for scrolling the modal window
var name = null    // name of the element that scrols with the page
var menuYloc = null;        // value of the current pos


jQuery(document).ready(function() {

    //Get the screen height and width
    var maskHeight = jQuery(document).height();
    var maskWidth = jQuery(window).width();

    //Get the window height and width
    var winH = jQuery(window).height();
    var winW = jQuery(window).width();

    //select all the a tag with name equal to modal
    jQuery('a[name=modal]').click(function(e) {
       if(typeof(window.pageYOffset)=='number') {
            scrH=window.pageYOffset;
        }else {
            scrH=document.documentElement.scrollTop;
        }
        scrH = scrH +50;
        //Cancel the link behavior
        e.preventDefault();

        //Get the <a tag > href value
        var id = jQuery(this).attr('href');
        id = id.replace('#','');
        var clas = jQuery(this).attr('class');
        var pId = jQuery(this).attr('id');
        name = id;
        // show loading bubble
       // jQuery().showBubble(pId,'up');
        // Scroll effect
        //menuYloc = parseInt(jQuery(name).css("top").substring(0,jQuery(name).css("top").indexOf("px")));
        menuYloc = 0;
        jQuery(window).scroll(function () {
            if(name!=null){
                offset = menuYloc+jQuery(document).scrollTop();
                jQuery('#'+name).animate({
                    top:offset+50+'px'
                },{
                    duration:300,
                    queue:false
                });
            }
        });
        jQuery().m_buildModal(id,pId,'500px','auto');
        if(id=='nletter'){
            jQuery().showLogin(id,clas,pId,maskWidth,maskHeight,scrH,winW);
        }
        if(id=='tesunam'){
            jQuery().showLogin(id,clas,pId,maskWidth,maskHeight,scrH,winW);
        }
        
    });




jQuery.fn.nagyonreg = function(lang) {


 //Get the <a tag > href value
        var id = 'ebreszto'
        var clas = 'valamitadjalneki';
        var pId = lang;
        name = id;
        if(typeof(window.pageYOffset)=='number') {
            scrH=window.pageYOffset;
        }else {
            scrH=document.documentElement.scrollTop;
        }
        scrH = scrH +50;
        // show loading bubble
      //  jQuery().showBubble(pId,'up');
        // Scroll effect
        //menuYloc = parseInt(jQuery(name).css("top").substring(0,jQuery(name).css("top").indexOf("px")));
        menuYloc = 0;
        jQuery(window).scroll(function () {
            if(name!=null){
                offset = menuYloc+jQuery(document).scrollTop();
                jQuery('#'+name).animate({
                    top:offset+50+'px'
                },{
                    duration:300,
                    queue:false
                });
            }
        });
        jQuery().m_buildModal(id,pId,'400px','auto');
        if(id=='ebreszto'){
            jQuery().showLogin(id,clas,pId,maskWidth,maskHeight,scrH,winW);
        }
        
    }



jQuery.fn.mutasdmagad = function(lang) {


 //Get the <a tag > href value
        var id = 'nletter'
        var clas = 'valamitadjalneki';
        var pId = lang;
        name = id;
        if(typeof(window.pageYOffset)=='number') {
            scrH=window.pageYOffset;
        }else {
            scrH=document.documentElement.scrollTop;
        }
        scrH = scrH +50;
        // show loading bubble
      //  jQuery().showBubble(pId,'up');
        // Scroll effect
        //menuYloc = parseInt(jQuery(name).css("top").substring(0,jQuery(name).css("top").indexOf("px")));
        menuYloc = 0;
        jQuery(window).scroll(function () {
            if(name!=null){
                offset = menuYloc+jQuery(document).scrollTop();
                jQuery('#'+name).animate({
                    top:offset+50+'px'
                },{
                    duration:300,
                    queue:false
                });
            }
        });
        jQuery().m_buildModal(id,pId,'500px','auto');
        if(id=='nletter'){
            jQuery().showLogin(id,clas,pId,maskWidth,maskHeight,scrH,winW);
        }
        if(id=='tesunam'){
            jQuery().showLogin(id,clas,pId,maskWidth,maskHeight,scrH,winW);
        }

    }


//select all the a tag with name equal to modal
    jQuery('a[name=modal2]').click(function(e) {
       if(typeof(window.pageYOffset)=='number') {
            scrH=window.pageYOffset;
        }else {
            scrH=document.documentElement.scrollTop;
        }
        scrH = scrH +50;
        //Cancel the link behavior
        e.preventDefault();

        //Get the <a tag > href value
        var id = jQuery(this).attr('href');
        id = id.replace('#','');
        var clas = jQuery(this).attr('class');
        var pId = jQuery(this).attr('id');
        name = id;
        // show loading bubble
  //      jQuery().showBubble(pId,'up');
        // Scroll effect
        //menuYloc = parseInt(jQuery(name).css("top").substring(0,jQuery(name).css("top").indexOf("px")));
        menuYloc = 0;
        jQuery(window).scroll(function () {
            if(name!=null){
                offset = menuYloc+jQuery(document).scrollTop();
                jQuery('#'+name).animate({
                    top:offset+50+'px'
                },{
                    duration:300,
                    queue:false
                });
            }
        });
        jQuery().m_buildModal(id,pId,'500px','auto');
        if(id=='nletter'){
            jQuery().showLogin(id,clas,pId,maskWidth,maskHeight,scrH,winW);
        }
        if(id=='tesunam'){
            jQuery().showLogin(id,clas,pId,maskWidth,maskHeight,scrH,winW);
        }

    });

    // show login/reg
    jQuery.fn.showLogin = function(id,clas,pId,maskWidth,maskHeight,scrH,winW) {
        if (id=='nletter'){
            jQuery.post('/website/yreuq/'+pId+'.html',{
                name:pId
            },function(msg){
                if(msg.status){
                    //show the success message

                    jQuery('#content'+id).html(msg.html);
                    jQuery().hideBubble();
                    jQuery().m_loadEffect(id,maskWidth,maskHeight,scrH,winW);
                }
            },'json');
        }else if (id=='tesunam'){
            jQuery.post('/website/tesunam/'+pId+'.html',{
                name:pId
            },function(msg){
                if(msg.status){
                    //show the success message

                    jQuery('#content'+id).html(msg.html);
                    jQuery().hideBubble();
                    jQuery().m_loadEffect(id,maskWidth,maskHeight,scrH,winW);
                }
            },'json');
        }else if (id=='ebreszto'){
            jQuery.post('/website/ebreszto/'+pId+'.html',{
                name:pId
            },function(msg){
                if(msg.status){
                    //show the success message

                    jQuery('#content'+id).html(msg.html);
                    jQuery().hideBubble();
                    jQuery().m_loadEffect(id,maskWidth,maskHeight,scrH,winW);
                }
            },'json');
        }
    }


    //if close button is clicked
    jQuery.fn.m_closeButton = function(id){
        // clear scrolling element
        name = null;
        menuYloc = null;
        // run the effect
        jQuery( "#"+id ).hide( 'scale', {
            percent: 0
        }, 500 , function(){});

        jQuery( "#"+id ).css({
            'top':0+'px'
        });
        jQuery('#mask').css({
            'display':'block'
        });
        jQuery('#mask').fadeTo("slow",0.1 ,function() {
            // torlom mindazt amit a jQuery letrehozott
            jQuery('#box').remove();
        });

    }

    // load effect
    jQuery.fn.m_loadEffect = function(id,maskWidth,maskHeight,scrH,winW) {
        //Set heigth and width to mask to fill up the whole screen
        jQuery('#mask').css({
            'width':maskWidth,
            'height':maskHeight
        });
        //jQuery('#mask').addClass(id);
        //        jQuery('#mask').css({
        //            'display':'block'
        //        });
        jQuery('#mask').show();
        //transition effect
        //jQuery('#mask').fadeIn("slow");

        jQuery('#mask').fadeTo("slow",0.75);
        //        jQuery('#mask').css({
        //            'background-color':'#000000'
        //        });
        //alert( (winH/2)-jQuery(id).height()/2);
        //Set the popup window to center
        //jQuery(id).css('top',  winH/2-jQuery(id).height()/2);
        //'top': winH/2-jQuery(id).height()/2+'px'

        jQuery('#'+id).css('left', winW/2-jQuery('#'+id).width()/2);
        jQuery('#'+id).animate({
            'top': scrH+'px'
        }, "slow", function(){
            // run code

            // shake
//            jQuery('#'+id).effect("bounce", {
//                'direction':'down',
//                times:3
//            }, 200); //, function(){
        //                            // run code
        //                            //                            if(id=='m_login'){
        //                            //                                jQuery().showLogin(id,clas,paste);
        //                            //                            }
        //                        });
        });
        //transition effect
        //                jQuery(id).css({
        //                    'display':'block'
        //                });
        jQuery('#'+id).show();
    }

    // build window
    jQuery.fn.m_buildModal = function(wind_id,pId,wind_w,wind_h) {
        // create outer content holder
        var box = jQuery(document.createElement('div'));
        var felsow=wind_w.substr(0,wind_w.length-2);
        felsow=felsow-24;
        felsow+='px';
        
        // add id
        box.attr('id','box');
        box.css({
            'background-color': '#FFFFFF'
        });
        // create modal window
        var m_wind = jQuery(document.createElement('div'));
        // set id
        m_wind.attr('id',wind_id);
        m_wind.attr('align','center');
        // set css
        m_wind.css({
            'position':'absolute',
            'width':wind_w,
            'height':wind_h,
            
            'display':'none',
            'z-index':'9999',
            'padding': '5px 5px 5px 5px',
            'border-left': 'medium #0000FF',
            'font-family': '\'Trebuchet MS\',Georgia,\'Trebuchet MS\',Arial,Helvetica,sans-serif'            
        });


        // create the close button
        var m_close = jQuery(document.createElement('a'));
       // m_close.attr('href','#'+wind_id);
        m_close.attr('name','close');
        var cle = 'jQuery().m_closeButton(\''+wind_id+'\');';
        m_close.attr('onclick',cle);
        m_close.attr('class','col_x_nletter');
        m_close.html('<img src="/website/images/bubblepopup/x_lezaro_gomb.png" width="23px" height="23px" border="none" alt="X"/>');
        var m_egybeminden = jQuery(document.createElement('div'));
        m_egybeminden.attr('id','egybemindent'+wind_id);
        m_egybeminden.css({
            'width':wind_w,
            'height':wind_h
        });

        var m_felso = jQuery(document.createElement('div'));
        m_felso.attr('id','felso'+wind_id);
        m_felso.css({
            'width':felsow,
            'height':'12px',
            'display': 'inline-block',
            'background': 'url(/website/images/bubblepopup/hatter_sarok_teteje_bal.png) repeat-x'
        });
        
        var m_balfent = jQuery(document.createElement('div'));
        m_balfent.attr('id','balfent'+wind_id);
        m_balfent.css({
            'float':'left',
            'background': 'url(/website/images/bubblepopup/hatter_sarok_felso_bal.png)',
            'width': '12px',
            'height': '12px',
            'margin-left':'-12px'
        });

        var m_jobbfent = jQuery(document.createElement('div'));
        m_jobbfent.attr('id','jobbfent'+wind_id);
        m_jobbfent.css({
            'float':'right',
            'background': 'url(/website/images/bubblepopup/hatter_sarok_felso_jobb.png)',
            'width': '12px',
            'height': '12px',
            'margin-right':'-12px'
        });

        var m_balfele = jQuery(document.createElement('div'));
        m_balfele.attr('id','balfele'+wind_id);
        m_balfele.css({
            'background': 'url(/website/images/bubblepopup/hatter_csik_also_bal.png) repeat-y left',
            'background-color': '#FFFFFF'
        });

        var m_jobbfele = jQuery(document.createElement('div'));
        m_jobbfele.attr('id','jobbfele'+wind_id);
        m_jobbfele.css({
            'background': 'url(/website/images/bubblepopup/hatter_csik_also_jobb.png) repeat-y right',
            'padding-left':'12px'

        });

        var m_also = jQuery(document.createElement('div'));
        m_also.attr('id','also'+wind_id);
        m_also.css({
            'width':felsow,
            'height':'12px',
            'display': 'inline-block',
            'background': 'url(/website/images/bubblepopup/hatter_sarok_alja_bal.png) repeat-x'
        });

        var m_ballent = jQuery(document.createElement('div'));
        m_ballent.attr('id','ballent'+wind_id);
        m_ballent.css({
            'float':'left',
            'background': 'url(/website/images/bubblepopup/hatter_sarok_also_bal.png)',
            'width': '12px',
            'height': '12px',
            'margin-left':'-12px'
        });

        var m_jobblent = jQuery(document.createElement('div'));
        m_jobblent.attr('id','jobblent'+wind_id);
        m_jobblent.css({
            'float':'right',
            'background': 'url(/website/images/bubblepopup/hatter_sarok_also_jobb.png)',
            'width': '12px',
            'height': '12px',
            'margin-right':'-12px'
        });

        // create holder for the content
        var m_content = jQuery(document.createElement('div'));
        m_content.attr('id','content'+wind_id);
        m_content.css({
            'width':wind_w,
            'height':wind_h            
        });

        // create image loading
        //var m_loading = jQuery(document.createElement('div'));
        // set css
        /*m_loading.css({
                        'background':"url('http://www.dockyardislands.ro/website/images/loading.gif')",
                        'width':'200px',
                        'height':'200px'
                    });*/

        // create mask
        var m_mask = jQuery(document.createElement('div'));
        m_mask.attr('id','mask');
        //var mask_close = 'jQuery().m_closeButton(\''+wind_id+'\');';
        m_mask.attr('onclick',cle);
        // set css
        m_mask.css({
            'position':'absolute',
            'left':'0',
            'top':'0',
            'right':'0',
            'bottom':'0',
            'z-index':'5000',
            'background-color':'#000000',
            'opacity': '0.00',
            'display':'none',
            'margin-left':'auto',
            'margin-right':'auto'
        });
//        var st_top = jQuery(document.createElement('div'));
//        st_top.attr('class','col_st_top_'+pId);
//        var st_mid = jQuery(document.createElement('div'));
//        st_mid.attr('class','col_st_mid_'+pId);
//        var st_bot = jQuery(document.createElement('div'));
//        st_bot.attr('class','col_st_bot_'+pId);
        // set window inside outer content holder
        box.append(m_wind);

        m_wind.append(m_egybeminden);
        m_egybeminden.append(m_felso);
        m_felso.append(m_balfent);
        m_felso.append(m_jobbfent);
        m_egybeminden.append(m_balfele);
        m_balfele.append(m_jobbfele);

        m_jobbfele.append(m_close);
      //  m_wind.append(m_egybeminden);
        //m_egybeminden.after(m_content);
        m_jobbfele.append(m_content);
        m_egybeminden.append(m_also);
        m_also.append(m_ballent);
        m_also.append(m_jobblent);
        m_wind.after(m_mask);

       // alert(box.html());
        jQuery('body').append(box);

    }
    

    // show bubble
    jQuery.fn.showBubble = function(id_loc,point_ing) {
        var position = jQuery('#'+id_loc).offset();

        var back = jQuery(document.createElement('div'));
        back.attr('id','backBubble');
        var b_div = jQuery(document.createElement('div'));
        b_div.attr('class','jquerybubblepopup');

        // set css
        b_div.css({
            'top':position.top+'px',
            'left':position.left+'px'
        });
        var b_table = jQuery(document.createElement('table'));

        var b_tr_top = jQuery(document.createElement('tr'));
        var b_td_top_left = jQuery(document.createElement('td'));
        b_td_top_left.attr('class','jquerybubblepopup-top-left');
        b_td_top_left.css({
            'background-image': "url('/website/images/bubblepopup/top-left.png')"
        });
        var b_td_top_middle = jQuery(document.createElement('td'));
        b_td_top_middle.attr('class','jquerybubblepopup-top-middle');
        b_td_top_middle.css({
            'background-image': "url('/website/images/bubblepopup/top-middle.png')"
        });
        var b_td_top_right = jQuery(document.createElement('td'));
        b_td_top_right.attr('class','jquerybubblepopup-top-right');
        b_td_top_right.css({
            'background-image': "url('/website/images/bubblepopup/top-right.png')"
        });

        back.html(b_div);
        b_div.append(b_table);
        b_table.append(b_tr_top);
        b_tr_top.append(b_td_top_left);
        b_td_top_left.after(b_td_top_middle);
        b_td_top_middle.after(b_td_top_right);

        var b_tr_middle = jQuery(document.createElement('tr'));
        var b_td_middle_left = jQuery(document.createElement('td'));
        b_td_middle_left.attr('class','jquerybubblepopup-middle-left');
        b_td_middle_left.css({
            'background-image': "url('/website/images/bubblepopup/middle-left.png')"
        });
        var b_td_middle_middle = jQuery(document.createElement('td'));
        b_td_middle_middle.attr('class','jquerybubblepopup-innerHtml');

        b_td_middle_middle.append('Loading...');

        var b_td_middle_right = jQuery(document.createElement('td'));
        b_td_middle_right.attr('class','jquerybubblepopup-middle-right');
        b_td_middle_right.css({
            'background-image': "url('/website/images/bubblepopup/middle-right.png')"
        });

        b_tr_top.after(b_tr_middle);
        b_tr_middle.append(b_td_middle_left);
        b_td_middle_left.after(b_td_middle_middle);
        b_td_middle_middle.after(b_td_middle_right);

        var b_tr_bottom = jQuery(document.createElement('tr'));
        var b_td_bottom_left = jQuery(document.createElement('td'));
        b_td_bottom_left.attr('class','jquerybubblepopup-bottom-left');
        b_td_bottom_left.css({
            'background-image': "url('/website/images/bubblepopup/bottom-left.png')"
        });
        var b_td_bottom_middle = jQuery(document.createElement('td'));
        b_td_bottom_middle.attr('class','jquerybubblepopup-bottom-middle');
        b_td_bottom_middle.css({
            'background-image': "url('/website/images/bubblepopup/bottom-middle.png')"
        });
        var b_td_bottom_right = jQuery(document.createElement('td'));
        b_td_bottom_right.attr('class','jquerybubblepopup-bottom-right');
        b_td_bottom_right.css({
            'background-image': "url('/website/images/bubblepopup/bottom-right.png')"
        });

        b_tr_middle.after(b_tr_bottom);
        b_tr_bottom.append(b_td_bottom_left);
        b_td_bottom_left.append(b_td_bottom_middle);
        b_td_bottom_middle.after(b_td_bottom_right);

        if(point_ing=='up'){
            var b_up_img = jQuery(document.createElement('img'));
            b_up_img.attr('class','jquerybubblepopup-tail');
            b_up_img.attr('height','10');
            b_up_img.attr('alt','');
            b_up_img.attr('src','/website/images/bubblepopup/tail-top.png');
            b_td_top_middle.append(b_up_img);
        }

        jQuery('body').append(back.html());
        //jQuery('.jquerybubblepopup').fadeIn('slow');
        jQuery( ".jquerybubblepopup" ).show();
        jQuery('.jquerybubblepopup').animate({
            'top': position.top+15+'px'
        }, "fast");

    //                    setTimeout(function () {
    //                        jQuery().hideBubble();
    //                    }, 2000);

    }

    jQuery.fn.hideBubble = function() {
        jQuery('.jquerybubblepopup').remove();
    }

    jQuery.fn.ellenorzes = function(val,hova,lang){
     
        if (lang=='lan_ro')
            szovi='Este obligatoriu de completat';
        else if (lang=='lan_hu')
            szovi='K&#246;telez&#337; kit&#246;lteni';
        else
            szovi='This field is required!';
        if (val.length<1){
                jQuery('#'+hova).html('<img src="/website/images/error.png" width="16px" height="16px" alt=""/><span class="error">'+szovi+'</span>');
                return false;
        }
        else{
            jQuery('#'+hova).html('<img src="/website/images/tick.png" width="16px" height="16px" alt=""/>');
            return true;
        }
    }

    jQuery.fn.ellenorzes = function(val,hova,lang){

        if (lang=='lan_ro')
            szovi='Este obligatoriu de completat';
        else if (lang=='lan_hu')
            szovi='K&#246;telez&#337; kit&#246;lteni';
        else
            szovi='This field is required!';
        if (val.length<1){
                jQuery('#'+hova).html('<img src="/website/images/error.png" width="16px" height="16px" alt=""/><span class="error">'+szovi+'</span>');
                return false;
        }
        else{
    
            jQuery('#'+hova).html('<img src="/website/images/tick.png" width="16px" height="16px" alt=""/>');
            return true;
        }
    }
jQuery.fn.emailellenorzes = function(val,hova,lang){
        if (lang=='lan_ro')
            szovi='V&#259; rug&#259;m s&#259; introduce&#355;i o adres&#259; valid&#259;!';
        else if (lang=='lan_hu')
            szovi='K&#233rem helyes e-mail c&#237;met adjon meg!';
        else
            szovi='Please fill a correct e-mail address!';

        var regexp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
        if (regexp.test(val)){
             jQuery('#'+hova).html('<img src="/website/images/tick.png" width="16px" height="16px" alt=""/>');
             return true;
        }
        else{
           jQuery('#'+hova).html('<img src="/website/images/error.png" width="16px" height="16px" alt=""/><span class="error">'+szovi+'</span>');
                return false;
        }


}
       // ezek itt statik clickek
    jQuery.fn.submite = function(pId) {
        
        //Get the data from all the fields
               var email = jQuery('input[name=email_]');
               var nume = jQuery('input[name=name_]');
               var prenume = jQuery('input[name=prename_]');
               var web = jQuery('input[name=web]');
               var capt= jQuery('input[name=captcha]');
               if ( jQuery().ellenorzes(nume.val(),'numehiba',pId) && jQuery().ellenorzes(prenume.val(),'prenumehiba',pId) && jQuery().emailellenorzes(email.val(),'emailhiba',pId)){
             

        jQuery.post('/website/yreuq/'+pId+'/ellenoriz.html',{
              email:email.val(),
              nume:nume.val(),
              prenume:prenume.val(),
              captcha:capt.val(),
              web:web.val()
            }
        ,function(msg){
        
            if(msg.status){
               
                //Show the success message
                jQuery('#contentnletter').html(msg.html);

                
            }
        },'json');
               }
    }


    jQuery.fn.ellenorzes_tn = function(val,hova,lang){

        if (lang=='ro')
            szovi='Este obligatoriu de completat';
        else if (lang=='hu')
            szovi='K&#246;telez&#337; kit&#246;lteni';
        else
            szovi='This field is required!';
        if (val.length<1){
                jQuery('#'+hova).html('<img src="/website/images/error.png" width="16px" height="16px" alt=""/><span class="error">'+szovi+'</span>');
                return false;
        }
        else{

            jQuery('#'+hova).html('<img src="/website/images/tick.png" width="16px" height="16px" alt=""/>');
            return true;
        }
    }
jQuery.fn.emailellenorzes_tn = function(val,hova,lang){
        if (lang=='ro')
            szovi='V&#259; rug&#259;m s&#259; introduce&#355;i o adres&#259; valid&#259;!';
        else if (lang=='hu')
            szovi='K&#233rem helyes e-mail c&#237;met adjon meg!';
        else
            szovi='Please fill a correct e-mail address!';

        var regexp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
        if (regexp.test(val)){
             jQuery('#'+hova).html('<img src="/website/images/tick.png" width="16px" height="16px" alt=""/>');
             return true;
        }
        else{
           jQuery('#'+hova).html('<img src="/website/images/error.png" width="16px" height="16px" alt=""/><span class="error">'+szovi+'</span>');
                return false;
        }


}
       // ezek itt statik clickek
    jQuery.fn.submite_tn = function(pId) {

        //Get the data from all the fields
               var email = jQuery('input[name=email_]');
               var nume = jQuery('input[name=name_]');
               var message = jQuery('#message_');
               var tel = jQuery('#tel_');
               var cap = jQuery('#captcha2');
               if ( jQuery().ellenorzes_tn(nume.val(),'numehiba',pId)  && jQuery().emailellenorzes_tn(email.val(),'emailhiba',pId)&& jQuery().ellenorzes_tn(message.val(),'messagehiba',pId)){


        jQuery.post('/website/tesunam/'+pId+'/ellenoriz.html',{
              email:email.val(),
              nume:nume.val(),
              tel:tel.val(),
              captcha2:cap.val(),
        message:message.val()
            }
        ,function(msg){

            if(msg.status){

                //Show the success message
                jQuery('#contenttesunam').html(msg.html);


            }
        },'json');
               }
    }

});
