function add_css_class(selector, class_name) {
    var elements = cssQuery(selector);
    for(var e = 0; e < elements.length; e++) {
        elements[e].className += " " + class_name;
    }
}
function init() {
    add_css_class("h1.h", "f");
    add_css_class("h2.h", "f");
    add_css_class("fieldset h2", "f");
    add_css_class("ul.people li:first-child", "first-child");
    add_css_class("ul.posts li:first-child", "first-child");
    add_css_class("ul.posts li ul.details li:first-child", "first-child");
    add_css_class("ul.navigation-months li:first-child", "first-child");
    add_css_class("ul.utilities li:first-child", "first-child");
    add_css_class("ul.links li:first-child", "first-child");
    add_css_class("fieldset#applicationForm", "offset");
    add_css_class("ul.navigation li:nth-child(1)", "i1");
    add_css_class("ul.navigation li:nth-child(2)", "i2");
    add_css_class("ul.navigation li:nth-child(3)", "i3");
    add_css_class("ul.navigation li:nth-child(4)", "i4");
    add_css_class("ul.navigation li:nth-child(5)", "i5");
    add_css_class("ul.navigation li:nth-child(6)", "i6");
    
    if(document.getElementById("applyTrigger")) {
        var trigger = document.getElementById("applyTrigger");
        trigger.setAttribute("onclick", "toggle(this); return false;");
        trigger.onclick = function() {
            toggle(this);
            return false;
        }
    }
    if(document.getElementById("calendarTrigger")) {
       var cal = document.getElementById("calendarTrigger");
       if(/offset/.test(cal.className)) {
            cal.className = cal.className.replace("offset", "");
            document.getElementById("inline").className += " offset";
       }
    }
   if(document.getElementById("wood-game-users")) { 
       $("#game-user-list").css("width",(($("#game-user-list li").length*40)-40)+"px")
    document.getElementById("game-scroll").scrollLeft = document.getElementById("game-user-list").offsetWidth;
   } 
}

function showCalendar() {
    var tempCal = document.getElementById("calendarTrigger");
    var cal = document.getElementById("inline");
       if(/offset/.test(cal.className)) {
            cal.className = cal.className.replace("offset", "");
            tempCal.className += " offset";
       }
}

function toggle(o) {
    if(document.getElementById("applicationForm")) {
        o.className += " offset";
        var people = document.getElementById("people");
        people.style.width = people.offsetWidth + 240 + "px";
        add_css_class("ul.people li:first-child", "extended");
        document.getElementById("scroller").scrollLeft = document.getElementById("people").offsetWidth;
        var el = document.getElementById("applicationForm");

        if(/offset/.test(el.className)) {
            el.className = el.className.replace("offset", "");
        } else {
            el.className += " offset";
        }
    }
}
function closeMessage() {
    if(document.getElementById("success")) {
        document.getElementById("success").style.display= "none";
        add_css_class("ul.people li:first-child", "offset");
        var people = document.getElementById("people");
        //people.style.width = people.offsetWidth - 470 + "px";
        people.style.width = people.offsetWidth - 160 + "px"; 
        $("#success").html("");
    }
}
function closeFieldset() {
    if(document.getElementById("applicationForm")) {
        document.getElementById("applicationForm").className += " offset";
        document.getElementById("applyTrigger").className = document.getElementById("applyTrigger").className.replace("offset", ""); 
        $("li.extended").removeClass("extended");
        document.getElementById("people").style.width = document.getElementById("people").offsetWidth - 240 + "px";
    }
}

function calculateTrees() {
    if(document.getElementById("calculator")) {
        var pages = document.getElementById("a4");
        var period = document.getElementById("period_s");
        
        var results = document.getElementById("saved-trees");
        var trees = roundNumber(pages.value * period.options[period.selectedIndex].value * 0.00012, 2);
        results.innerHTML = treeNumberResult_1+" <span>" + trees + " "+treeNumberResult_2+"</span>";
        results.style.display = "block";
    }
}

function roundNumber(rnum, rlength) {
    var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
    return newnumber;
}

 $(document).ready(function () {
    init(); 
});  

