var h1 = 0, h2 = 0;
var flag1 = 1, flag2 = 1;

jQuery(document).ready(function() {
	
	$('#carousel_1 > ul').bxSlider640({
		speed: 1000,
		pager: true,
		prevText: '',
		nextText: ''
	});
	
	$('#carousel_2 > ul').bxSlider640({
		speed: 1000,
		pager: true,
		prevText: '',
		nextText: ''
	});
	
	$('#carousel_3 > ul').bxSlider640({
		speed: 1000,
		pager: false,
		prevText: '',
		nextText: ''
	});
	
	$('.slider > ul').bxSlider({
		mode: 'fade',
		speed: 1000,
		pager: false,
		prevText: '',
		nextText: '',
		auto: true,
		pause: 10000
	});
	
	$('.blogs_carousel > ul').bxSlider({
		mode: 'fade',
		speed: 700,
		pager: false,
		prevText: '',
		nextText: ''
	});
	
	$('.calendar tr').each(function() {
		$(this).children('td:first').addClass('first_cell');
		$(this).children('td:last').addClass('last_cell');
	});
	
	$('span.form_button').hover(function(){
		$(this).parent().find('input').css('background-position', 'left -54px');
	});
	
	$('span.form_button').mouseleave(function(){
		$(this).parent().find('input').css('background-position', 'left 0px');
	});
	
	$('.top_menu > ul > li').each(function(i) {
		$(this).css('z-index', 10 - i);
	});
	
	$('.news_tabs').tabs();
	
	$('.comment_tabs').tabs();
	
	$('.top_menu > ul > li').hover( function() {
		if($(this).children('.sub_menu').length) {
		    if (!h1) {
		    	h1 = $(this).children('.sub_menu').height();
		    }
		    if (flag1) {
            	$(this).children('.sub_menu').css('height', '0px');
            	$(this).children('.sub_menu').css('opacity', '0');
            }
            $(this).children('.sub_menu').css('display', 'block');
			$(this).children('.sub_menu').stop().animate({height: h1 + 'px', opacity: 1}, 500);
			$(this).children('.sub_menu_open').addClass('opened');
			$(this).addClass('top_menu_opened');
		}
		if($(this).children('.sub_menu_news').length) {
		    if (!h2) {
		    	h2 = $(this).children('.sub_menu_news').height();
		    }
		    if (flag2) {
            	$(this).children('.sub_menu_news').css('height', '0px');
            	$(this).children('.sub_menu_news').css('opacity', '0');
            }
            $(this).children('.sub_menu_news').css('display', 'block');
			$(this).children('.sub_menu_news').stop().animate({height: h2,opacity: 1}, 500);
		}
	}, function() {
		if($(this).children('.sub_menu').length) {
			flag1 = 0;
			$(this).children('.sub_menu').stop().animate({opacity: 0}, { queue:false, duration:500 }).slideUp(500, function() {flag1 = 1;});
			$(this).children('.sub_menu_open').removeClass('opened');
			$(this).removeClass('top_menu_opened');
		}
		if($(this).children('.sub_menu_news').length) {
			flag2 = 0;
			$(this).children('.sub_menu_news').stop().animate({opacity: 0}, { queue:false, duration:500 }).slideUp(500, function() {flag2 = 1;});
		}
	});
	
	$('.triple_container, .half_container').each(function() {
		setEqualHeight($(this).find('.equal_height'));
	});
	
	$('.video_link a').hover( function() {
		$(this).parents('.video_link:first').find('.video_box_play').addClass('hover');
	}, function() {
		$(this).parents('.video_link:first').find('.video_box_play').removeClass('hover');
	});
	
	$('.videos li a').hover( function() {
		$(this).parents('li:first').find('.video_box_play').addClass('hover');
	}, function() {
		$(this).parents('li:first').find('.video_box_play').removeClass('hover');
	});
	
	$('.top_tools_box').hover( function() {
		$(this).stop(true,true).animate({
			opacity: 1,
			top: '0',
			cleartype: true, 
			cleartypeNoBg: true
		}, 300, function() {
			$(this).find('ul li a').css('position', 'relative');
		});
	}, function() {
		$(this).find('ul li a').css('position', 'static');
		$(this).stop(true,true).animate({
			opacity: 0.5,
			top: '-3',
			cleartype: true, 
			cleartypeNoBg: true
		}, 300, function() {
			// Animation complete.
		});
	});
	
	$('.more_box_open').click(function(e) {
		$(this).parents('.tvprogram_info:first').find('.more_box').slideDown(500);
		$(this).parents('.tvprogram_info:first').find('.read_more_a').fadeOut(500);
	});
	
	$('.open_box ul').css('left', function() {
		var leftCoord = $(this).parent().position().left - 1;
		if ($.browser.chrome) {
		   leftCoord -= 8;
		}
		return leftCoord;
	});
	
	
	$('.open_box ul.buttons').css('left', function() {
		return 0;
	});
	
	
	
	if($('input').is('.date-pick')){
		$('.date-pick').datepicker();
	}
	
	if($('div').is('.ad-gallery')) {
	
		var galleries = $('.ad-gallery').adGallery();
		
		galleries[0].addAnimation('wild',
			function(img_container, direction, desc) {
				var current_left = parseInt(img_container.css('left'), 10);
				var current_top = parseInt(img_container.css('top'), 10);
				if(direction == 'left') {
					var old_image_left = '-'+ this.image_wrapper_width +'px';
					img_container.css('left',this.image_wrapper_width +'px');
					var old_image_top = '-'+ this.image_wrapper_height +'px';
					img_container.css('top', this.image_wrapper_height +'px');
				} else {
					var old_image_left = this.image_wrapper_width +'px';
					img_container.css('left','-'+ this.image_wrapper_width +'px');
					var old_image_top = this.image_wrapper_height +'px';
					img_container.css('top', '-'+ this.image_wrapper_height +'px');
				};
				if(desc) {
					desc.css('bottom', '-'+ desc[0].offsetHeight +'px');
					desc.animate({bottom: 0}, this.settings.animation_speed * 2);
				};
				img_container.css('opacity', 0);
				return {
					old_image: {left: old_image_left, top: old_image_top, opacity: 0},
					new_image: {left: current_left, top: current_top, opacity: 1},
					easing: 'easeInBounce',
					speed: 2500
				};
			}
		);
	
	}
	
	var zIndex = 0;																									
	
	$('.special_gallery').carousel({
		carouselWidth: 1000,
		carouselHeight: 260,
		frontWidth: 326, 
		frontHeight: 241,
		top: 7,
		directionNav: true,
		reflection: false,
		reflectionHeight: 0.2, 
		reflectionOpacity: 0.3,
		backZoom: 0.75,
		hMargin: 0.6,
		speed: 1000,
		before: function(carousel){																		
			$('.slideItem.front .slide_info').fadeOut(200);
			$('.slideItem.front .video_box_play').fadeOut(200);
			$('.slideItem.front').removeClass('front');
		},
		after: function(carousel){
			$('.slideItem').each( function() {
				if(zIndex <= $(this).css('z-index')) {
					$(this).addClass('front');
					zIndex = $(this).css('z-index');
				}
				$('.slideItem.front .video_box_play').fadeIn(200);
				$('.slideItem.front .slide_info').fadeIn(200);
			});
		}
	});
	
	$('.slideItem').each( function() {
		if(zIndex <= $(this).css('z-index')) {
			$(this).addClass('front');
			zIndex = $(this).css('z-index');
		}
	});
	$('.slideItem.front .slide_info').show();
	$('.slideItem.front .video_box_play').show();
	
	if($('div').is('.scrollbox')) {
		$('.scrollbox').jScrollPane({
			showArrows: true,
			verticalDragMinHeight: 105,
			verticalDragMaxHeight: 105,
			verticalGutter: 14
		});
	}
	
	$('.reg_box_show').click(function() {
		$('.reg_container').slideDown(500);
	});
	
	$('.profile_image').click(function(){
		$('div.profileImage, #popUpBack').fadeIn('show');
	});

	//add to favorites photo
	$('#add_favorites_photo').click(function() {
		  alert('В разработке');
		  return false;
	});

	
	//comment photo
	$('#comment_photo').click(function() {
			$('#Comment_content').focus();
	});

	//print button
	$('#print_button').click(function() {
			window.print();
			return false;
	});
	
	$('.share_link').click(function(e) {
    	$('.overlay').fadeIn(300);
		$('.popup-send.share_popup').fadeIn(300);
		return false;
    });
	
	function sendSuccess(){
		alert('asdsa');
	}
	
	$('.close_popup').click(function() {
		
		$.cookie("surprize_popup", true);
		$('.popup_mg').fadeOut(1000);
    	$('.overlay').fadeOut(300);
		$('.popup-send').fadeOut(300);
    });


	/*--------------------------------------------surprize------------------------------------------------*/

	
	$('.popup_mg.surprize .next_surprize').click(function() {
		var currentPopupNumber = $(this).parents('.popup_mg:first').attr('id').slice(-1);
		var nextPopupNumber = parseInt(currentPopupNumber) + 1;
		$(this).parents('.popup_mg:first').fadeOut(1000);
		$('#s' + nextPopupNumber + '').delay(300).fadeIn(1000);
    });
	

	$('.close_surprize').click(function() {
		$.cookie("surprize_popup", true);
		$('.popup_mg').fadeOut(1000);
    	$('.overlay').fadeOut(1000);
    });
    
    $('.overlay').click(function() {
		$.cookie("surprize_popup", true);
		$('.popup_mg').fadeOut(1000);
    	$('.overlay').fadeOut(1000);
    });
	
	
	/*--------------------------------------------surprize_end--------------------------------------------*/
	

});

function setEqualHeight(columns)
 {
 var tallestcolumn = 0;
 columns.each(
 function()
	 {
		 currentHeight = $(this).height();
		 if(currentHeight > tallestcolumn)
		 {
			tallestcolumn  = currentHeight;
		 }
	 }
 );
 columns.height(tallestcolumn);
 }
 
var userAgent = navigator.userAgent.toLowerCase();
jQuery.browser = {
	version: (userAgent.match( /.+(?:rv|it|ra|ie|me)[\/: ]([\d.]+)/ ) || [])[1],
	chrome: /chrome/.test( userAgent ),
	safari: /webkit/.test( userAgent ) && !/chrome/.test( userAgent ),
	opera: /opera/.test( userAgent ),
	msie: /msie/.test( userAgent ) && !/opera/.test( userAgent ),
	mozilla: /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent )
};

