//global var
var bgConteiner = "#bg";


$(document).ready(function() {

	//run function smallWidth
	smallWidth();
	$(window).resize(function(){
		smallWidth();
	});


});//END ready


//function smallWidth 
	function smallWidth(){
		var docWidth = $(document).width();
		
		if(docWidth < 1211){
			//$('html').toggleClass('small');
			$('html').addClass('small');
			$('html').removeClass('middle');
		} else if(docWidth > 1210  && docWidth < 1530) {
			//$('html').toggleClass('middle');
			$('html').addClass('middle');
			$('html').removeClass('small');
		} else {
			$('html').removeClass('small middle');
		}
	}
//end function


//function setHeightImages
	function setHeightImages(selectorConteiner){
		var container = $(selectorConteiner);
		var img = container.children('img');
		
		//reset css
		container.attr('style', '');
		img.attr('style', '');
			
		//set css
		var queryDocument = $(document);
		var lastImg = container.children('img:last');
		
		var bodyHeight = queryDocument.height();
		var imageheight = lastImg.height();
		
		if(imageheight < bodyHeight){
			img.css({
				'width': 'auto',
				'height': bodyHeight
			});
			var marginLeft = (queryDocument.width() - lastImg.width()) / 2;
			marginLeft = (marginLeft > 0) ? 0 : marginLeft;
			container.css('margin-left', marginLeft + 'px');
		}	
	}
//end function


//Animate functions
	//function addDataNumberImg
	function addDataNumberImg(selectorConteiner){
		$(selectorConteiner).children('img').each(function(i){
			$(this).data('imgNumber', i);	
		});
	}//end function 
	
	
	function clickNextSlide(selectorConteiner, thisIndex, time){
		var currentImg = $(selectorConteiner).data('animate', true).children('img').filter(function(){ if($(this).data('imgNumber') == thisIndex) return true; else return false; });
		var currentTitle = currentImg.next();
		currentImg.insertBefore(selectorConteiner + ' > img:last');
		var newCurrentTitle = currentTitle.insertBefore(selectorConteiner + ' > img:last');
		
		rideNextSlide(selectorConteiner, time, function(){
			$(selectorConteiner).data('animate', false);	
		});
	
	}//end function 
	
	//function rideNextSlide
	function rideNextSlide(selectorConteiner, time, callbackFunction){
		var currentImg = $(selectorConteiner).children('img:last');
	  	currentImg.next().animate({top: '-125px'}, 500, function(){
			$(this).prependTo(selectorConteiner).css('top', '90px');
		});	
			
		currentImg.fadeTo(time, 0, function(){
			$(this).prependTo(selectorConteiner).css('opacity', '1');
			if (typeof callbackFunction == 'function') callbackFunction();	
		});
	}//end function 
	
	//function fadeNextSlide
	function fadeNextSlide(selectorConteiner, time, callbackFunction){
		$(selectorConteiner).children('img:last').fadeOut(time, function(){
			/*$(this).prependTo(selectorConteiner).show();*/
			$(this).remove();
			if (typeof callbackFunction == 'function') callbackFunction();	
		});
	}//end function 
//END Animate functions	


//Cookie functions
	function set_cookie ( name, value, expires_year, expires_month, expires_day, path, domain, secure )
	{
	  var cookie_string = name + "=" + escape ( value );
	
	  if ( expires_year )
	  {
	    var expires = new Date ( expires_year, expires_month, expires_day );
	    cookie_string += "; expires=" + expires.toGMTString();
	  }
	
	  if ( path )
		cookie_string += "; path=" + escape ( path );
	
	  if ( domain )
		cookie_string += "; domain=" + escape ( domain );
	
	  if ( secure )
		cookie_string += "; secure";
	
	  document.cookie = cookie_string;
	}
	function delete_cookie ( cookie_name )
	{
	  var cookie_date = new Date ( );  //Òêóùàÿ äàòà è âðåìÿ
	  cookie_date.setTime ( cookie_date.getTime() - 1 );
	  document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
	}
	function get_cookie ( cookie_name )
	{
	  var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );
	
	  if ( results )
	    return ( unescape ( results[2] ) );
	  else
	    return null;
	}
//end cookie functions