function testAjax () {
    $("#msg").text("Зареждане..."); 
    $("#msg").css("display","block");
  
    postData="";
         
         postData = "email="+$("#email").val(); 
         postData = postData+"&FirstName="+initialCap($("#FirstName").val()); 
         postData = postData+"&LastName="+initialCap($("#LastName").val()); 
         postData = postData+"&recycle="+$("#recycleType input:checked").val(); 
         postData = postData+"&location="+initialCap($("#location").val());  
         postData = postData+"&ages="+$("#ages").val();
         postData = postData+"&gender="+$("#genderSelector input:checked").val();  
         postData = postData+"&RegisterAccount=1";   

   // alert (postData);  
  $.ajax({
   type: "POST",
   url: "http://globulgreen.bg/user_actions.php",
   data: postData,
   success: function(msg){
     
   $("#msg").html(msg);
    // alert( "Data Saved: " + msg );
     message= msg.split("|") 

    if (message[0]==0) { 
        $("#success").css("display","block");
        $("li.extended").removeClass("extended");
        document.getElementById("people").style.width = document.getElementById("people").offsetWidth - 240 + "px";
        
        massagaall = "<h2 class='f'>Ти си един от нас!</h2>";
        massagaall = massagaall+"<p>Благодарим ти, че се присъедини към нашата инициатива!</p>";
        massagaall = massagaall+"<p>На указаната електронна поща, ти изпратихме препратка за потвърждаване на твоето участие.</p>";
        massagaall = massagaall+"<p class='button'><a href='#' class='close' onclick='closeMessage(); return false;';>Затвори</a></p>";
        $("#success").html(massagaall); 
         
        sIFR.replace(spfuturNormal, {
        selector: "div.success h2", 
        css: {".sIFR-root":{ "color": "#ffffff", "font-size": "13px", "font-weight": "normal", "letter-spacing": "2", "text-align": "left", "text-transform": "uppercase"}}, 
        wmode: "transparent"
        });
        $("#applicationForm").css("display","none") ; 
        
    }  else {
        $("#error-msg").css("display","block");
        $("#error-msg").html(message[1]);
    } 

   }
 });
}
function initialCap(field) {
   return field.substr(0, 1).toUpperCase() + field.substr(1).toLowerCase(); 
}
function validateUserData () { 
  rsv.displayType = "display-html";
  rsv.errorFieldClass = "error";
  rsv.errorTextIntro = "Моля попълнете задължителните полета и изпратете формата отново!"; 
// add JS validation
var rules = [];

// Attendee / work information
 

rules.push("required,FirstName,Моля въведете вашето име");
rules.push("reg_exp,FirstName,^[а-я]+$,i,Моля въведете вашето име на кирилица ");   
rules.push("length=2-15,FirstName,Дължината на вашето име може да бъде между 5 и 15 символа"); 
 
rules.push("required,LastName,Моля въведете вашата фамилия");
rules.push("reg_exp,LastName,^[а-я]+$,i,Моля въведете вашата фамилия на кирилица ");     
rules.push("length=2-15,LastName,Дължината на вашата фамилия може да бъде между 5 и 15 символа"); 
 
rules.push("required,location, Моля посочете вашето местожителство");
rules.push("reg_exp,location,^[а-яА-Я ]+$,i,Моля, въведете вашето местожителство на кирилица");

rules.push("required,email,Моля въведете електронен адрес"); 
//rules.push("reg_exp,email,^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$,i,Моля въведете коректен електронен адрес");
rules.push("valid_email,email,Моля въведете коректен електронен адрес");

rules.push("required,ages, Моля въведете вашата възраст");
rules.push("digits_only,ages, Моля въведете цифри");
rules.push("range=4-90,ages, Можете да участвате ако сте на възраст между 4 и 90 години");

rules.push("required,gender, Моля въведете вашия пол");    

rules.push("required,recycle, Моля въведете какво ще върнете за рециклиране"); 



//rules.push("required,Subject,Subject");
//rules.push("required,Message,<?=ESTATE_CONTACT_FORM_ERROR_MSG2?> "); 

if (rsv.validate( document.getElementById("userDataForm"), rules)) {
    $("#error-msg").css("display","none");
    $("#success-msg").css("display","none") ; 
    $("#sendButton").attr("disabled","disabled");
     testAjax();
} else {
    $("#success-msg").css("display","none") ;
    $("#error-msg").css("display","block"); 
}
}

function redirectUrl (Url, id) {
   window.location = Url+"/frog_details/?id="+id+"";
}

function initDeleteLink () {
    $("a.delete").click(function () {
    
      deleteConfirm (this);
       return false 
    });
}

