$(document).ready(function() {
	/* Main menu */
	$('.f-mm-l-li').hover(
		function() {
			$(this).find('.f-mm-l-li-submenu').css('display', 'block');
		},
		function() {
			$(this).find('.f-mm-l-li-submenu').css('display', 'none');
		}
	);
	// Callback form text toggling
	$('.f-info-cb-input-toggleable ').each( function(index, element) {
		txtToggle( $(this), $(this).val() );
	} );
	
	// --- --- --- --- ---
	// Content page openers handlers
	$('.content-page-opener').live('click', function(event) {
	
		event.preventDefault();
		
		var menu_block_class = '';
		var curr_href = $(this).attr('href');
		var curr_href_base = curr_href + '';
		var index_of_question = curr_href_base.indexOf('?');
		if ( index_of_question >= 0 ) {
			curr_href_base = curr_href_base.substr(0, index_of_question);
		}
		document.news_selected = '';
		
		if ( $(this).hasClass('content-page-opener-company') ) {
			menu_block_class = 'company';
		} else if ( $(this).hasClass('content-page-opener-production') ) {
			menu_block_class = 'production';
		} else if ( $(this).hasClass('content-page-opener-logistic') ) {
			menu_block_class = 'logistic';
		} else if ( $(this).hasClass('content-page-opener-buy') ) {
			menu_block_class = 'buy';
		} else if ( $(this).hasClass('content-page-opener-factory') ) {
			menu_block_class = 'factory';
		} else if ( $(this).hasClass('content-page-opener-pack') ) {
			menu_block_class = 'pack';
		} else if ( $(this).hasClass('content-page-opener-career') ) {
			menu_block_class = 'career';
		} else if ( $(this).hasClass('content-page-opener-press') ) {
			menu_block_class = 'press';
			if ( $(this).hasClass('f-info-news-txt-item') ) {
				curr_href = 'press/news/';
				curr_href_base = 'press/news/';
				document.news_selected = $(this).attr('news_id');
			}
		}
		
		// Hide all menues
		$('.cb-page-menu-wrp').css('display', 'none');
		// And show new one
		$('.cb-page-menu-wrp-' + menu_block_class).css('display', 'block');
		
		// Highlight links
		$('.content-page-opener').removeClass('act');
		var content_page_opener_act = $('.content-page-opener-' + menu_block_class + '[href*="' + curr_href_base + '"]');
		$(content_page_opener_act).addClass('act');
		
		// Additional highlighting
		$('.cb-prod-menu-wrp').removeClass('cb-prod-menu-wrp-act');
		$(content_page_opener_act).parents('.cb-prod-menu-wrp').addClass('cb-prod-menu-wrp-act');
		
		// Get content
		//$('.cb-page-content').html('');
		window.api.getContentPane().html('');
		getPage( curr_href );
		
		// Open the window
		openPageContent();
		
	});
	// --- --- --- --- ---
	
	// Page content closing
	$('.cb-page-content-close').click( function() {
		closePageContent();
	});
	
	// News open/close
	$('.cb-page-cont-news-item-date, .cb-page-cont-news-item-announce').live('click', function() {
		$(this).parents('.cb-page-cont-news-item').find('.cb-page-cont-news-item-descr').toggle();
		$(this).parents('.cb-page-cont-news-item').find('.cb-page-cont-news-item-date').toggleClass('cb-page-cont-news-item-date-opened');
		window.api.reinitialise();
	});
	
	// production menu open/close
	$('.cb-prod-menu-hdr').live('click', function() {
		$(this).parents('.cb-prod-menu-wrp').toggleClass('cb-prod-menu-wrp-act');
	});
	
	// Online order
	$('.f-info-cb-form').submit( function(event) {
		event.preventDefault();
		var com = $(this).find("[name='com']").val();
		var man = $(this).find("[name='man']").val();
		var mail = $(this).find("[name='mail']").val();
		var phone = $(this).find("[name='phone']").val();
		var text = $(this).find("[name='text']").val();
		
		$.ajax({
			url: $(this).attr('action'),
			type: "post",
			data: 'com=' + com + '&man=' + man + '&mail=' + mail + '&phone=' + phone + '&text=' + text,
			dataType: "html",
			success: function( data ) {
				if ( data.substr(0, 1) == '1' ) {
					$('.f-info-callback').html('<img class="f-info-callback-success" src="images/message_sent_successfull.png" alt="Ваше сообщение отправлено"/>');
				}
			}
		});
	});
	
	//Scrollbar
	var api = $('.cb-page-content').jScrollPane({
		maintainPosition: false,
		verticalDragMinHeight: 50,
		verticalDragMaxHeight: 50,
		autoReinitialise: true,
		autoReinitialiseDelay: 2000,
		maintainPosition: true
	}).data('jsp');
	window.api = api;
});

