 /*var webroot = "/best/";*/
 var webroot = "/";   
    
/*newsletter add*/    
function checkEmail(frm)
{
    var re = new RegExp('([A-Za-z0-9._%+-]+)@([A-Za-z0-9-]+).([A-Za-z]{2,4})');
    if(frm.newsletter_username.value.replace(/^\s+|\s+$/g, '')=='')
    {
        alert("Va rugam sa introduceti adresa de email!");
        frm.newsletter_username.focus();
        return false;
    }
    else if(!frm.newsletter_username.value.match(re))
    {
        alert("Adresa de email nu este valida!");
        frm.newsletter_username.focus();
        return false;
    }
    else 
    {
        return true;
    }    
}

//Document's loaded, register the click trigger
$(document).ready(function()
{
    $("#newsletter_username").focusin(function () 
    {
        if ($(this).val() == "Adresa e-mail")
        {
            $(this).val('');
        }
    });
    $("#newsletter_username").focusout(function () 
    {
        if ($(this).val().length === 0)
        {
            $(this).val('Adresa e-mail');
        }
    });
    $("#want_username_login").focusin(function () 
    {
        if ($(this).val() == 'Username')
        {
            $(this).val('');
        }
    });
    $("#want_username_login").focusout(function () 
    {
        if ($(this).val().length === 0)
        {
            $(this).val('Username');
        }
    });
    $("#want_password_login").focusin(function () 
    {
        if ($(this).val() == 'Password')
        {
            //$(this).val('');
            $('#want_password_login').hide();
            $('#want_password_masked_login').show();
            $('#want_password_masked_login').focus();
        }
    });

    $('#want_password_masked_login').blur(function()
    {
        if($('#want_password_masked_login').val() == '')
        {
            $('#want_password_login').show();
            $('#want_password_masked_login').hide();
        }
    });
    $("#submit_newsletter").click(function()
    {
        submitNewsletterForm();
        return false;
    });
    $("#submit_contact_form").click(function()
    {
        submitContactForm();
        return false;
    });
    $("#submit_concurs_form").click(function()
    {
        submitConcursForm();
        return false;
    });
    $("#submit_login").click(function()
    {
        submitLogin();
        return false;
    });
    /*search start*/
    $("#want_cautare").focusin(function () 
    {
        if ($(this).val() == 'Cauta')
        {
            $(this).val('');
        }
    });
    $("#want_cautare").focusout(function () 
    {
        if ($(this).val().length === 0)
        {
            $(this).val('Cauta');
        }
    });

    // Change form action to suite what the users wants to search
    $("#want_ce_caut").change(function() 
    {
        if ($(this).val() == 'evenimente')     $("#search_form").attr("action", webroot+"evenimente.html");
        if ($(this).val() == 'stiri')         $("#search_form").attr("action", webroot+"noutati.html");
        if ($(this).val() == 'foto')         $("#search_form").attr("action", webroot+"galerii-foto.html");
        if ($(this).val() == 'locatii')        $("#search_form").attr("action", webroot+"locatii.html");
        if ($(this).val() == 'lyrics')        $("#search_form").attr("action", webroot+"lyrics.html");
    });
    /*search end*/
}); 
function submitContactForm()
{
    $.ajax({type:"POST", url:webroot+"contact_form", data:$("#contact_form").serialize(), cache:false, timeout:10000,
        success: function()
        {
            // Request has been successfully submitted
            // alert("Succeeded");
        },
        error: function()
        {
            // An error occurred, do something about it
            alert("Failed");
        },
        complete: function(responseText)
        {
            // We're all done so do any cleaning up.
            // alert("Regardless of the outcome we're going to add the comment for demonstration purposes:");
            addCommentContact(responseText.responseText);
        }
    });
}

