//-------------------//
// overall functions //

var ua = navigator.userAgent.toLowerCase();
var isOpera = (ua.indexOf('opera')  > -1);
var isIE = (!isOpera && ua.indexOf('msie') > -1);

function getDocumentHeight() {
	return Math.max(document.compatMode != 'CSS1Compat' ? document.body.scrollHeight : document.documentElement.scrollHeight, getViewportHeight());
}
function getDocumentWidth() {
	return Math.max(document.compatMode != 'CSS1Compat' ? document.body.scrollWidth : document.documentElement.scrollWidth, getViewportWidth());
}
function getViewportHeight() {
	return ((document.compatMode || isIE) && !isOpera) ? (document.compatMode == 'CSS1Compat') ? document.documentElement.clientHeight : document.body.clientHeight : (document.parentWindow || document.defaultView).innerHeight;
}
function getViewportWidth() {
	return ((document.compatMode || isIE) && !isOpera) ? (document.compatMode == 'CSS1Compat') ? document.documentElement.clientWidth : document.body.clientWidth : (document.parentWindow || document.defaultView).innerWidth;
}

function ShowOverAll() {
	$(".b-overall").css("height", (getDocumentHeight()));
	$(".b-overall").css("width", (getDocumentWidth()));
	$(".b-overall IFRAME").css("height", (getDocumentHeight()));
	$(".b-overall IFRAME").css("width", (getDocumentWidth()));
	$(window).resize(function(){
		$(".b-overall").css("height", (getDocumentHeight()));
		$(".b-overall").css("width", (getDocumentWidth()));
		$(".b-overall IFRAME").css("height", (getDocumentHeight()));
		$(".b-overall IFRAME").css("width", (getDocumentWidth()));
	});
	$(".b-overall").show();
}

function HideOverAll() {
	$(".b-overall").hide();
}

// overall functions //
//-------------------//




var $b_header_dates_timer;
var $b_popup_filter_timer;
var $b_session_time_popup_timer;
var $b_session_time_popup_checker;
var $b_card_place_time_popup_timer;

function b_header_dates_GO($to) {
	$("#b-header-dates .container").stop();
	if ($to=='prev')
		$("#b-header-dates .container").scrollTo("-=120px", 500, {axis:'x'});
	if ($to=='next')
		$("#b-header-dates .container").scrollTo("+=120px", 500, {axis:'x'});
}

function b_side_tabs_show($what) {
	$('.b-side-tabs-title A').removeClass('cur');
	$('.b-side-tabs-title .'+$what+' A').addClass('cur');
	$('.b-side-tabs .places').hide();
	$('.b-side-tabs .opinions').hide();
	$('.b-side-tabs .users').hide();
	$('.b-side-tabs .'+$what).show();
}

function b_card_full_show($what) {
	b_session_time_popup_hide();
	$('.b-card-full .menu A').parent().removeClass('active');
	$('.b-card-full .menu A.m-'+$what).parent().addClass('active');
	$('.b-card-full .session').hide();
	$('.b-card-full .trailer').hide();
	$('.b-card-full .content').hide();
	$('.b-card-full .'+$what).show();
}

function b_popup_filter_hide() { clearTimeout($b_popup_filter_timer); $('.b-popup-filter').hide(); }

function b_session_time_popup_hide() {
	clearTimeout($b_session_time_popup_timer);
	$('.b-card-full .time .popup').hide();
	/* hack for opera */
	if ($b_session_time_popup_checker==0) {
		$('.b-card-full .session .body TABLE').css('border-color', 'red');
		$b_session_time_popup_checker=1;
	} else {
		$('.b-card-full .session .body TABLE').css('border-color', 'green');
		$b_session_time_popup_checker=0;
	}
};

function b_card_place_time_popup_hide() {
	clearTimeout($b_card_place_time_popup_timer);
	$('.b-card-place .time .popup').hide();
};


