var slider_animate_speed = 500;

var slider_animate_easing  = 'easeInOutCirc';

var scrollTo_speed = 500;

var before_scroll_speed = 1000;


var slider_animate = {
	'slider' : {
		'stallions'   : {left: 60},
		'mares'       : {left: 175},
		'in-training' : {left: 289},
		'youngstock'  : {left: 402},
		'partbredteke'	  : {left: 517},
		'forsale'     : {left: 631}
	},
	'slidertext' : {
		'stallions'   : {left: -63},
		'mares'       : {left: 53},
		'in-training' : {left: 164},
		'youngstock'  : {left: 282},
		'partbredteke'   : {left: 406},
		'forsale'     : {left: 511}
	}
}
///////////////////////////////////////////////

var width = screen.width;

var css_file = '1024';
if(width > 0) {
	if(width >= 1024 && width < 1280) {
		css_file = '1024';
	} else if(width >= 1280 && width < 1440) {
		css_file = '1280';
	} else if(width >= 1440 && width < 1680) {
		css_file = '1440';
	} else if(width >= 1680) {
		css_file = '1680';
	}
}

var SLIDER_IMAGES_LOADED = false;

$(document).ready(function() {
	
	$.preload( [
		'ninja_hover.jpg',
		'slider_top.png',
		'stallions-arrow.png',
		'frame_hover.png',
		'frame.png',
		'slider.png'
	], {base:'/templates/www/i/'});





	$(document).pngFix({blankgif: '/images/blank.gif'});

	/**
	 *SLIDER
	 */

	$('#stallions-list .horse').live('click', function() {
		$.cookie('scrollTop', $(window).scrollTop(), {path: '/'});
	});


	$('#slider-container ul li').click(function(event, options) {

		var settings = {
			only_slider  : false,
			no_scroll    : false
		}

		$.extend(settings, options);


		if($("#slider,#slider-text,#section-photo-container .section-photo").is(':animated')) {
			return;
		}

		$.cookie('section', $(this).attr('class'), {path: '/'});

		$('#stallions-list').addClass($(this).attr('class'));

		SLIDER_animate($(this), settings);
	});



	$('#slider-container li').live('mouseenter mouseleave', function(event){
		if(event.type == 'mouseenter') {
			var left = $(this).position().left-7-25;
			$('#slider-tooltip').css('left', left).text($(this).attr('caption')).show();
		} else {
			$('#slider-tooltip').empty().hide();
		}
	});
	/**
	 *SLIDER
	 */



	$('#ninja,#slider-photo-nav .arrow,#gall-sections li,#salyr').live('mouseenter mouseleave', function(event){
		if(event.type == 'mouseenter')
			$(this).addClass('hover');
		else
			$(this).removeClass('hover');
	});

	$('img.hover').live('mouseenter mouseleave', function(event){
		if(event.type == 'mouseenter')
			$(this).attr('src', $(this).attr('src').replace('.jpg', '_hover.jpg'))
		else
			$(this).attr('src', $(this).attr('src').replace('_hover.jpg', '.jpg'))
	});

	$('#menu-gallery:not(.noanimate)').live('mouseenter mouseleave', function(event){
		$(this).stop();
		if(event.type == 'mouseenter')
			$(this).animate({top:5}, 300);
		else
			$(this).animate({top:-70}, 300);
	});




	/**
	 * FEEDBACK
	 */
	$("input[name='name'],input[name='email'],textarea[name='text']").keypress(function(){$(this).removeClass('error')});

	$("#fb-send").click(function(){
		var name  = $("input[name='name']").removeClass('error').val();
		var email = $("input[name='email']").removeClass('error').val();
		var text  = $("textarea[name='text']").removeClass('error').val();

		//показываем картинку
		//$("img.ajax").show();
		$.post("/feedback.php", {'name':name,'email':email,'text':text}, function(data){
			switch(data)
			{
				case 'error:name':
					$("input[name='name']").addClass('error');
					break;
				case 'error:email':
					$("input[name='email']").addClass('error');
					break;
				case 'error:text':
					$("textarea[name='text']").addClass('error');
					break;
				case 'OK':
					$("p.result").show();
					setTimeout(function(){
						$("p.result").hide();
						$("#feedback-form").trigger('reset');
					}, 2000);
					break;
			}

			//$("img.ajax").hide();
		})
	});

	

});