// Function to add the comment to the page
function addCommentContact(responseText)
{
    $(".contact_response").html(responseText);
    $(".contact_response").fadeIn("slow");
    $(".contact_response").delay(5000).fadeOut('slow');
}
function submitConcursForm()
{
    $.ajax({type:"POST", url:webroot+"contests/concurs_form", data:$("#concurs_form").serialize(), cache:false, timeout:10000,
        success: function()
        {
            // Request has been successfully submitted
            // alert("Succeeded");
        },
        error: function()
        {
            // An error occurred, do something about it
            alert("Failed");
        },
        complete: function(responseText)
        {
            // We're all done so do any cleaning up.
            // alert("Regardless of the outcome we're going to add the comment for demonstration purposes:");
            if(responseText.responseText=="Mesajul a fost trimis către departamentul de concursuri. Vă mulţumim pentru participare.")
            {
                $("#concurs_form")[0].reset();
            }
            addCommentConcurs(responseText.responseText);
        }
    });
}

// Function to add the comment to the page
function addCommentConcurs(responseText)
{
    $(".contact_response").html(responseText);
    $(".contact_response").fadeIn("slow");
    $(".contact_response").delay(5000).fadeOut('slow');
}
   
function submitNewsletterForm()
{
    $.ajax({type:"POST", url:webroot+"newsletter", data:$("#newsletter_form").serialize(), cache:false, timeout:10000,
        success: function()
        {
            // Request has been successfully submitted
            // alert("Succeeded");
        },
        error: function()
        {
            // An error occurred, do something about it
            alert("Failed");
        },
        complete: function(responseText)
        {
            // We're all done so do any cleaning up.
            // alert("Regardless of the outcome we're going to add the comment for demonstration purposes:");
            addCommentLocally(responseText.responseText);
        }
    });
}
function addCommentLocally(responseText)
{
    $(".newsletter_response").html(responseText);
    $(".newsletter_response").fadeIn("slow");
    $('.newsletter_response').delay(5000).fadeOut('slow');
}
/*newsletter end*/

function decode(encoded) 
{
    return decodeURIComponent(encoded.replace(/\+/g,  " "));
}
/*login start*/
function submitLogin()
{
    var want_return = $("#want_return").val();
    want_return  = decode(want_return);
    $.post(webroot+"users/login", { want_username:$("#want_username_login").val(), want_password:$("#want_password_masked_login").val() } ,function(data)
    {
        $("#msgbox_login").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow");
        
        if (data=='incorrect')
        {
            $("#msgbox_login").fadeTo(200,0.1,function()
            {
                $(this).html('Parola incorecta.').addClass('messageboxerror').fadeTo(900,1);
            });
            $('#want_password_masked_login').css("background","#ffcfcf");
            $('#want_password_masked_login').css("border-color","#ff7d7d");
        }
        else if (data=='ok')
        {
            /*var user_name = '';
            $("#msgbox_login").fadeTo(200,0.1,function()
            {
                $(this).html('Logged in.').addClass('messageboxok').fadeTo(900,1);
            });
            
            $.ajax({
              url: webroot+'users/getUserLogged/',
              type: 'GET',
              async: false,
              data: {language: "php", version: 5},  
              success: function(data) 
              {
                  user_name = data;
              }
            });
    
            $("#login_form").css('display', 'none');
            $('#logged_in_succes').css("display","inline");
            $("#logged_in_succes").html("Bun venit <strong>"+user_name+"</strong><br /><a href='"+webroot+"users/logout' class='bp'>Logout</a>");*/
            window.location.replace("http://www.best-event.ro"+want_return);
        }
        else
        {
            $('#want_password_masked_login').css("background","#ffffff");
            $('#want_password_masked_login').css("border-color","#b8b8b8");
        }
        setTimeout(function() { $('#msgbox_login').fadeOut('600'); }, 3000);
    });
}          
/*login end*/


window.onerror=function(desc,page,line,chr){
/* alert('JavaScript error occurred! \n'
  +'\nError description: \t'+desc
  +'\nPage address:      \t'+page
  +'\nLine number:       \t'+line
 );*/
}