$(document).ready(function() {
	
	
	/* hacks =) */
	$('.l-wrap').css('margin', 0);
	$('#b-header-dates .link-wrap').css('padding', 0);
	$('#b-header-dates .link').hover(
		function(){ $(this).addClass('header-dates-link-hover'); },
		function(){ $(this).removeClass('header-dates-link-hover'); }
	);
	$('#b-header-dates .prev DIV').hover(
		function(){ $(this).addClass('header-dates-link-hover'); },
		function(){ $(this).removeClass('header-dates-link-hover'); }
	);
	$('#b-header-dates .next DIV').hover(
		function(){ $(this).addClass('header-dates-link-hover'); },
		function(){ $(this).removeClass('header-dates-link-hover'); }
	);
	
	
	/* Dates in header */
	var $width = 0;
	$('#b-header-dates LI').each(function(i){ $width += $(this).width(); });
	$('#b-header-dates UL').css('width', $width + 'px');
	$("#b-header-dates .next A").hover(
		function(){
			b_header_dates_GO('next');
			$b_header_dates_timer = setInterval("b_header_dates_GO('next')", 500);
		},
		function(){
			$("#b-header-dates .container").stop();
			clearInterval($b_header_dates_timer);
		}
	);
	$("#b-header-dates .prev A").hover(
		function(){
			b_header_dates_GO('prev');
			$b_header_dates_timer = setInterval("b_header_dates_GO('prev')", 500);
		},
		function(){
			$("#b-header-dates .container").stop();
			clearInterval($b_header_dates_timer);
		}
	);
	$("#b-header-dates .prev A").click(function(){ return false; });
	$("#b-header-dates .next A").click(function(){ return false; });
	
	
	/* Check graph or text */
	$('.b-check-gt A.graph').click(function(){
		$('.b-check-gt DIV').removeClass('t').addClass('g');
		$('#text-content').hide();
		$('#graph-content').show();
		$.post('/ajax/settypelist', {'type':'graph'}, function(data){
			if(list_type == 1){
				window.location.reload(); 
			}
		});
		return false;
	});
	$('.b-check-gt A.text').click(function(){
		$('.b-check-gt DIV').removeClass('g').addClass('t');
		$('#graph-content').hide();
		$('#text-content').show();
		$.post('/ajax/settypelist', {'type':'text'});
		return false;
	});
	
	
	/* cards */
	$('.b-card').hover(
		function(){ $('.more', this).show(); },
		function(){ $('.more', this).hide(); }
	);
	
	
	/* full cards */
	b_card_full_show('session');
	$('.b-card-full .menu A.m-session').click(function(){ b_card_full_show('session'); return false; })
	$('.b-card-full .menu A.m-trailer').click(function(){ b_card_full_show('trailer'); return false; })
	$('.b-card-full .menu A.m-content').click(function(){ b_card_full_show('content'); return false; })
	/* full cards - session time popups */
	$('.b-card-full .time .popup').each(function(i){ $(this).css('top', '-'+($(this).height()+5)+'px'); });
	$('.b-card-full .time .popup').parent().hover(
		function() {
			b_session_time_popup_hide();
			$('.popup', this).css('width', $('.popup', this).width()+'px'); // hack for opera
			$('.popup', this).show();
		},
		function() {
			$b_session_time_popup_timer = setTimeout("b_session_time_popup_hide()", 500);
		}
	);
	
	
	/* tabs in sidebar */
	if (!showInformerRight.length) showInformerRight = 'places';
	b_side_tabs_show(showInformerRight);
	$('.b-side-tabs-title .places A').click(function(){ b_side_tabs_show('places'); return false; });
	$('.b-side-tabs-title .opinions A').click(function(){ b_side_tabs_show('opinions'); return false; });
	$('.b-side-tabs-title .users A').click(function(){ b_side_tabs_show('users'); return false; });
	
	
	/* popup filter general */
	$(".b-popup-filter").hover(
		function(){ clearTimeout($b_popup_filter_timer); },
		function(){ $b_popup_filter_timer = setTimeout("b_popup_filter_hide()", 2000); }
	);
	$('.b-popup-filter .btn-cancel').click(function(){
		$(this).parent().parent().hide();
		return false;
	});
	
	/* popup cinema filter */
	$('#popup-filter-cinema .b-popup-filter').css('left', $('#popup-filter-cinema A.btn').width()+27+18+10+'px');
	$('#popup-filter-cinema A.btn').click(function(){
		clearTimeout($b_popup_filter_timer);
		b_popup_filter_hide();
		$('#popup-filter-cinema .b-popup-filter').show();
		$b_popup_filter_timer = setTimeout("b_popup_filter_hide()", 2000);
		return false;
	});
	$('#popup-filter-cinema .btn-filter').click(function(){
		// СОБЫТИЕ ПРИ НАЖАТИИ НА КНОПКУ "ФИЛЬТРОВАТЬ"
		$('#popup-filter-cinema .b-popup-filter').hide();
		return false;
	});
	
	/* popup genre filter */
	$('#popup-filter-genre .b-popup-filter').css('left', $('#popup-filter-genre A.btn').width()+27+18+10+'px');
	$('#popup-filter-genre A.btn').click(function(){
		b_popup_filter_hide();
		$('#popup-filter-genre .b-popup-filter').show();
		$b_popup_filter_timer = setTimeout("b_popup_filter_hide()", 1000);
		return false;
	});
	$('#popup-filter-genre .btn-filter').click(function(){
		// СОБЫТИЕ ПРИ НАЖАТИИ НА КНОПКУ "ФИЛЬТРОВАТЬ"
		$('#popup-filter-genre .b-popup-filter').hide();
		return false;
	});
	
	
	/* popup map */
	$('.b-place-desc A.map-link').click(function(){
		$('.b-place-desc .popup-map').css('left', $(this).offset().left + $(this).width() + 10 + 'px');
		$('.b-place-desc .popup-map').css('top', $(this).offset().top - 25 + 'px');
		$('.b-place-desc .popup-map').show();
		src = $('#map_iframe_input').val();
		$('#map_iframe').attr('src', src);
		return false;
	});
	$('.b-place-desc .popup-map A.close').click(function(){
		$('.b-place-desc .popup-map').hide();
		return false;
	});
	
	/* in place cards - session popups */
	$('.b-card-place .time .popup').each(function(i){$(this).css('top', '-'+($(this).height()+5)+'px'); });
	$('.b-card-place .time .popup').parent().hover(
		function() {
			b_card_place_time_popup_hide();
			$('.popup', this).css('width', $('.popup', this).width()+'px'); // hack for opera
			$('.popup', this).show();
		},
		function() {
			$b_card_place_time_popup_timer = setTimeout("b_card_place_time_popup_hide()", 500);
		}
	);
	
	
});



