$(document).ready(function() {
     $('#bg_image_wrapper').each( function (){
         $('body').css("background",$(this).attr("rel"));
         
     }) 
	$('#login_btn').click(function() {
		 $('.loginBox').show();
		  return false;
	      });
		
		 $('.close').click(function() {
		 $('.loginBox').hide();
		  return false;
	      });
	  
	 $('#loginSubmit').click(function() {
	//	alert('Handler for .submit() called.');
  		$('.loginBox form').submit();
	});
	
	  $('.loginBox input').bind('keypress', function(e) {
     if(e.keyCode==13){
  		$('.loginBox form').submit();
		return false;
       }
	});

			
	 $('.showAll').click(function() {
		 $('.limitHeight').removeClass().slideDown("slow");
		 $('.showAll').hide();
		 return false;
	  });	
	
	$(".menu ul li").filter(function() {
      var currentURL = window.location.toString().split("/");
      return $(this).children("a").attr("href") == currentURL[currentURL.length-1];
    }).addClass("active");
			
            
});


// COUNTDOWN

function countdown_clock(year, month, day, hour, minute, format)
         {
         //I chose a div as the container for the timer, but
         //it can be an input tag inside a form, or anything
         //who's displayed content can be changed through
         //client-side scripting.
         html_code = '<em id="countdown"></em>';
         
         document.write(html_code);
         
         countdown(year, month, day, hour, minute, format);                
         }
         
function countdown(year, month, day, hour, minute, format)
         {
         Today = new Date();
         Todays_Year = Today.getFullYear() - 2000;
         Todays_Month = Today.getMonth() + 1;                  
         
         //Convert both today's date and the target date into miliseconds.                           
         Todays_Date = (new Date(Todays_Year, Todays_Month, Today.getDate(), 
                                 Today.getHours(), Today.getMinutes(), Today.getSeconds())).getTime();                                 
         Target_Date = (new Date(year, month, day, hour, minute, 00)).getTime();                  
         
         //Find their difference, and convert that into seconds.                  
         Time_Left = Math.round((Target_Date - Todays_Date) / 1000);
         
         if(Time_Left < 0)
            Time_Left = 0;
         
         switch(format)
               {
               case 0:
                    //The simplest way to display the time left.
                    document.all.countdown.innerHTML = Time_Left + ' seconds';
                    break;
               case 1:
                    //More datailed.
                    days = Math.floor(Time_Left / (60 * 60 * 24));
                    Time_Left %= (60 * 60 * 24);
                    hours = Math.floor(Time_Left / (60 * 60));
                    Time_Left %= (60 * 60);
                    minutes = Math.floor(Time_Left / 60);
                    Time_Left %= 60;
                    seconds = Time_Left;
                    
                    dps = 's'; hps = 's'; mps = 's'; sps = 's';
                    //ps is short for plural suffix.
                    if(days == 1) dps ='';
                    if(hours == 1) hps ='';
                    if(minutes == 1) mps ='';
                    if(seconds == 1) sps ='';
                    
                    document.getElementById('countdown').innerHTML = '<span>'+days + '</span>дни ' + ' ';
                    document.getElementById('countdown').innerHTML +='<span>'+hours + '</span>часа ' + ' ';
                    document.getElementById('countdown').innerHTML +='<span>'+minutes + '</span>мин.' + ' и ';
                    document.getElementById('countdown').innerHTML +='<span>'+seconds + '</span>сек.';
                    break;
               default: 
                    document.getElementById('countdown').innerHTML = Time_Left + ' seconds';
               }
               
         //Recursive call, keeps the clock ticking.
         setTimeout('countdown(' + year + ',' + month + ',' + day + ',' + hour + ',' + minute + ',' + format + ');', 1000);
         }	
         
         
// NEW JQUERY AJAX TABS

function initTabs(Tabs,id, selectedIndex){
	var z=0;
	$.each(Tabs,function(i,j){

		/* Sequentially creating the tabs and assigning a color from the array: */
		var tmp = $('<li class="tab'+id+'"><a href="#" title="'+j[1]+'"><span>'+i+'</span></a></li>');
		
		/* Setting the page data for each hyperlink: */
		tmp.find('a').data('page',j[0]);
		
		/* Adding the tab to the UL container: */
		$('ul.'+id).append(tmp);
	})

	/* Caching the tabs into a variable for better performance: */
	var the_tabs = $('.tab'+id+' a');
	
	the_tabs.click(function(e){
	/* "this" points to the clicked tab hyperlink: */
		var element = $(this);
		
		$('li.tab'+id).each(function(){
			$(this).removeClass('ntabactive');
		});
		$(element).parent().addClass('ntabactive');
		
		/* Checking whether the AJAX fetched page has been cached: */
		if(!element.data('cache'))
		{	
			/* If no cache is present, show the gif preloader and run an AJAX request: */
			$('#'+id).html('<img src="../images/v4/ajax-loader.gif" class="preloader" />');

			$.get(element.data('page'),function(msg){
				$('#'+id).html(msg);
				
				/* After page was received, add it to the cache for the current hyperlink: */
				element.data('cache',msg);
			});
		}
		else $('#'+id).html(element.data('cache'));
		
		e.preventDefault();
	})
	
	if(!selectedIndex) {
		selectedIndex=0;
	}
	
	/* Emulating a click on the first tab so that the content area is not empty: */
	the_tabs.eq(selectedIndex).click();
}
         	
		
