$(document).ready(function() {
	effects_init();
	decode_mail_links();
	forms_funcs();
	cufon_replace();
});

function effects_init() {
	$("a#login_link").click(function() {
		var $box = $("div#login_box");
		if ($box.is(":hidden")) {
			$box.slideDown("fast");
		}
		else {
			$box.slideUp("fast");
		}

		$(this).blur();
		return false;
	});

	$("#article_bar .bar .links a").click(function() {
		var item_num = $(this).attr("id").replace('bar_link_item_', '');

		if ($(this).hasClass("active")) {
			$(this).removeClass("active")
			$("#bar_bg_item_" + item_num).hide();
			$("#bar_bg_item_0").show();
			$("#bar_text_item_" + item_num).slideUp("fast", function() {
				$("#article_bar_inner").removeClass("inner_opened");
				$("#article_bar_inner").addClass("inner_closed");
			});
		}
		else {
			var switch_active = false;
			for (var i = 1; i < 5; i++) {
				if ($("#bar_link_item_" + i) && $("#bar_link_item_" + i).hasClass("active")) {
					switch_active = i;
					break;
				}
			}

			if (switch_active !== false) {
				$("#bar_link_item_" + switch_active).removeClass("active")
				$("#bar_bg_item_" + switch_active).hide();
				$("#bar_text_item_" + switch_active).fadeOut("fast", function() {
					$("#bar_text_item_" + item_num).fadeIn("fast");
				});

				$(this).addClass("active")
				$("#bar_bg_item_" + item_num).show();
			}
			else {
				$(this).addClass("active")
				$("#article_bar_inner").removeClass("inner_closed");
				$("#article_bar_inner").addClass("inner_opened");
				$("#bar_bg_item_" + item_num).show();
				$("#bar_bg_item_0").hide();
				$("#bar_text_item_" + item_num).slideDown("fast");
			}
		}

		$(this).blur();
		return false;
	});

	$("div.main_menu a[id^='mm_']").each(function() {
		var expl = $(this).attr('id').split('_');
		var $section_box = $("div#mm_section_"+expl[1])

		if ($section_box.length > 0) {
			$(this).click(function() {
				if ($section_box.hasClass("hidden")) {
					$section_box.slideDown("fast");
					$section_box.removeClass("hidden");
				}
				else {
					$section_box.slideUp("fast");
					$section_box.addClass("hidden");
				}

				$(this).blur();
				return false;
			});
		}
	});

	$("div#people_list div.more a").click(function() {
		var item_id = $(this).attr("id").replace('person_more_', '');
		var $cv_box = $("div#cv_box_" + item_id);

		if ($cv_box.hasClass("hidden")) {
			$(this).addClass("active");
			$cv_box.slideDown("fast");
			$cv_box.removeClass("hidden");
		}
		else {
			$cv_box.slideUp("fast");
			$cv_box.addClass("hidden");
			$(this).removeClass("active");
		}

		$(this).blur();
		return false;
	});

	$("#poll_box a").click(function() {
		var href = $(this).attr("href");
		var expl = href.split("?");
		var target = expl[0];
		if(expl[1] != 'undefined') {
			var formData = expl[1];
		}
		else {
			var formData = '';
		}
		formData += formData != '' ? '&ajax=1' : '?ajax=1';

		var options = {
	        url: target,
	        data: formData,
	        type: "GET",
			success: poll_submit_response
	    };
		$.ajax(options);

		$(this).blur();
		return false;
	});

	$("a#banner_wrap").hoverIntent(banner_over, banner_out);

	$('a.lightbox').lightBox({
		overlayOpacity: 0.4,
		imageLoading: hostname + '/img/lightbox/loading.gif',
		imageBtnClose: hostname + '/img/lightbox/close-' + lang + '.gif',
		imageBtnPrev: hostname + '/img/lightbox/prev-' + lang + '.gif',
		imageBtnNext: hostname + '/img/lightbox/next-' + lang + '.gif',
		imageBlank:	hostname + '/img/lightbox/blank.gif',
		//fixedNavigation: true,
		txtImage: 'Foto',
		txtOf: '/'
	});

	$("div#login_box div.submit input")
	.hover(
		function() {
			$(this).css("background-image", "url(" + hostname + "/img/bg-login-box-submit-hover.gif)");
		},
		function() {
			$(this).css("background-image", "url(" + hostname + "/img/bg-login-box-submit.gif)");
		}
	);

	$("div#top_menu .search_box input.submit_cz")
	.hover(
		function() {
			$(this).css("background-image", "url(" + hostname + "/img/bg-search-box-submit-hover-cz.gif)");
		},
		function() {
			$(this).css("background-image", "url(" + hostname + "/img/bg-search-box-submit-cz.gif)");
		}
	);

	$("div#top_menu .search_box input.submit_en")
	.hover(
		function() {
			$(this).css("background-image", "url(" + hostname + "/img/bg-search-box-submit-hover-en.gif)");
		},
		function() {
			$(this).css("background-image", "url(" + hostname + "/img/bg-search-box-submit-en.gif)");
		}
	);
}

