var globOpen = true;
var globSimple = false;
document.isReady = false;

var popVoteOpen = false;

$(document).ready(function(){
	document.isReady = true;
});

function resizePopup(idpopup,idfader) {
	_lbox = $("."+idpopup)
	_fader = $("."+idfader)
	var _height = 0;
	var _width = 0;
	var _minWidth = $('body > div:eq(0)').outerWidth();
	_minWidth = 1000;
	if (window.innerHeight) {
		_height = window.innerHeight;
		_width = window.innerWidth;
	} else {
		_height = document.documentElement.clientHeight;
		_width = document.documentElement.clientWidth;
	}
	
	var arrayPageSize = getPageSize();
	_widthFader  = arrayPageSize[0];
	_heightFader = arrayPageSize[1];
	var _thisHeight = _lbox.outerHeight();
	var _page = $('body > div:eq(0)');
	if (_lbox.length) {
		if (_height > _page.outerHeight(true)) {
			_fader.css('height', _heightFader);
		}
		else {
			_fader.css('height', _heightFader);
		}
		
		if (_width < _minWidth) {
			_fader.css('width',_widthFader);
		} 
		else {
			_fader.css('width','100%');
		}
		
		if (_height > _thisHeight) {
			if (!window.innerHeight) {
				_lbox.css({
					position:'absolute',
					top: (document.documentElement.scrollTop + (_height - _thisHeight) / 2)+"px"
				});
			} else {
				_lbox.css({
					position:'fixed',
					top: ((_height - _lbox.outerHeight()) / 2)+"px"
				});
			}
		}
		else {
			_lbox.css({
				position:'absolute',
				top: 230
			});
			_fader.css({
				height:_heightFader
			});
		}
		if (_width > _lbox.outerWidth()) _lbox.css({left:(_width - _lbox.outerWidth()) / 2 + "px"});
		else _lbox.css({position:'absolute',left: 0});
	}
}

function ajaxPopupLoad(id, simple) {

	resizePopup('popupPhoto','popupBlack');
	jQuery(window).resize(function(){
		resizePopup('popupPhoto','popupBlack');
	});
	$('.popupBlack').show();
	$('.popupBlack').css('opacity', 0.6);
	
	var top = 230;
	
	$('.ajaxThrobber').css({left: (($(document.body).outerWidth()/2) - 16)+'px'});
	if(globSimple) {
		$('.ajaxThrobber').css( { position: 'absolute', top: top + $(window).scrollTop() } ).show();
	} else {
		$('.ajaxThrobber').css( { position: 'fixed', top: top } ).show();
	}
	$('.popupPhoto').load('/contest_popup/', { simple: simple, id: id}, function(){
		ajaxPopupShow();
	});

}

function ajaxPopupShow(){
	if (globOpen) {
		var top = 170;
		$('.ajaxThrobber').hide();
		$('.popupPhoto').show(0);
		/*$('#centerpopup').click(function(){
			ajaxPopupHide();
			return false;
		});*/
		$('.popupPhoto').fadeIn(333, function(){
		});
	}
}

function ajaxPopupHide(){
	globOpen = false;
	$('.ajaxThrobber').hide();
	$('.popupPhoto').fadeOut(333);
	$('.popupBlack').fadeOut(333, function(){
		resizeMask();
	});
	$('.popupPhoto').html('');
	return false;
}

function resizeMask(){
	$('.popupBlack').height($(document).height());
	$('.popupBlack').width($(window).width() < $(document.body).width() ? $(document.body).width() : $(window).width());
}

