$(document).ready(function(){
    // Datepicker
    $('#datepicker').datepicker({
        inline: true
    });
    
    $("div.button").overlay({
        mask: {
            color: 'gray',
            loadSpeed: 200,
            opacity: 0.5
        },
        fixed: false,
        top: 'center',
        
        target: 'div.overlay',

        // when overlay is opened, load our player
        onLoad: function() {},

        // when overlay is closed, unload our player
        onClose: function() {}
    });
    
    $("a.news_preview").fancybox({
        'titleShow'        : false,
        'transitionIn'     : 'none',
        'transitionOut'    : 'none'
    });
    
    $("a.gal_preview").fancybox({
        'titleShow'        : true,
        'transitionIn'     : 'none',
        'transitionOut'    : 'none',
        'titleFormat'      : formatGalleryTitle
    });
    
    $("a.static_preview").fancybox({
        'titleShow'        : true,
        'titlePosition'    : 'inside',
        'transitionIn'     : 'none',
        'transitionOut'    : 'none'
    });
    
    $("img#blaros").mouseenter(function()
    {
       $(this).effect('shake', { times:2, distance: 3, direction: 'up' }, 100);
    });
    
})

function blink(time, interval){
    var timer = window.setInterval(function(){
        $("img#blaros").css("border", "0");
        window.setTimeout(function(){
            $("img#blaros").css("border", "5px solid #cccccc");
        }, 100);
    }, interval);
    window.setTimeout(function(){clearInterval(timer);}, time);
}

function formatGalleryTitle(title, currentArray, currentIndex, currentOpts)
{
    return '<div id="">' + 'Imaginea ' + (currentIndex + 1) + ' din ' + currentArray.length + '</div>';
}

function showFullArticle(elem)
{
    $(elem).parent().slideToggle('fast');
    $(elem).parent().next().slideToggle('fast');
}
function showSmallArticle(elem)
{
    $(elem).parent().slideToggle('fast');
    $(elem).parent().prev().slideToggle('fast');
}

//  global confirmation message
function showConfirm(message)
{
    $('#confirm').html(message);
    $('#confirm').dialog({'modal':true});
}


//  new advert popup validation
function validatePopup()
{
    var b_valid = true;
    
    $('#advert_form .s01').hide();
    $('#advert_form .s11').hide();
    $('#advert_form .s21').hide();
    $('#advert_form .s31').hide();
    $('#advert_form .s41').hide();
    
    var n_cat = $('#advert_form select.i0').val();
    var s_title = $('#advert_form .i1').val();
    var s_content = $('#advert_form .ta').val();
    var s_email = $('#advert_form .i2').val();
    var s_phone = $('#advert_form .i3').val();
    var s_entered_code = $('#advert_form .i5').val();
    var s_random_code = $('#advert_form .s7 b').html();
    
    if(check_profanity(s_title) && check_profanity(s_content) && check_profanity(s_email))
    {
        //  validate category
        if(n_cat == 0)
        {
            b_valid = false;
            $('#advert_form .s01').show();
        }
        
        //  validate title
        if(s_title == '')
        {
            b_valid = false;
            $('#advert_form .s11').show();
        }
        
        //  validate content
        if(s_content == '')
        {
            b_valid = false;
            $('#advert_form .s21').show();
        }
        
        //  validate phone email and phone
        if(s_email == '' && s_phone == '')
        {
            b_valid = false;
            $('#advert_form .s31').show();
        }

        if(s_entered_code != s_random_code)
        {
            b_valid = false;
            $('#advert_form .s41').show();        
        }
        
        else if(s_email!='' && !validateEmail(s_email))
        {
            b_valid = false;
            $('#advert_form .s31').html('Adresă de e-mail invalidă.');
            $('#advert_form .s31').show();
        }
        else if(s_phone!='' && !validatePhone(s_phone))
        {
            b_valid = false;
            $('#advert_form .s31').html('Telefon invalid.');
            $('#advert_form .s31').show();        
        }
    }
    else
    {
        b_valid = false;
        
        //close the popup
        $("div.button").each(function(){
            $(this).overlay().close();
        })
        
        //  clear the inputs
        $('#advert_form :input').val("");
        $('#advert_form textarea').val("");
        
        //  show message
        showConfirm('S-au detectat cuvinte inadecvate. Știrea nu a fost adăugată.')
    }
    
    if(b_valid)
    {
        $('#advert_form input[name=sec]').val($('#advert_form .s7 b').html());
        $('#advert_form').submit();
    }
}

// regex validation for email
function validateEmail(elementValue)
{  
   var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;  
   return emailPattern.test(elementValue);  
}

// regex validation for phone
function validatePhone(elementValue)
{  
   var phonePattern = /^[+]{0,1}[0-9 .]{8,}$/;  
   return phonePattern.test(elementValue);  
}

function validateNews()
{
    var b_valid = true;
    
    var n_cat = $('#news_form select.i0').val();
    var s_title = $('#news_form .i1').val();
    var s_content = $('#news_form .ta').val();
    var s_source = $('#news_form .i2').val();
    
    $('#news_form .s01').hide();
    $('#news_form .s11').hide();
    $('#news_form .s21').hide();
    $('#news_form .s31').hide();    
    
    if(check_profanity($('#news_form textarea').val()))
    {
        //  validate category
        if(n_cat == 0)
        {
            b_valid = false;
            $('#news_form .s01').show();
        }
        //  validate title
        if(s_title == '')
        {
            b_valid = false;
            $('#news_form .s11').show();
        }
        //  validate content
        if(s_content == '')
        {
            b_valid = false;
            $('#news_form .s21').show();
        }
        
        if (b_valid)
        {
            $('#news_form').submit();
        }
    }
    else
    {
        showConfirm('S-au detectat cuvinte inadecvate. Știrea nu a fost adăugată.')
    }
}