function decode_mail_links() {
	$("span[class^='mail_link_replace']").each(function() {
		var $html = Base64.decode($(this).attr("title"));
		$(this).replaceWith($html);
	});
}

function forms_funcs() {
	$("div#form div.content table td.submit a").click(function() {
		$(this).parents("form").submit();

		this.blur();
		return false;
	});

	$('div#form div.content table td.col2 select').sSelect({ddMaxHeight: '300px'});

	$('div#form form, div#login_box form').append('<input type="hidden" name="js_checker" value="1" />');
}

function cufon_replace() {
	Cufon.replace('div#article_bar .content .bar .links a, div.subtitle div.inner, div.subtitle2 div.inner');
};

function poll_submit_response(data) {
	if (data.indexOf('|') == -1) {
		var txt = data;
	}
	else {
		var expl = data.split("|");
		var txt = expl[1];

		var $count = $("a#poll_answer_" + expl[0] + ' .meter .count');
		var num = parseInt($count.text());
		$count.text(num += 1);
	}

	var $response_box = $("#poll_response");
	if ($response_box.length) {
		$response_box.html(txt);
	}
	else {
		$("#poll_box").append('<br /><div id="poll_response" class="hidden">' + txt + '</div>');
		$("#poll_response").slideDown("fast");
	}
}

function banner_over() {
	$("a#banner_wrap .inner2").animate({ paddingTop:"13px" }, "fast");
}

function banner_out() {
	$("a#banner_wrap .inner2").animate({ paddingTop:"8px" }, "fast");
}

var Base64 = {
    _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

    encode : function (input) {
        var output = "";
        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
        var i = 0;

        input = Base64._utf8_encode(input);

        while (i < input.length) {

            chr1 = input.charCodeAt(i++);
            chr2 = input.charCodeAt(i++);
            chr3 = input.charCodeAt(i++);

            enc1 = chr1 >> 2;
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            enc4 = chr3 & 63;

            if (isNaN(chr2)) {
                enc3 = enc4 = 64;
            } else if (isNaN(chr3)) {
                enc4 = 64;
            }

            output = output +
            this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
            this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

        }

        return output;
    },

    decode : function (input) {
        var output = "";
        var chr1, chr2, chr3;
        var enc1, enc2, enc3, enc4;
        var i = 0;

        input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

        while (i < input.length) {

            enc1 = this._keyStr.indexOf(input.charAt(i++));
            enc2 = this._keyStr.indexOf(input.charAt(i++));
            enc3 = this._keyStr.indexOf(input.charAt(i++));
            enc4 = this._keyStr.indexOf(input.charAt(i++));

            chr1 = (enc1 << 2) | (enc2 >> 4);
            chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
            chr3 = ((enc3 & 3) << 6) | enc4;

            output = output + String.fromCharCode(chr1);

            if (enc3 != 64) {
                output = output + String.fromCharCode(chr2);
            }
            if (enc4 != 64) {
                output = output + String.fromCharCode(chr3);
            }

        }

        output = Base64._utf8_decode(output);

        return output;

    },

    _utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    _utf8_decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }
}
