var currentItem = 1;
function carouselRotate(){
    $('#item'+currentItem).fadeOut(200);
    $("#itemLink"+currentItem).removeClass('itemSelected');
    if(currentItem == numItems){
        currentItem = 0;
    }
    var nextItem = currentItem + 1;    
    $('#item'+nextItem).fadeIn(300);
    $("#itemLink"+nextItem).addClass('itemSelected');
    currentItem++;

}
function clickButton(i){
    if(i == currentItem){
        return;
    }
    $('#item'+currentItem).fadeOut(200);
    $('#item'+i).fadeIn(300);

    $("#itemLink"+currentItem).removeClass('itemSelected');
    $("#itemLink"+i).addClass('itemSelected');
    currentItem = i;
}
function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x==c_name)
    {
    return unescape(y);
    }
  }
}

$(document).ready(function(){
    if(getCookie('legal') == 1){
        $(".legalBlack").hide();
    }
    $(".yesLink").click(function(){
        $(".legalBlack").hide();
        document.cookie = 'legal=1; path=/ ;';

    });
    $(".noLink").click(function(){
        $(".legal").hide();
        $(".legalNo").show();

    });
    
    //	comment next lines to cancel the random background function and use a custom one   
    
    var randomnumber=Math.ceil(Math.random()*4);
    
    switch(randomnumber){
        case 1:
            $("body").css('background', 'url("/wp-content/themes/gib/images/gib-bgs-blue.jpg") no-repeat center top');
            $(".wrapper").css('background', 'url("/wp-content/themes/gib/images/gib-bgs-v2-blue-live.jpg") no-repeat center top #ede9e0');
            $("body").css('background-color', '#30566d');
            break;
        case 2:
            $("body").css('background', 'url("/wp-content/themes/gib/images/gib-bgs-green.jpg") no-repeat center top');
            $(".wrapper").css('background', 'url("/wp-content/themes/gib/images/gib-bgs-v2-green-live.jpg") no-repeat center top #ede9e0');
            $("body").css('background-color', '#3b5b32');
            break;
        case 3:
            $('body').css('background', 'url("/wp-content/themes/gib/images/gib-bgs-orange.jpg") no-repeat center top');
            $(".wrapper").css('background', 'url("/wp-content/themes/gib/images/gib-bgs-v2-orange-live.jpg") no-repeat center top #ede9e0');
            $("body").css('background-color', '#a96710');
            break;
        case 4:
            $("body").css('background', 'url("/wp-content/themes/gib/images/gib-bgs-red.jpg") no-repeat center top');
            $(".wrapper").css('background', 'url("/wp-content/themes/gib/images/gib-bgs-v2-red-live.jpg") no-repeat center top #ede9e0');
            $("body").css('background-color', '#92191e');
            break;
    } 
    
    // Navigation section
    $('<ul class="shield"></ul>').insertBefore('ul.children');
    $('li.current_page_item').find("a").addClass('selectedNav');
    $('li.current_page_parent').find("a").addClass('selectedNav');
    $("ul.topnav li ").mouseenter(function(){
        $(this).find("ul.shield").show();
        $(this).find("ul.children").show();
        $(this).find("a").addClass('selectedNav');
        var backgroundPositionX = parseInt($(this).find("a").css('background-position').split(' ')[0]);
        $(this).find("ul.children").css('background-position', -backgroundPositionX+'px -147px');
        //alert(backgroundPositionX);
    });
    $("ul.topnav li ").mouseleave(function(){
      $(this).find("ul.children").hide();
      if(!($(this).hasClass('current_page_item') || $(this).hasClass('current_page_parent'))){
          $(this).find("a").removeClass('selectedNav');
      }
      
      $(this).find("ul.shield").hide();
    });
    // Nav section ends here.
    // facebook section starts here.
    
    // facebook seciton ends here.
    if($("#items-wrapper ").length){
        var num_items = $("#items-wrapper > div").size();
        var total_width = num_items * 550;
        var current = 1;
        $("#items-wrapper").width(total_width+"px");
        $("#go-left").hover(function(){
            $(this).css("background-position", "0px -45px");
        }, function(){
            $(this).css("background-position", "0px 0px");
        });
        $("#go-right").hover(function(){
            $(this).css("background-position", "-50px -45px");
        }, function(){
            $(this).css("background-position", "-50px 0px");
        });
         $("#go-left").click(function(){
            if(current != 1){
                $("#items-wrapper").animate({
                    left: '+=550'
                },1000, function(){});
                current -=1;
                $("#go-right").css("display","block");
            }
            if(current == 1){
                $("#go-left").css("display","none");
            }

        });
        $("#go-right").click(function(){
            if(current != num_items){
                $("#items-wrapper").animate({
                    left: '-=550'
                },1000, function(){});
                current += 1;
                $("#go-left").css("display","block");
            }

            if(current == num_items){
                $("#go-right").css("display","none");
            }
        });
    }
   
    $('input.widefat').focus(function(){
        if($(this).val() == 'Name' || $(this).val() == 'Email'){
            $(this).val('');
        }
        
    });
    $('#cm-ajax-email').focusout(function(){
        if($(this).val() == ''){
            $(this).val('Email');
        }
    });
    $('#cm-ajax-name').focusout(function(){
        if($(this).val() == ''){
            $(this).val('Name');
        }
    });
    
});