$(function(){
 $('a').focus(function(){this.blur();});
 SI.Files.stylizeAll();
 slider.init();

 $('input.text-default').each(function(){
  $(this).attr('default',$(this).val());
 }).focus(function(){
  if($(this).val()==$(this).attr('default'))
   $(this).val('');
 }).blur(function(){
  if($(this).val()=='')
   $(this).val($(this).attr('default'));
 });

 $('input.text,textarea.text').focus(function(){
  $(this).addClass('textfocus');
 }).blur(function(){
  $(this).removeClass('textfocus');
 });

 var popopenobj=0,popopenaobj=null;
 $('a.popup').click(function(){
  var pid=$(this).attr('rel').split('|')[0],_os=parseInt($(this).attr('rel').split('|')[1]);
  var pobj=$('#'+pid);
  if(!pobj.length)
   return false;
  if(typeof popopenobj=='object' && popopenobj.attr('id')!=pid){
   popopenobj.hide(50);
   $(popopenaobj).parent().removeClass(popopenobj.attr('id').split('-')[1]+'-open');
   popopenobj=null;
  }
  return false;
 });
 $('p.images img').click(function(){
  var newbg=$(this).attr('src').split('bg/bg')[1].split('-thumb')[0];
  /*$(document.body).css('backgroundImage','url('+_siteRoot+'images/bg/bg'+newbg+'.jpg)');*/
 
  $(this).parent().find('img').removeClass('on');
  $(this).addClass('on');
  return false;
 });
 $(window).load(function(){
  /*$.each(css_ims,function(){(new Image()).src=_siteRoot+'uploaded/events/'+this;});
  $.each(css_cims,function(){
   var css_im=this;
   $.each(['blue','purple','pink','red','grey','green','yellow','orange'],function(){
    (new Image()).src=_siteRoot+'uploaded/events/'+this+'/'+css_im;
   });
  }); */
 }); 
 $('div.sc-large div.img:has(div.tml)').each(function(){
  $('div.tml',this).hide();
  $(this).append('<a href="#" class="tml_open">&nbsp;</a>').find('a').css({
   left:parseInt($(this).offset().left)+864,top:parseInt($(this).offset().top)+1
  }).click(function(){
   $(this).siblings('div.tml').slideToggle();
   return false;
  }).focus(function(){this.blur();}); 
 });
});
var slider={
 num:-1,
 cur:0,
 cr:[],
 al:null,
 at:5*1000,
 ar:true,
 init:function(){
  if(!slider.data || !slider.data.length)
   return false;

  var d=slider.data;
  slider.num=d.length;
  var pos=Math.floor(Math.random()*1);//slider.num);
  for(var i=0;i<slider.num;i++){
   $('#'+d[i].id).css({left:((i-pos)*1000)});
   $('#slide-nav').append('<a id="slide-link-'+i+'" href="#" onclick="slider.slide('+i+');return false;" onfocus="this.blur();">'+(i+1)+'</a>');
  }

  $('img,div#slide-controls',$('div#slide-holder')).fadeIn();
  slider.text(d[pos]);
  slider.on(pos);
  slider.cur=pos;
  window.setTimeout('slider.auto();',slider.at);
 },
 auto:function(){
  if(!slider.ar)
   return false;

  var next=slider.cur+1;
  if(next>=slider.num) next=0;
  slider.slide(next);
 },
 slide:function(pos){
  if(pos<0 || pos>=slider.num || pos==slider.cur)
   return;

  window.clearTimeout(slider.al);
  slider.al=window.setTimeout('slider.auto();',slider.at);

  var d=slider.data;
  for(var i=0;i<slider.num;i++)
   $('#'+d[i].id).stop().animate({left:((i-pos)*1000)},1000,'swing');
  
  slider.on(pos);
  slider.text(d[pos]);
  slider.cur=pos;
 },
 on:function(pos){
  $('#slide-nav a').removeClass('on');
  $('#slide-nav a#slide-link-'+pos).addClass('on');
 },
 text:function(di){
  slider.cr['a']=di.client;
  slider.cr['b']=di.desc;
  slider.ticker('#slide-client span',di.client,0,'a');
  slider.ticker('#slide-desc',di.desc,0,'b');
 },
 ticker:function(el,text,pos,unique){
  if(slider.cr[unique]!=text)
   return false;

  ctext=text.substring(0,pos)+(pos%2?'-':'_');
  $(el).html(ctext);

  if(pos==text.length)
   $(el).html(text);
  else
   window.setTimeout('slider.ticker("'+el+'","'+text+'",'+(pos+1)+',"'+unique+'");',30);
 }
};

