$(document).ready(function(){

	schedule_mode = Number($.cookie('msm'));
	desired_day = location.hash.split('#')[1];

	var this_multiplex = $("#multiplex_id").text();
	$(".msch_time").attr('href',function(){
		return 'http://www.cinemapark.ru/booking/start/' + this_multiplex + '/' + $(this).attr('sid') + '/';
	});

	$(".msch_time").tooltip({
		showURL:	false,
		fade:		100,
		bodyHandler:	tooltip_time
	});

	function drag_handler_l(event,ui) {
		var this_position = this.offsetLeft;
		var this_parentwidth = $(this).parent().width();
		$(".timebar_l img").css('left',this_position+'px');
		var other_position = $(".timebar_r img").css('left').split('p')[0];
		if ( this_position > other_position && other_position > 0 ) {
			$(".timebar_r img").css('left',this_position+'px');
			$(".shader_r").css('width',this_parentwidth - this_position - 11 + 'px');
		};
		$(".shader_l").css('width',this_position + 'px');
	};

	function drag_handler_r(event,ui) {
		var this_position = this.offsetLeft;
		var this_parentwidth = $(this).parent().width();
		$(".timebar_r img").css('left',this_position+'px');
		var other_position = $(".timebar_l img").css('left').split('p')[0];
		if ( this_position < other_position ) {
			$(".timebar_l img").css('left',this_position+'px');
			$(".shader_l").css('width',this_position + 'px');
		};
		$(".shader_r").css('width',this_parentwidth - this_position - 11 + 'px');
	};

	$(".timebar_l img").draggable({
		axis: 'x',
		containment: 'parent',
		cursor: 'e-resize',
		drag: drag_handler_l,
		stop: drag_handler_l
	});

	$(".timebar_r img").draggable({
		axis: 'x',
		containment: 'parent',
		cursor: 'e-resize',
		drag: drag_handler_r,
		stop: drag_handler_r
	});

	$(".timebar_l img, .shader_l").dblclick(function(){
		$(".timebar_l img").css('left','0');
		$(".shader_l").css('width','0');
	});

	$(".timebar_r img, .shader_r").dblclick(function(){
		$(".timebar_r img").css('left','').css('right','0');
		$(".shader_r").css('width','0');
	});
		
	current_width = window.innerWidth;
	$(window).resize(function(e){
		if ( window.innerWidth != current_width ) {
			current_width = window.innerWidth;
			$(".timebar_l img").css('left','0');
			$(".timebar_r img").css('left','').css('right','0');
			$(".shader_l, .shader_r").css('width','0');
		};
	});

	$(".multiplex_schedule_datepicker a").click(function(){
		desired_day = $(this).attr('href').split('#')[1];
		if ( schedule_mode == 1 ) {
			var current_day_pos = $(this).parent().parent().parent().position().top;
			var desired_day_pos = $(".multiplex_schedule[id='s_" + desired_day + "']").position().top;
			window.scrollBy( 0, desired_day_pos - current_day_pos );
		} else {
			display_day_schedule(desired_day);
		};
	});

	init_schedule_display();

	$(".multiplex_schedule_mode0, .multiplex_schedule_mode1").click(function(){
		$(".multiplex_schedule_mode" + schedule_mode).hide();
		if ( schedule_mode == 1 ) {
			desired_day = $(this).attr('href').split('#')[1];
			schedule_mode = 0;
		} else {
			schedule_mode = 1;
		};
		init_schedule_display();
		$.cookie('msm', schedule_mode, { path: '/', expires: 1000 } );
		if ( pageTracker ) { pageTracker._trackEvent("Расписание мультиплекса", "Режим " + schedule_mode ); };
	});

});

function init_schedule_display() {
	$(".multiplex_schedule_mode" + schedule_mode).show();
	if ( schedule_mode == 1 ) {
		$(".multiplex_schedule").show();
		start_scroll();
	} else {
		display_day_schedule(desired_day);
	};
};

function display_day_schedule(day) {
	$(".multiplex_schedule").hide();
	var schedule_obj = $(".multiplex_schedule[id='s_" + day + "']");
	if ( schedule_obj.length > 0 ) {
		schedule_obj.show();
	} else {
		$(".multiplex_schedule").eq(0).show();
	};
};

function start_scroll() {
	if ( desired_day ) {
		var desired_day_obj = $(".multiplex_schedule[id='s_" + desired_day + "']");
		if ( desired_day_obj.length > 0 ) {
			var desired_day_pos = desired_day_obj.position().top;
			var first_day_pos = $(".multiplex_schedule").position().top;
			window.scroll( 0, desired_day_pos - 100 );
		};
	};
};

function tooltip_time() {
	var this_multiplex = $("#multiplex_id").text();
	var this_id = $(this).attr('sid');
	return '<div id="tooltip_header">Состояние мест</div><img src="http://graph.cinemapark.ru/hall_seats/' + this_multiplex + '/' + this_id + '.png" height="100"/>';
};
