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;
}
arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
return arrayPageSize;
};

function hidewidget () {
    	$('#ajax_container').hide();
		$('.widget-overlay').hide();
			$('.widget-overlay').html('<img class="loadergif" id="loadergif" src="/i/loader.gif" />');
			$('#ajax_container').html('');
    }
	
function showregtrue () {
	var data = '<div class="inv_card"><div class="invitation_card clearfix"><div class="caption_card clearfix"><p class="sm" style="text-align: center;">Вы успешно зарегистрировались  на сайте</p></div></div></div>';
	$('.widget-overlay').show();
	var arrPageSizes = ___getPageSize();
	var body_height = arrPageSizes[1];
	$('.widget-overlay').height(body_height);
	$('#loadergif').hide();
	$('#ajax_container').html(data);
	$('#ajax_container').width(780);
	$('#ajax_container').show();
	var marginleft = (arrPageSizes[0]-$('#ajax_container').width())/2 + 'px';
		if($.browser.msie && $.browser.version.substr(0,3)!="8.0" && $.browser.version.substr(0,3)!="9.0"){
			$('#ajax_container').css('left', marginleft);
		} else {
			$('#ajax_container').css('margin-left', marginleft);
		}
	var margintopnopx = (body_height-$('#ajax_container').height())/2;
	if (margintopnopx < 0) {
	  margintopnopx = 100;
	  var all_height = $('#ajax_container').height() + 120;
	  $('.widget-overlay').height(all_height);
		
	}
	var margintop = margintopnopx + 'px';				
	$('#ajax_container').css('margin-top', margintop);
	setTimeout(hidewidget, 2000);
	return false;
}
    
function makepage(src) {
  // We break the closing script tag in half to prevent
  // the HTML parser from seeing it as a part of
  // the *main* page.
  return "<html>\n" +
    "<head>\n" +
    "<title>Temporary Printing Window</title>\n" +
    "<script>\n" +
    "function step1() {\n" +
    "  setTimeout('step2()', 10);\n" +
    "}\n" +
    "function step2() {\n" +
    "  window.print();\n" +
 //   "  window.close();\n" +
    "}\n" +
    "</scr" + "ipt>\n" +
    "</head>\n" +
    "<body onLoad='step1()'>\n" +
    "<img src='" + src + "'/>\n" +
    "</body>\n" +
    "</html>\n";
}