$(window).load(function() {

	// refresh animation in miliseconds
	jQuery.fx.interval = 55;
	
	/* Periodical effects */
	
	// Sun
	var sun_interval = 1500;
	var sun_interval_obj = setInterval(function() {
		var sun_obj = $('.cb-sun-img');
		
		var min_opacity = 0.7;
		var max_opacity = 1;
		var curr_opacity = $(sun_obj).css('opacity');
		
		if ( curr_opacity >= max_opacity ) {
			$(sun_obj).fadeTo(sun_interval, min_opacity);
		} else {
			$(sun_obj).fadeTo(sun_interval, max_opacity);
		}
	}, sun_interval + 100);
	
	// Tower light
	var towerlight_interval = 1500;
	$('.cb-towerlight-img').css('opacity', 0.7);
	var towerlight_interval_obj = setInterval(function() {
		var towerlight_obj = $('.cb-towerlight-img');
		
		var min_opacity = 0.6;
		var max_opacity = 1;
		var curr_opacity = $(towerlight_obj).css('opacity');
		
		if ( curr_opacity >= max_opacity ) {
			$(towerlight_obj).fadeTo(towerlight_interval, min_opacity);
		} else {
			$(towerlight_obj).fadeTo(towerlight_interval, max_opacity);
		}
	}, towerlight_interval + 100);
	
	/* Cars animation */
	setTimeout( "carShift( '.cb-cars-car-1', 3000, 290 )", 10000 );
	setTimeout( "carShift( '.cb-cars-car-2', 3000, 290 )", 16000 );
	setTimeout( "carShift( '.cb-cars-car-3', 2500, 290 )", 6000 );
	
	setTimeout( "carShift( '.cb-cars-car-1-return', 3000, 30, 'easeOutCubic' )", 30000 );
	
	/* Clouds animation */
	
	var cloud_duration = 200000;
	
	cloudShift( '.cb-clouds-cloud-1', cloud_duration, -600, 1250 );
	setTimeout(function() {
		cloudShift( '.cb-clouds-cloud-2', cloud_duration, -600, 1250 );
	}, (cloud_duration + 1) / 2);
	
	setInterval(function() {
		cloudShift( '.cb-clouds-cloud-1', cloud_duration, -600, 1250 );
	}, cloud_duration + 100);
	setTimeout(function() {
		setInterval(function() {
			cloudShift( '.cb-clouds-cloud-2', cloud_duration, -600, 1250 );
		}, cloud_duration + 100);
	}, (cloud_duration + 1) / 2);
	
});

// Get page content
function getPage( page_url ) {
	$.ajax({
		url: page_url,
		type: "post",
		dataType: "html",
		success: function( data ) {
			//$('.cb-page-content').html(data);
			window.api.getContentPane().html(data);
			window.api.reinitialise();
			// For certificates
			$('.cb-p-cont-certificate-lnk').lightBox();
			// Google map
			if ( $('#cb_page_cont_com_cont_map').length > 0 ) {
				(function() {
					var latlng = new google.maps.LatLng(53.9311, 51.205);

					var entire_map = new google.maps.Map(document.getElementById('cb_page_cont_com_cont_map'), {
						zoom: 12,
						center: latlng,
						mapTypeId: google.maps.MapTypeId.HYBRID,
						streetViewControl: false
					});

					var marker = new google.maps.Marker({
						position: latlng,
						map: entire_map,
						title: 'Влади'
					});
				})();
			}
			// If we need tj open a news
			if ( typeof(document.news_selected) != 'undefined' && document.news_selected != '' ) {
				$('.cb-page-cont-news-item[news_id="' + document.news_selected + '"]').find('.cb-page-cont-news-item-date').click();
			}
		}
	});
}

// Close the page content window
function closePageContent() {
	if ( $('.cb-page').css('display') != 'none' ) {
		//$('.cb-page').css('display', 'none');
		$('.cb-page').fadeOut('250');
	}
}

// Open the page content window
function openPageContent() {
	if ( $('.cb-page').css('display') == 'none' ) {
		//$('.cb-page').css('display', 'block');
		$('.cb-page').fadeIn('250');
	}
	var scroll_top_val = 70;
	if ( $(window).scrollTop() > scroll_top_val ) {
		$(window).scrollTop(scroll_top_val);
	}
}

// Callback form text toggling
function txtToggle( obj_selector, text ) {
	
	$(obj_selector).focus( function() {
		var curr_text = $(obj_selector).val();
		if ( curr_text == text ) {
			$(obj_selector).val('');
		}
	} );
	$(obj_selector).blur( function() {
		var curr_text = $(obj_selector).val();
		if ( $(obj_selector).val() == '' ) {
			$(obj_selector).val(text);
		}
	} );
}

// Clouds floating
function cloudShift( obj_selector, cloud_duration, start_pos, end_pos ) {
	if ( typeof(start_pos) != 'undefined' && start_pos != 'none' ) {
		$(obj_selector).css('left', start_pos + 'px');
	}
	$(obj_selector).animate({
		left: end_pos
	}, {
		duration: cloud_duration
	});
}

// Moving car from left to right
function carShift( car_obg_selector, car_duration, shift, car_easing ) {
	if ( typeof(car_easing) == 'undefined' ) {
		car_easing = 'easeInCubic';
	}
	$(car_obg_selector).animate({
		left: shift
	}, {
		duration: car_duration,
		easing: car_easing
	});
}