//---------------------------//
//     Всплывающие формы     //

$(document).ready(function() {
	
	$('#login-form-big').click(function() {
		ShowOverAll();
		$('#b-big-comment-form').hide();
		$('#b-big-login-form').show();
		return false;
	});
	
	$('#comment-form').click(function() {
		ShowOverAll();
		$('#b-big-comment-form').show();
		return false;
	});
	
	$('.opinions .all-link .add').click(function() {
		ShowOverAll();
		$('#b-big-comment-form').show();
		return false;
	});
	
	$('.b-photo_form a.cancel').click(function() {
		HideOverAll();
		$('.b-photo_form').hide();
		return false;
	});
	
	$('.want_photo').click(function() {
		ShowOverAll();
		$('.b-photo_form').show();
		return false;
	});

	$('#b-big-login-form A.cancel').click(function(){
		$('#b-big-login-form').hide();
		HideOverAll();
		return false;
	});
	
	$('#b-big-comment-form A.cancel').click(function(){
		$('#b-big-comment-form').hide();
		HideOverAll();
		return false;
	});
	
	var comment_rating = 0;
	var rating_tmp = 0;
	
	/* рейтинг - меняем */
	$('#b-big-comment-form .b-rating3').mousemove(function(e){
		// rating - это оценка от 1 до 5
		rating_tmp = Math.ceil((e.clientX-$(this).offset().left)/23)
		offset = rating_tmp*23;
		$('div', this).css('width', offset+'px');
		$('#comment_add_raiting').val(rating_tmp);
	});
	
	/* рейтинг - фиксируем */
	$('#b-big-comment-form .b-rating3').click(function(e){
		comment_rating = rating_tmp;
	});
	
	/* рейтинг - при уходе мышки - возвращаем зафиксированный */
	$('#b-big-comment-form .b-rating3').mouseout(function(){
		$('div', this).css('width', (comment_rating*23)+'px');
	});
	
});

//     Всплывающие формы     //
//---------------------------//


//---------------------------//
//        Окно помощи        //

$(document).ready(function() {
	
	$('#ticket-help').click(function() {
		ShowOverAll();
		$('#b-popup-help-info').show();
		return false;
	});
	
	$('#b-popup-help-info A.cancel').click(function(){
		$('#b-popup-help-info').hide();
		HideOverAll();
		return false;
	});
	
});

//        Окно помощи        //
//---------------------------//




function setCommentId(id){
	$('#comment_id').val(id);
}

$(document).ready(function() {
	$('.comment-form123').click(function() {
		ShowOverAll();
		$('#b-big-comment-form').show();
		return false;
	});
	
	$('.comment-show123').click(function(){
		var tmp = this.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.getElementsByTagName("div");
		if (tmp[1])
			if ($(tmp[1]).css('display') == 'none') $(tmp[1]).show(); else $(tmp[1]).hide();
		return false;
	})
	$('#b-big-comment-form .cancel123').click(function(){
		$('#b-big-comment-form').hide();
		setCommentId(0);
		HideOverAll();
		return false;
	});
})