function SLIDER_animate(LI, settings) {
	$('#slider').show().attr('class', LI.attr('class')).animate(
		slider_animate.slider[LI.attr('class')],
	slider_animate_speed, slider_animate_easing, function(){});

	$('#slider-text').show().attr('class', LI.attr('class')).animate(
		slider_animate.slidertext[LI.attr('class')],
	slider_animate_speed, slider_animate_easing, function(){});


	if(!settings.only_slider) {

		$('#slider-text').empty().append(
			$('<a>').attr('href', LI.attr('href')).text(LI.attr('caption')).bind('click', function(){
				$(window).scrollTo('.horse:eq(0)', {offset:-10, duration:scrollTo_speed});
				return false;
			})
		);

	
		SLIDER_load_section(LI, settings.no_scroll);
	

	} else {
		$('#slider-text').text(LI.attr('caption'));
	}
}

function SLIDER_load_section(LI, no_scroll) {


	//показываем фотку раздела
	SLIDER_show_section_photo(LI, function(LI){});

	//показываем стрелочки
	SLIDER_show_arrows(LI);


	$('#stallions-list').empty();
	$('#stallions-loading').show();

	//грузим на аяксе раздел
	var langCode = LANG == 'ru' ? '' : LANG+'/';

	var get_url = '/'+langCode+LI.attr('class')+'/';

setTimeout(function(get_url) {

	$.get(get_url, function(data) {
		
		$('#stallions-loading').hide();

		$('#stallions-list').html(data);


		if(!no_scroll) {
			setTimeout(function(){
				$(window).scrollTo('.horse:eq(0)', {offset:-10, duration:scrollTo_speed});
			}, before_scroll_speed)
		}

		//скроллим до старого места
		if($.cookie('scrollTop') > 0) {
//			$(window).scrollTo($.cookie('scrollTop'), {duration:500});
//			$.cookie('scrollTop', 0);
		}


	});

}, 1000, get_url);


}

function SLIDER_show_section_photo(LI, callback) {
	$('#section-photo-container .section-photo').animate({
		left      : '-'+LI.attr('offset')+'px'
	}, slider_animate_speed, '', function() {
	});

}


function SLIDER_show_section_photo3(LI, callback) {//return;


	var element = $('#section-photo-container .photo.'+LI.attr('class'));

	var container = $('#section-photo-container');

	var photo_container = $('#section-photo-container .section-photo');


	var left_offset = Math.abs(photo_container.position().left);

	//photo_container.position().left

	var left_offset = (element.position().left - left_offset);


	

	console.log(left_offset)

	


	$('#section-photo-container .section-photo').animate({
		left      : '+=-'+left_offset+'px'
	}, 500, '', function() {
	});


	console.log(photo_container.position().left)

}

function SLIDER_show_section_photo2(LI, callback) {
	$('#section-photo-container .section-photo').fadeTo(section_photo_hide_speed, 0, function(){
		$('#section-photo-container .section-photo').attr('class', LI.attr('class')).fadeTo(section_photo_show_speed, 1, function(){
			if(typeof callback == 'function') {
				callback(LI);
			}
		});
	});
}

function SLIDER_show_arrows(LI) {
	var BACK_LI = LI.prev('li');
	var NEXT_LI = LI.next('li');


	$('#slider-photo-nav .arrow').unbind('click');

	if(BACK_LI.length > 0) {
		$('#slider-photo-nav .arrow.back').click(function(){BACK_LI.trigger('click')}).show().find('span').text(BACK_LI.attr('caption'))
	} else
		$('#slider-photo-nav .arrow.back').hide();


	if(NEXT_LI.length > 0) {
		$('#slider-photo-nav .arrow.next').click(function(){NEXT_LI.trigger('click')}).show().find('span').text(NEXT_LI.attr('caption'))
	} else
		$('#slider-photo-nav .arrow.next').hide();

}
