var name = null    // name of the element that scrols with the page
var menuYloc = null;        // value of the current pos
var formSave = null;
jQuery.noConflict();
jQuery(document).ready(function() {

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

    //Get the window height and width
    var winH = jQuery(window).height();
    var winW = jQuery(window).width();
        
    
    //select the a tag with name equal to modal
    jQuery('a[name=modal]').click(function(e) {
        //Cancel the link behavior
        e.preventDefault();
        if(typeof(window.pageYOffset)=='number') {
            scrH=window.pageYOffset;
        }else {
            scrH=document.documentElement.scrollTop;
        }        
        scrH = scrH+50;
        
        var id = jQuery(this).attr('href');
        name = id;
        // show loading bubble      
        // 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
                });
            }
        });
        
        // ez annak van ha van egy form elott aminek van egy imputja es
        // be kell irjad az email cimet akkor ez megoldja hogy az ott beolvasott email cim ertek
        // keruljon be a form unk email inputjaba
        if(jQuery(this).attr('id')=='em'){
            jQuery('input[name=email_o]').val(jQuery('input[name=email_of]').val());
        }
        
        //Set heigth and width to mask to fill up the whole screen
        jQuery('#mask').css({
            'width':maskWidth,
            'height':maskHeight
        });

        jQuery('#mask').show();
        //transition effect
        //jQuery('#mask').fadeIn("slow");

        jQuery('#mask').fadeTo("slow",0.75);

        //Set the popup window to center
//        jQuery(id).animate({
//            'top': winH-jQuery(id).height()/2+'px'
//        }, "slow");
//        jQuery(id).css('left', winW/2-jQuery(id).width()/2);
//
//        // shake effect
//        jQuery(id).effect("bounce", {
//            'direction':'down', 
//            times:3
//        }, 200);
        
        jQuery(id).css('left', winW/2-jQuery(id).width()/2);
        jQuery(id).animate({
            'top': scrH+'px'
        }, "slow", function(){
            // shake
            jQuery(id).effect("bounce", {
                'direction':'down',
                times:3
            }, 200);
        });

        //transition effect
        //        jQuery(id).css({
        //            'display':'block'
        //        });

        jQuery(id).show();
        formSave = jQuery('.form').html();
        //alert(jQuery('.form').html());

    });

    //if close button is clicked
    jQuery('.window .close').click(function (e) {
         
        //Cancel the link behavior
        e.preventDefault();

        jQuery('#dialog').animate({
            'top' : -((winH/2-jQuery('#dialog').height()/2)+plusHeight)+'px'
        }, 'slow');

        jQuery('#mask').css({
            'display':'block'
        });
        jQuery('#mask').fadeTo("slow",0.1 ,function() {
            jQuery('#mask').hide();
            jQuery('.form').html('');            
            jQuery('.form').html(formSave);
        });
        name=null;
    });

    //if mask is clicked
    jQuery('#mask').click(function () {

        jQuery('#dialog').animate({
            'top': -((winH/2-jQuery('#dialog').height()/2)+plusHeight)+'px'
        }, 'slow');

        jQuery('#mask').css({
            'display':'block'
        });
        jQuery('#mask').fadeTo("slow",0.1 ,function() {
            jQuery('#mask').hide();
            jQuery('.form').html('');
            jQuery('.form').html(formSave);
        });
        name=null;
    });

    /* The following code is executed once the DOM is loaded */

    /* This flag will prevent multiple comment submits: */
    var workinge = false;
        

    /* Listening for the submit event of the form: */
    jQuery('#abonareForma').submit(function(e){
                
        e.preventDefault();
        if(workinge) return false;
                
        workinge = true;
        jQuery('#submitab').val('Working..');
        jQuery('span.error').remove();
                
        jQuery.post('/newsletter.php',jQuery(this).serialize(),function(msg){
          
            workinge = false;
            jQuery('#submitab').val('Send');
                        
            if(msg.status){
                                
                //show the success message
                formSave = jQuery('.form').html();                                
                jQuery('.form').html(msg.html);
                jQuery('.form').fadeIn('slow');                                

            }else {
                formSave = jQuery('.form').html();
                /*
				/	If there were errors, loop through the
				/	msg.errors object and display them on the page
				/*/

                jQuery.each(msg.errors,function(k,v){
                    //jQuery('label[for='+k+']').append('<span class="error">'+v+'</span>');
                    jQuery('input[name='+k+'_o]').addClass('hightlight');
                    jQuery('input[name='+k+'_o]').val(v);

                });
            }
        },'json');
                

    //});

    });
});
