
$(document).ready(function() {
    
    function resize_top(){
        var height=$(document).height()/5;
        $('#content,#content1').css('margin-top',height+"px");
    }
   
    function physix(el){
      

        el.effect("shake", {
            times:1,
            distance:10
        }, 150);
      
      
    }  
   
   
    $(window).bind('resize', function() {
       // resize_top()
    });
  //  resize_top()
   
    /*** newsletter **/
    $("#abonare").click(function(e){
        e.preventDefault();
        id=$(this).closest('.news_container').attr('newsid'); // find the closest news container
        thisTime=$(this).closest('.news_container').find('.date'); //find date
        var th=$(this);
        $.post("/main/abonare",{
            single:true,
            id:id,
            lang:lang
        }, function(html){
            if (html.length>7){
                html='<div id="fancyDiv" style="display:none">'+html+'</div>';
                $.fancybox({
                    'content' : html,
                    'title' : $(thisTime).html(),
                    'width'	: '50%',
                    'height' : '40%',
                    'autoScale' : true,
                    'autoDimensions': false,
                    'overlayColor' : '#000',
                    'overlayOpacity': 0.7,
                    'transitionIn'   : 'elastic',
                    'transitionOut'   : 'elastic',
                    'easingIn' : 'easeOutQuad',
                    'easingOut' : 'easeInQuad',
                    'onStart' :function (){
                        $("html").css('overflow','hidden');
                    },
                    'onClosed' : function(){
                        $("html").css('overflow-y','auto');
                    },
                    'onComplete' : function(){
                        $("#fancybox-title-wrap").before("<div id='fancy_print' style='float:left; cursor:pointer'><img src='fancybox/print.png' class='unitPng' /></div>")
                        .css('margin-right','40px');
                        $("#fancybox-wrap").css({
                            'top':'0'
                        });
                        $("#fancybox-title").css({
                            'top':'-20px',
                            'bottom':'auto',
                            'height':'30'
                        });
                        $("#fancybox-inner").css({
                            'color':'#000',
                            'height':'100%',
                            'top':'0'
                        })
                        $("#fancybox-outer").css({
                            'width':'99%',
                            'height':'103%'
                        });
                        $("#fancyDiv").css({
                            'padding-left':'30px',
                            'padding-right':'20px',
                            'padding-bottom':'10px',
                            'padding-top':'20px',
                            'text-align':'justify',
                            'font-family':'times New Roman',
                            'height':'80%'
                        }).fadeIn('slow');
                        $('#fancy_print').click(function(){
                            var html= $("#fancyDiv").html();
                            printIt(html);
                        });
                    }
                });
            }//if html>7 chars
        })//post
    })//click
    /*** ***/
    var wd=$("#content").width();
    var dir=new Array('lr', 'rl');
    //-----------------------------------bg functions
    //replace whitespaces
    String.prototype.trim = function() {
        return this.replace(/^\s+|\s+$/g,"");
    }
    
    //html decode
    function htmlDecode(uri){
        return unescape($("<p></p>").html(uri).text());
    }
    function isIE()
    {
        return (navigator.userAgent.indexOf('MSIE') !=-1)
    }
    function verIE(){
        if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
            var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
            return ieversion;
        }
        else return 0;
    }
    function resize(){
        if($("#path img").offset().left>208)  $(".shine span").fadeIn('slow');
        else $(".shine span").fadeOut('slow');
    }
    //gallery
    //gallery
    function start_fancy(id)
    {
        var is_fancy=false;
        $(id).each(function() {
            var cl='';
            var iscl=$(this).attr('class')!=''?true:false;
            is_fancy=false;
            iscl?cl=$(this).attr('class'):'';
            if (cl!='nofancy'){
                is_fancy=true;
                $(this).attr('class','fancy');
                str="<a href='"+$(this).attr("src")+"' rel='"+id+"'>";
                /* filename=$(this).attr("src");
              /*ext='.'+filename.split('.').pop();*/
                str=str.replace('_min','')
                $(this).wrap( str + "</a>");
            }
        });
        if (is_fancy) $("a.[rel="+id+"]").fancybox({
            'transitionIn'    : 'elastic',
            'transitionOut'    : 'elastic',
            'easingIn' : 'easeOutBack',
            'easingOut' : 'easeInBack',
            'titleFormat' : function(title, currentArray, currentIndex, currentOpts) {
                return '<span id="fancybox-title-wrap"><span id="fancybox-title-left"></span><span id="fancybox-title-main">'
                + (currentIndex + 1) + ' / ' + currentArray.length + '<b style="text-align:center"> '
                + title + '</b></span><span id="fancybox-title-right"></span></span>'
            },
            'onStart': function() {
                $("html").css('overflow','hidden');
            },
            'onClosed': function() {
                $("html").css('overflow-y','auto');
            }
        });
    }
    
    //-----------end bg functions-----
    start_fancy('#content img');
    //--------start menu----------
 
    //reclama-----------
    
    function slide(el1) {
       
        var el1=$("#"+el1);
        var nr=Math.floor(Math.random() * 2);
     
        el1.jScrollPane().flip({
            direction:dir[nr],
            color: '#EAF1F4',
	
            onEnd: function(){
		   
        
                physix(el1);               
            }

        })
        
        
    }
    function fade_img(image,text){
        $("#path").fadeOut(300, function(){
            $("#path").html("<img src='images/"+image+"'  class='unitPng'/>"+text).fadeIn();
            resize();
            $(window).resize(function(){
                resize();
            })
        })
    }

    function jpaginate(id,lang){
        //limit------
        var limit=2;
            //-------------
            //  $.post('/main/ret_news', {
            show_rows:true
        //  }, function(show_rows){
        $.post('/main/ret_news',{
            lang:lang,
            rows:limit,
            page:'1',
            sidx:'id',
            sord:'desc'
        },
        function(data){
            $(".jspPane").wrapInner('<div id="'+id+'"></div>').css({
                "position": "relative",
                "padding-bottom":"50px"
            });
            $('#'+id).before('<div id="news_div" style=""></div>');
            if (verIE()!='6') {
                $('#'+id).css('width','200px');
            }
            var obj = jQuery.parseJSON(data);
            var totalpages=Math.ceil(obj.records/limit);
            var records=obj.records;
            load_news(1, limit, records,lang);
            $('#'+id).paginate({
                count : totalpages,
                start : 1,
                display : 5,
                border: true,
                border_color	: '#62E0FF',
                text_color  	: '#fff',
                background_color   	: '#1FBDFA',
                border_hover_color: '#1FBDFA',
                text_hover_color  : '#2D438C',
                background_hover_color: '#62E0FF',
                images: false,
                mouse: 'press',
                onChange : function(page){
                    load_news(page, limit, records,lang);
                }
            });
            switch(lang){
                case 'ro' :{
                    $(".jPag-first").html('Prima');
                    $(".jPag-last").html('Ultima')
                };
                break;
                case 'ru' : {
                    $(".jPag-first").text('Перв.');
                    $(".jPag-last").text('Посл.')
                };
                break;
            }
        })
    //})
    }
    //------------------
    //----------end menu ----------
    $('#loader')
    .hide()  // hide it initially
    .ajaxStart(function() {
        $.validationEngine.closePrompt(".formError",true)     ;
        $(this).show();
    })
    .ajaxStop(function() {
        $(this).hide();
    //  auto_height();
    });
    //--------------ajax form validation
    //bg functions
    function create_recaptcha(){
        if($('.go_contact,.go').length>0)
            $.post('/main/get_captcha',{
                div:'captcha_div'
            },function (response){
                $('#captcha').html(response);
            });
    }
    function ckbox(va,ids){
        va='.'+va+':checked';
        id='#'+ids;
        if ($(va).val()=='on'){
            if ($(id).length!= 0) $.validationEngine.closePrompt('.'+ids+'formError',true);
            return true
        }
        else {
            if ($(id).length!= 0) $.validationEngine.buildPrompt(id,'Selectati cel putin o optiune','error');
            return false
        }
    }
    function is_valid(){
        if ($("#formular").validationEngine({
            returnIsValid:true
        })
        && ckbox('checkbox','chk')
            && ckbox('checkbox1','chk1')
                ) return true //true;
        else return false
    }
    //main
    function is_recaptcha(fx_nr)
    {
        var ch= Recaptcha.get_challenge()
        var re=Recaptcha.get_response()
        $.post('/main/get_captcha',{
            validate:'true',
            recaptcha_challenge_field:ch,
            recaptcha_response_field:re
        },
        function (response){
            if (response.replace(/^\s+|\s+$/, '')=='succes') {
                switch (fx_nr)
                {
                    case '1':
                        if (is_valid()) $('#submit').trigger('click');
                        break;
                    case '2':
                        if ($("#formID").validationEngine({
                            returnIsValid:true
                        }))
                            $('#submit_contact').trigger('click');
                        break;
                }
            }
            else{
                $.validationEngine.buildPrompt('#captcha_txt','cod gresit!','error');
                $('#captcha_txt').html(response);
            }
        });
    }
    //--------------------end ajax form validation--------------
    //lang
    $('.l_curr').click(function(){
        var lang=$(this).html();
        lang=lang.toLowerCase();
        window.location='/main/index/'+lang;
    })
    $('.l_curr').each(function(){
        //$('#main_1 span').removeClass('blob_l');
        var lang=$('#l_sel').html();
        lang=lang.toUpperCase();
        if ($(this).html()==lang) $(this).css({
            'color':'red',
            'background':'url(images/tr.png) no-repeat',
            'padding':'5px'
        });
    //$('#'+lang).addClass('blob_l');
    })
    //lang variables
    
    //
    //--------slider---------------------------------------------------
    $('#h_logo').hide();
    $('#logo').show();
    //----------------
    $('#slidesHolder1, #slidesHolder2').mouseover(function(){
        var id="#"+$(this).attr('id')+' div';
        $(id).css('background','')
    })
    $('#slidesHolder1, #slidesHolder2 ').mouseout(function(){
        var id="#"+$(this).attr('id')+' div';
        $(id).css('background','url(images/2.jpg)')
    })

    $("#path").html($(".active").html());
    fade_img("home.png", $(".active").html());
    
    slide("content");
if (window.location.pathname=="/")  $("#menu li:first-child").addClass('active');

//---------------end jquery--------------------
});