$(document).ready(function(){

	if (jQuery.browser.msie && parseInt(jQuery.browser.version) < 7) {
		globSimple = true;
		$('.popupPhoto').css({position: 'absolute',top:'400px'});
		$('.ajaxThrobber').css({position: 'absolute',top:'400px'});
	}

	$('.open-image').click(function(){
		globOpen = true;
		$('.popupPhoto').html('');
		if ($('.popupPhoto')) {
			var ob = $(this);
			
			id = parseInt(String(ob.attr('id')).split('picture_').join(''));
			if (isNaN(id))
			{
				id = String(ob.attr('id')).split('picture_plus_').join('');
			}
			ajaxPopupLoad(id, globSimple);
		}
		return false;
	});
	$('.popupBlack').click(function(){
		ajaxPopupHide();
		return false;
	});

});
function loadVoteFormPopup(contestant_id) {
	if (!document.isReady || isNaN(parseInt(contestant_id))) {
		return;
	}
	popUpVote = $('.popupVotePopup');
	
	if(popVoteOpen){
		popUpVote.hide();
	}
	
	popUpVote.html('<img src="/images/wait.gif" alt="wait" title="wait"/>')
	popUpVote.slideDown(500);
	popVoteOpen = true;

	$('#vote').load('/popupVoteForm', {'contestant_id' : contestant_id}, function(){
		$('.popupVotePopup .close a').click(function(e){
			popUpVote.slideUp(500);
			popVoteOpen = false;
			return false;
		});
		
		$('.reload_captcha').click(function(e){
			$('img.captcha').attr("src","/captcha?r=" + Math.random() + "&reload=1");
			return false;
		});
		
		$('.votePopFormSubmit').click(function(e){ 
			$('.votePopFormWait').show();
			$('.votePopFormSubmit').hide();
			$('#votePopFrm').submit();
		});
		
		$('#votePopFrm').bind('submit', function() {
			fields = $(this).find(':input, :text');
			var allFilled = true;
			for(i = 0; i < fields.size(); i++){
				ob = fields.eq(i);
				var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
				if (ob.val().length <= 0 && ob.attr('id') != 'id') {
					alert('Пожалуйста, заполните все поля');
					$('.votePopFormWait').hide();
					$('.votePopFormSubmit').show();
					return false;
				}
				if(ob.attr('id') == 'email' && !reg.test(ob.val())){
					alert('Формат e-mail не верный.');
					$('.votePopFormWait').hide();
					$('.votePopFormSubmit').show();
					return false;
				}
			}
			$(this).ajaxSubmit({
				url: '/popupVoteForm',
				dataType: 'html',
				success: function(res){
					$('#main_popup').hide().html(res).slideDown(500);
					$('.votePopFormWait').hide();
					$('.votePopFormSubmit').show();
				}
			});
			return false;
		});
	});
}

function loadVoteForm(contestant_id,place_id,has_double) {
	if (!document.isReady || isNaN(parseInt(place_id))) {
		return;
	}
	if(popVoteOpen){
		$(".popupVote").hide();
	}
	
	var top = '-448px';
	if (place_id < 5)
	{
		step = parseInt(place_id);
		$('.popupVote').css({ left: (8+step*194)+'px', top: top});
	}
	else
	{
		top = '-188px';
		step = parseInt(place_id) - 5;
		$('.popupVote').css({ left: (8+step*194)+'px', top: top });
	}
	
	$('.popupVote').html('<img src="/images/wait.gif" alt="wait" title="wait"/>')
	$('.popupVote').slideDown(500);
	popVoteOpen = true;

	$('#vote').load('/popupVoteForm', {'contestant_id' : contestant_id}, function(){
		$('.popupVote .close a').click(function(e){
			$(".popupVote").slideUp(500);
			popVoteOpen = false;
			return false;
		});
		
		$('.reload_captcha').click(function(e){
			$('img.captcha').attr("src","/captcha?r=" + Math.random() + "&reload=1");
			return false;
		});
		
		$('.votePopFormSubmit').click(function(e){ 
			$('.votePopFormWait').show();
			$('.votePopFormSubmit').hide();
			$('#votePopFrm').submit();
		});
		
		$('#votePopFrm').bind('submit', function() {
			fields = $(this).find(':input, :text');
			var allFilled = true;
			for(i = 0; i < fields.size(); i++){
				ob = fields.eq(i);
				var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
				if (ob.val().length <= 0 && ob.attr('id') != 'id') {
					alert('Пожалуйста, заполните все поля');
					$('.votePopFormWait').hide();
					$('.votePopFormSubmit').show();
					return false;
				}
				if(ob.attr('id') == 'email' && !reg.test(ob.val())){
					alert('Формат e-mail не верный.');
					$('.votePopFormWait').hide();
					$('.votePopFormSubmit').show();
					return false;
				}
			}
			$(this).ajaxSubmit({
				url: '/popupVoteForm',
				dataType: 'html',
				success: function(res){
					$('#main_popup').hide().html(res).slideDown(500);
					$('.votePopFormWait').hide();
					$('.votePopFormSubmit').show();
				}
			});
			return false;
		});
	});
}

function getPageSize() {
	        
     var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}

	return [pageWidth,pageHeight];
}