$(document).ready(function(){ 
	
	$("#link_to_rules").click(function() {
		var current = $($(this).attr("href")).css("display");
		if(current == "none") {
			$($(this).attr("href")).show("slow");
		} else {
			$($(this).attr("href")).hide("slow");
		}
		return false;
	});
	
	$("#register_submit_button").attr("disabled", "disabled");
	$("#register_submit_button").fadeTo(0, 0.5);
	
	$("#i_agree").click(function() {
		if($("#register_submit_button").attr("disabled") && $(this).attr("checked") == true) {
			$("#register_submit_button").removeAttr("disabled");
			$("#register_submit_button").fadeTo(0, 1);
		} else {
			$("#register_submit_button").attr("disabled", "disabled");
			$("#register_submit_button").fadeTo(0, 0.5);
		}
	});
	
	$(".navline ul").each(function(){
		var width = $(this).parent().width();
		var nnwidth = width/2;
        if($.browser.msie && $.browser.version.substr(0,3)!="8.0" && $.browser.version.substr(0,3)!="9.0"){
	        var margin = (0 - nnwidth) + 'px';
	        $(this).css('margin-left', margin);
        }
	});
	$(".navline ul:last").each(function(){
		var width = $(this).parent().width();
		var nnwidth = width/2;
		if($.browser.msie && $.browser.version.substr(0,3)!="8.0" && $.browser.version.substr(0,3)!="9.0"){
	        var margin = (0 - nnwidth - 76) + 'px';
	        $(this).css('margin-left', margin);
        }
	});
	$(function() {
        $('.gallery_list a').lightBox();
    });
	$(".navline td").hover(
		function () {
			$(this).addClass("jshover");
		},
		function () {
			$(this).removeClass("jshover");
		}
	);
	$("#search").focus(function(){$(this).attr("value",'')});
	$("#subscribe").focus(function(){$(this).attr("value",'')});
	
	$("#printing").click(function(){
		printWin=window.open($(this).attr("href"),'printWindow','resizable=no,width=500,height=500,menubar=yes,status=no,scrollbars=yes');
		printWin.document.open();
  		printWin.document.write(makepage($(this).attr("href")));
  		printWin.document.close();

		return false;
	});
	
	$("p.errorsubscribe").click(function(){$(this).hide(500)});
	
	$("#sendinvite").click(function(){
		$('.widget-overlay').show();
		$.ajax({
				url: '/includes/inviteform.php',
				success: function(data) {
				var arrPageSizes = ___getPageSize();
				var body_height = arrPageSizes[1];
				$('.widget-overlay').height(body_height);
				$('#loadergif').hide();
				$('#ajax_container').html(data);
				$('#ajax_container').width(780);
				$('#ajax_container').show();
				var marginleft = (arrPageSizes[0]-$('#ajax_container').width())/2 + 'px';
				if($.browser.msie && $.browser.version.substr(0,3)!="8.0" && $.browser.version.substr(0,3)!="9.0"){
					$('#ajax_container').css('left', marginleft);
				} else {
					$('#ajax_container').css('margin-left', marginleft);
				}
				var margintop = (body_height-$('#ajax_container').height())/2 + 'px';
				$('#ajax_container').css('margin-top', margintop);
				var imgpath = $("#sendinvite").attr("href");
				$("#eventsrc").attr("value", imgpath);
				var eventid = $("#hiddeneventid").attr("value");
				$("#eventid").attr("value", eventid);
				var imgstr = '<img src="' + imgpath + '" width="' + 220 + '" />';
				$('#imgdiv').html(imgstr);
				$('#close_link').click(function() {
					/*$('.widget-overlay').hide();
					$('.widget-overlay').html('<img class="loadergif" id="loadergif" src="/i/loader.gif" />');
					$('#ajax_container').html('');
					$('#ajax_container').hide();*/
					hidewidget();
					return false;
				});

				$("#eventform").submit(function(){
					var email = $("#email").attr("value");
				    if ((/^([a-z0-9_\-]+\.)*[a-z0-9_\-]+@([a-z0-9][a-z0-9\-]*[a-z0-9]\.)+[a-z]{2,4}$/i).test(email)) {
						$.post('/sendelement.php', $("#eventform").serialize(), function(data) {
						  $('.inv_form').html(data);
						});
					} else {
							$("#emailerror").show();
							$("#emailerror").fadeOut(2000);	
					}
					return false;
				});
			}
		});
		return false;
	});
	/*
	$.ajax({
		url: '/includes/sizes.php',
		success: function(data) {
		var arrPageSizes = ___getPageSize();
		var body_height = arrPageSizes[1];
		$('.widget-overlay').height(body_height);
		$('#loadergif').hide();
		$('#ajax_container').html(data);
		$('#ajax_container').width(780);
		$('#ajax_container').show();
		var marginleft = ($(window).width()-$('#ajax_container').width())/2 + 'px';
		var margintop = ($(window).height()-$('#ajax_container').height())/2 + 'px';
		$('#ajax_container').css('margin-left', marginleft);
		$('#ajax_container').css('margin-top', margintop);
	}
});
*/
	$(window).resize(function() {
		var arrPageSizes = ___getPageSize();
		var body_height = arrPageSizes[1];
		var marginleft = (arrPageSizes[0]-$('#ajax_container').width())/2 + 'px';
		if($.browser.msie && $.browser.version.substr(0,3)!="8.0" && $.browser.version.substr(0,3)!="9.0"){
			$('#ajax_container').css('left', marginleft);
		} else {
			$('#ajax_container').css('margin-left', marginleft);
		}
		var margintop = (body_height-$('#ajax_container').height())/2 + 'px';
		$('#ajax_container').css('margin-top', margintop);
	});
	
	$('.widget-overlay').click(function() {
		/*$('.widget-overlay').hide();
		$('.widget-overlay').html('<img class="loadergif" id="loadergif" src="/i/loader.gif" />');
		$('#ajax_container').html('');
		$('#ajax_container').hide();*/
		hidewidget();
		return false;
	});
	
	/* wish-list Delete */
	$("div.wish_element a.delete").click(function() {
		var itemid = $(this).attr("href");
		$.post('/deletewish.php', {ELEMENT_ID : itemid});
		$("#element_"+itemid).hide("slow", function() {
			$("#element_"+itemid).remove();
		});

		return false;
	});
	
	/* регистрация */
	$("form[name='regform']").submit(function() {
		$("input[name='REGISTER[EMAIL]']").val($("input[name='REGISTER[LOGIN]']").val());
		/*$("input[name='REGISTER[CONFIRM_PASSWORD]']").val($("input[name='REGISTER[PASSWORD]']").val());*/
	});
	
	$(".royal img").fadeTo(0, 0.6);
	$(".royal img").hover(
		function () {
			$(this).fadeTo(0, 1);
		},
		function () {
			$(this).fadeTo(0, 0.6);
		}
	);
	
	$("#feedback").validity(function() {
		//$("#server").match(/^\d{1,3}[\.]\d{1,3}[\.]\d{1,3}[\.]\d{1,3}[:]\d{1,5}$/, 'Р’РІРµРґРёС‚Рµ РІ С„РѕСЂРјР°С‚Рµ ip:port');
		$("#NAME").require('Вы не указали имя!');
		$("#EMAIL").require('Вы не указали e-mail!');
		$("#MESSAGE").require('Вы не заполнили поле Сообщение');
		$("#EMAIL").match('email', 'Некорректный E-mail!');
    });
	
	$("#show_auth").click(function() {
		$("#contest_register").slideUp(250);
		$("#contest_auth").slideDown(250);
		return false;
	});
	
	$("#show_register").click(function() {
		$("#contest_auth").slideUp(250);
		$("#contest_register").slideDown(250);
		return false;
	});
	
	$("#form_contest_auth").submit(function() {
		if($("#i_agree_contest").attr("checked") != true) {
			return false;
		}
	});
	
	$("#form_contest_register").submit(function() {
		if($("#i_agree_contest_register").attr("checked") != true) {
			return false;
		}
	});
	
	$("a.i_agree_with_rules").click(function() {
		if($($(this).attr("href")).css("display") == "block") {
			$($(this).attr("href")).slideUp(250);
		} else {
			$($(this).attr("href")).slideDown(250);
		}
		return false;
	});
	
	$("#form_contest_auth #i_agree_contest").change(function() {
		if($(this).attr("checked") == true) {
			$("#form_contest_auth input[type='submit']").removeAttr("disabled");
		} else {
			$("#form_contest_auth input[type='submit']").attr("disabled", "disabled");
		}
	});
	
	$("#form_contest_register #i_agree_contest_register").change(function() {
		if($(this).attr("checked") == true) {
			$("#form_contest_register input[type='submit']").removeAttr("disabled");
		} else {
			$("#form_contest_register input[type='submit']").attr("disabled", "disabled");
		}
	});
	
});