function getDataAjax (date) {
    postData="";
         
         postData = "GetPlayers=1";
         postData = postData+"&datevalue="+date; 
  

   // alert (postData);  
  $.ajax({
   type: "POST",
   url: "http://globulgreen.bg/game_actions.php",
   data: postData,
   success: function(msg){
     
   $("#scroller").html(msg);
   add_css_class("ul.people li:first-child", "first-child");
   add_css_class("fieldset#applicationForm", "offset");
   
       if(document.getElementById("scroller")) {
        document.getElementById("scroller").scrollLeft = document.getElementById("people").offsetWidth;
    }
    
    
    if(document.getElementById("applyTrigger")) {
        var trigger = document.getElementById("applyTrigger");
        trigger.setAttribute("onclick", "toggle(this); return false;");
        trigger.onclick = function() {
            toggle(this);
            return false;
        }
    }
    
    add_css_class("fieldset.apply h2", "f");
    
    sIFR.replace(spfuturNormal, {
    selector: "fieldset.apply h2", 
    css: {".sIFR-root":{ "color": "#ffffff", "font-size": "13px", "font-weight": "normal", "letter-spacing": "2", "text-align": "left", "text-transform": "uppercase"}}, 
    wmode: "transparent"
    });
   }
 });
}

function validateSendForm () { 
  rsv.displayType = "display-html";
  rsv.errorFieldClass = "error";
  rsv.errorTextIntro = "Моля попълнете задължителните полета и изпратете формата отново!"; 
// add JS validation
var rules = [];

// Attendee / work information
 
rules.push("required,YourName, Моля въведете вашето име");
rules.push("reg_exp,YourName,^[а-яА-Я ]+$,i,Моля въведете вашето име на кирилица");
rules.push("required,FriendMail,Моля въведете електронен адрес"); 
rules.push("valid_email,FriendMail,Моля въведете коректен електронен адрес");



//rules.push("required,Subject,Subject");
//rules.push("required,Message,<?=ESTATE_CONTACT_FORM_ERROR_MSG2?> "); 

if (rsv.validate( document.getElementById("SendMailForm"), rules)) {
    $("#send-error-msg").css("display","none");
    $("#send-success-msg").css("display","none");
    $("#sendMailButton").attr("disabled","disabled");
     sendaMailAjax();
} else {
   $("#send-success-msg").css("display","none");
   $("#send-error-msg").css("display","block");
   //$("#send-error-msg").html("Моля въведете вярна информация, както е указано в примерите на подчертаните полета"); 
    
}
}
function sendaMailAjax () {
    postData="";
         
         postData = "FriendMail="+$("#FriendMail").val();
         nameString = $("#YourName").val();
         nameArray = nameString.split(" ");
         if (nameArray[1]) {
            firstName= initialCap(nameArray[0]); 
            lastName= initialCap(nameArray[1]);  
            YourName= firstName+" "+lastName;
         } else {
            YourName= initialCap(nameArray[0]); 
         }
         postData = postData+"&YourName="+YourName;
         postData = postData+"&SendMail=1";   

   // alert (postData);  
  $.ajax({
   type: "POST",
   url: "http://globulgreen.bg/user_actions.php",
   data: postData,
   success: function(msg){
    // alert( "Data Saved: " + msg );
     message= msg.split("|") 

    if (message[0]==0) { 
        $("#send-success-msg").css("display","block"); 
        $("#send-success-msg").html(message[1]);
        $("#sendMailButton").removeAttr("disabled");
        $("#FriendMail").val("")  
        $("#YourName").val("") 

    }  else {
       $("#send-error-msg").css("display","block");
       $("#send-error-msg").html(message[1]);
    } 

   }
 });
}
var bannerID = 1;     
function rotateBanner() {
    $("#banner").attr("src", "http://www.globulgreen.bg/wp-content/themes/green/i/content/0"+bannerID+".jpg");
    if(bannerID < 4) {
        bannerID++;
    } else {
        bannerID = 1;
    }
    $("#banner").animate({ opacity: 1}, 5000, function() {rotateBanner();});
}
rotateBanner();




function MM_openBrWindow(theURL,winName,features) { //v2.0
  remote = window.open(theURL,winName,features);
  if (remote.opener == null) remote.opener = window;
    remote.opener.name = "opener";
}
