(function ($) {
    $.fn.vAlign = function(container) {
        return this.each(function(i){
	   if(container == null) {
	      container = 'div';
	   }
	   var paddingPx = 10; //change this value as you need (It is the extra height for the parent element)
	   $(this).html("<" + container + ">" + $(this).html() + "</" + container + ">");
	   var el = $(this).children(container + ":first");
	   var elh = $(el).height(); //new element height
	   var ph = $(this).height(); //parent height
	   if(elh > ph) { //if new element height is larger apply this to parent
	       $(this).height(elh + paddingPx);
	       ph = elh + paddingPx;
	   }
	   var nh = (ph - elh) / 2; //new margin to apply
	   $(el).css('margin-top', nh-1); //bo margin w srodku
        });
     };
})(jQuery);

$(document).ready(function()
{
	//full screen background
	$.backstretch("/images/background.png");
	
	//middling everythins
	$('.middle').each(function(index) {
		$(this).vAlign();
  	});

	$("#content").height($("#content").height() - 10);
		
        //transparenting
        $(".transparent").each(function(index){
            $(this).transBGdraw();
        });

        if($("#two").height() > 0){
            $("#content").height($("#one").height() + $("#two").height() - 3);
        }else{
            $("#content").height($("#one").height() - 3);
        }


        $(window).resize();

	if ($.browser.msie && $.browser.version == "7.0") {

		$("#menuTransBG").css({ top : $("#menu").offset().top - 20});
		$("#content-1TransBG").css({ top: $("#content-1").offset().top - 20});
		$("#content-2TransBG").css({ top: $("#content-2").offset().top - 20});        
	}
        
        $("#contact_href").fancybox({
            'transitionIn'	:	'elastic',
            'transitionOut'	:	'elastic',
            'speedIn'           :       600,
            'speedOut'          :       200,
            'overlayShow'	:	true
        });
});

$(window).resize(function(){
    resize();
});

function resize(){
    
//    $(".bar").each(function(){
//            $(this).height(31);
//        });
    
    $("#menu").height($("#content-container").height());
    
    var menu_h = $("#menu").height();
    var content_h = $("#content-container").height();
    var diff_size = 0;
    
    if (menu_h < content_h == true) {
        $("#menu").height(menu_h + (content_h - menu_h));
    }
    
    else if (menu_h > content_h == true) {
        $("#menu").height(menu_h - (menu_h - content_h));
    }
    
    else {
        $("#menu").height(menu_h);
    }

    if ($.browser.msie) {

	if ($.browser.version == "7.0")
		$("#menu").height($("#menu").height() + 4);
	else if ($.browser.version == "8.0")
		$("#menu").height($("#menu").height() - 4);
	else {}

    }
    
    else {

	$("#menu").height($("#menu").height() - 3);

    }
    if($("#two").height() > 0){
        $("#content").height($("#one").height() + $("#two").height() - 3);
    }else{
        $("#content").height($("#one").height() - 3);
    }

    $(".transBG").each(function() {$(this).transBGredraw();});
    ie_fix();
}

function ie_fix() {

	var menu = $("#menu");

	menu.find("div.heading").each(function(){

		var h_width = $(this).width();
		/*var h_width = menu.width();
		if (menu.css("padding-left") > 0)
			h_width -= menu.css("padding-left");
		if (menu.css("padding-right") > 0)
			h_width -= menu.css("padding-right");

		var triangle = $("div.triangle-right-up");

		h_width -= triangle.width();

		if (triangle.css("padding-left") > 0)
			h_width -= triangle.css("padding-left");
		if (triangle.css("padding-right") > 0)
			h_width -= triangle.css("padding-right");1

		if ($(this).css("padding-left") > 0)
			h_width -= $(this).css("padding-left");
		if ($(this).css("padding-right") > 0)
			h_width -= $(this).css("padding-right");

		h_width = Math.round(h_width + 0.5);

		if ($.browser.msie && $.browser.version == "7.0") 
			h_width -= 11;
		
		$(this).width(h_width);

		*/
		var t = $("div.triangle-right-up");
		var n = $(this).position();

		while (t.position().top > n.top) {
			h_width--;
			$(this).width(h_width);
		}

		if ($.browser.msie && $.browser.version == "7.0") {

			$("#two").find("div.bar").each(function(){
				$(this).height(28);
			});
			$("#content-2TransBG").css({ top: $("#content-2").offset().top});
		}
	});

}