if(!window.SI){var SI={};};
SI.Files={
 htmlClass:'SI-FILES-STYLIZED',
 fileClass:'file',
 wrapClass:'cabinet',
 
 fini:false,
 able:false,
 init:function(){
  this.fini=true;
 },
 stylize:function(elem){
  if(!this.fini){this.init();};
  if(!this.able){return;};
  
  elem.parentNode.file=elem;
  elem.parentNode.onmousemove=function(e){
   if(typeof e=='undefined') e=window.event;
   if(typeof e.pageY=='undefined' &&  typeof e.clientX=='number' && document.documentElement){
    e.pageX=e.clientX+document.documentElement.scrollLeft;
    e.pageY=e.clientY+document.documentElement.scrollTop;
   };
   var ox=oy=0;
   var elem=this;
   if(elem.offsetParent){
    ox=elem.offsetLeft;
    oy=elem.offsetTop;
    while(elem=elem.offsetParent){
     ox+=elem.offsetLeft;
     oy+=elem.offsetTop;
    };
   };
  };
 },
 stylizeAll:function(){
  if(!this.fini){this.init();};
  if(!this.able){return;};
 }
};
function fix_flash() {
    // loop through every embed tag on the site
    var embeds = document.getElementsByTagName('embed');
    for (i = 0; i < embeds.length; i++) {
        embed = embeds[i];
        var new_embed;
        // everything but Firefox & Konqueror
        if (embed.outerHTML) {
            var html = embed.outerHTML;
            // replace an existing wmode parameter
            if (html.match(/wmode\s*=\s*('|")[a-zA-Z]+('|")/i))
                new_embed = html.replace(/wmode\s*=\s*('|")window('|")/i, "wmode='transparent'");
            // add a new wmode parameter
            else
                new_embed = html.replace(/<embed\s/i, "<embed wmode='transparent' ");
            // replace the old embed object with the fixed version
            embed.insertAdjacentHTML('beforeBegin', new_embed);
            embed.parentNode.removeChild(embed);
        } else {
            // cloneNode is buggy in some versions of Safari & Opera, but works fine in FF
            new_embed = embed.cloneNode(true);
            if (!new_embed.getAttribute('wmode') || new_embed.getAttribute('wmode').toLowerCase() == 'window')
                new_embed.setAttribute('wmode', 'transparent');
            embed.parentNode.replaceChild(new_embed, embed);
        }
    }
    // loop through every object tag on the site
    var objects = document.getElementsByTagName('object');
    for (i = 0; i < objects.length; i++) {
        object = objects[i];
        var new_object;
        // object is an IE specific tag so we can use outerHTML here
        if (object.outerHTML) {
            var html = object.outerHTML;
            // replace an existing wmode parameter
            if (html.match(/<param\s+name\s*=\s*('|")wmode('|")\s+value\s*=\s*('|")[a-zA-Z]+('|")\s*\/?\>/i))
                new_object = html.replace(/<param\s+name\s*=\s*('|")wmode('|")\s+value\s*=\s*('|")window('|")\s*\/?\>/i, "<param name='wmode' value='transparent' />");
            // add a new wmode parameter
            else
                new_object = html.replace(/<\/object\>/i, "<param name='wmode' value='transparent' />\n</object>");
            // loop through each of the param tags
            var children = object.childNodes;
            for (j = 0; j < children.length; j++) {
                try {
                    if (children[j] != null) {
                        var theName = children[j].getAttribute('name');
                        if (theName != null && theName.match(/flashvars/i)) {
                            new_object = new_object.replace(/<param\s+name\s*=\s*('|")flashvars('|")\s+value\s*=\s*('|")[^'"]*('|")\s*\/?\>/i, "<param name='flashvars' value='" + children[j].getAttribute('value') + "' />");
                        }
                    }
                }
                catch (err) {
                }
            }
            // replace the old embed object with the fixed versiony
            object.insertAdjacentHTML('beforeBegin', new_object);
            object.parentNode.removeChild(object);
        }
    }
}

