function setCookie(c_name,value,expiredays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}

function getCookie(c_name) {
	if (document.cookie.length>0) {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) {
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}


//var headerindex = 1;
var $txs=setInterval('headerAutoChange()', 4000);
$stop = 0;

$(document).ready(
function(){
	(getCookie("sifrFetch")) ? "" : setCookie("sifrFetch", true);
   docReadyQ();
   loginMore();
   formatDate();
});


function formatDate (){
    $("span.news-date").each(
        function(){
            var val = $(this).html();
            $(this).html(val.substring(0,10));
        }
    );
}

function docReadyQ(){
        addEvents();
        patchActiveMenuElement();


        $('.smartGallery').each(function(index) {
            $galleryCount++;
            $(this).addClass("g"+index);
        });
        smartGalleryStart();

        mainHeaderEvents();
        

        if(! $(".mainpage-header").position()){
            $top = $("#content-left").height();
            $("#content-main").css("min-height", $top+"px" );
        }
        
//        $(".news:first-child").next(".news-full").show();
//        $(".news:first-child").hide();

        $(".news:first").click();

        powiadomZnajomego();
        cardRadioPos();
        addActiveTabs();
        formsWhiteSpace();
        sifr();
}

function docReady(){
    titlePatch();
    logosPatch();
    formRequiredFields("#conferenceForm");
    formRequiredFields("#newsletter_table");
    conferenceLogosPatch();
    subpagegrayConfLogosPatch();
}

function formRequiredFieldsHelp(){

}

function subpagegrayConfLogosPatch(){
    
            var maxW = 140;
            var maxH = 105;
            var elem = $("#header-gray-logo img");
            if( (elem.width() / maxW) > (elem.height() / maxH)){
                elem.width(maxW);
            }else{
                elem.height(maxH);
            }

            elem.css("margin-left", ((183 - elem.width()) / 2) + "px").css("margin-top", ((139 - elem.height()) / 2) + "px");
}

function mainPageConfLogosPatch() {
    $(".mainpage-header-st:visible .mainpage-header-logo img").each(
        function(){
            var maxW = 140;
            var maxH = 105;
            var elem = $(this);
            if( (elem.width() / maxW) > (elem.height() / maxH)){
                elem.width(maxW);
            }else{
                elem.height(maxH);
            }

            elem.css("margin-left", ((174 - elem.width()) / 2) + "px").css("margin-top", ((135 - elem.height()) / 2) + "px");
        }
    );
}

function conferenceLogosPatch() {
    $(".logo img").each(
        function(){
            if($(this).width() > $(this).height()){
                $(this).css("width", "130px");
				var height = $(this).height() + 25;
				$(this).parent().parent().parent().css("height", height + "px");
            }else{
                $(this).css("height", "105px");
				$(this).parent().parent().parent().css("height", "130px");
            }
        }
    );
}

function attachOnBlur(element) {
	var sel = '#conferenceForm';
	
	element.onblur = function() {			
		if($(this).attr("value") != ''){
			$(this).removeClass("required");
		}else{
			if(!$(this).hasClass("required")){
				$(this).addClass("required");
			}
		}
		if($("*.required").length == 0){
			$(sel + " input.submit").removeAttr("disabled");
			$(sel + " input.submit").removeClass("disabled");
		}else{
			$(sel + " input.submit").attr("disabled", "disabled");
			$(sel + " input.submit").addClass("disabled");
		}	
	}
}

function formRequiredFields(sel){

    $(sel + " input.submit").attr("disabled", "disabled");
    $(sel + " input.submit").addClass("disabled");

    $("input.submit.disabled").click(
        function(){
            $("*.[required='1']").addClass("required");

            $("*.required").each(
                function() {
                    if($(this).attr("value") != ''){
                        $(this).removeClass("required");
                    }
                }
            );

            if($("*.required").length == 0){
                $(sel + " input.submit").removeAttr("disabled");
                $(sel + " input.submit").removeClass("disabled");
            }

        }
    );

	var requiredFields = ['imie','nazwisko','firma','ulica',
						  'miasto','telefon',/*'email'*/,'nip'];
	
	for(var i in requiredFields) {
		var element = document.getElementById(requiredFields[i]);
		
		if (element != undefined) {
			attachOnBlur(element);
		}
	}
	/*
	window.emailField.onblur = function() {	
		alert(this.value);	
		if($(this).attr("value") != ''){
			$(this).removeClass("required");
		}else{
			if(!$(this).hasClass("required")){
				$(this).addClass("required");
			}
		}
		if($("*.required").length == 0){
			$(sel + " input.submit").removeAttr("disabled");
			$(sel + " input.submit").removeClass("disabled");
		}else{
			$(sel + " input.submit").attr("disabled", "disabled");
			$(sel + " input.submit").addClass("disabled");
		}	
	}*/

	
	// Ponizszy kod nie dziala w Chrome i Opera, zamieniono go powyzszym
/*    $("*.[required='1']").blur(
        function(){
            if($(this).attr("value") != ''){
                $(this).removeClass("required");	
            }else{
                if(!$(this).hasClass("required")){
                    $(this).addClass("required");
                }
            }
            if($("*.required").length == 0){
                $(sel + " input.submit").removeAttr("disabled");
                $(sel + " input.submit").removeClass("disabled");
            }else{
                $(sel + " input.submit").attr("disabled", "disabled");
                $(sel + " input.submit").addClass("disabled");
            }
        }
    );*/

//    $("*.[required='1']").blur(
//        function(){
//            $(this).trigger("keypress");
//        }
//    );
}

function logosPatch(){

//    $(".smartGallery .box-gall").each(
//        function(){
//            width = $(this).width();
//            left = 100 - Math.round(width/2);
//            $(this).css("margin-left", left+"px");
//
//            height = $(this).height();
//            top = 60 - Math.round(height/2);
//            $(this).css("margin-top", top+"px");
//        }
//    );
}

function logoPatch(obj){

//    console.debug(obj);

//    width = obj.find("img").width();
//    if (width > 200){
//        width = 200;
//    }
//    obj.find("img").attr("width", width);
//    left = 100 - Math.round(width/2);
//    obj.css("margin-left", left+"px");
    
//    height = obj.find("img").height();
//    console.debug(height);
//    top = 60 - Math.round(height/2);
//    obj.find("img").css("margin-top", top+"px");
}

function titlePatch(){
    if($("#menu-gray-header span").height() == "16"){
        $("#menu-gray-header span").css("padding-top", "8px");
    }
}

function addActiveTabs(){
    $(".mainpage-header-st").each(
        function(index){
            $(this).find(".mainpage-header-controls-elements li:nth-child(" + (index+1) + ") a").addClass("active");
        }
      );
}

function checkRadioPos(){
        if($("#credit-card:checked").length){
            $("#card-no").removeAttr("disabled");
            $("#card-date").removeAttr("disabled");

            $("#card-no").parent().parent().slideDown();
            $("#card-date").parent().parent().slideDown();
        }else{
            $("#card-no").attr("disabled", "disabled");
            $("#card-date").attr("disabled", "disabled");

            $("#card-no").parent().parent().slideUp();
            $("#card-date").parent().parent().slideUp();
        }
    }

function cardRadioPos(){
    checkRadioPos();
    $("*:radio").click(
        function(){
            checkRadioPos();
        }
    );
}

function loginMore(){
    $("#login-more-scroll").click(
        function(){
            $("#login-more").slideDown();
            $("#login-more-scroll").slideUp();
        }
    );
}

function formsWhiteSpace() {
    if ($("p.errors").html() == ''){
        //$("p.errors").hide();
    }
}

function patchActiveMenuElement() {
    if($(".menu-element.orange.active").position()){
        $top = $(".menu-element.orange.active").position().top+33;
        $("#menu-element-patcher-active").css("top", $top+"px").show();
    }

    if($(".menu-element.gray.active").position()){
        //alert($(".menu-element.gray.active").height());
        $top = $(".menu-element.gray.active").position().top+$(".menu-element.gray.active").height()+15;
        $("#menu-gray-element-patcher-active").css("top", $top+"px").show();
    }

    if($("#content-left div.active div").position()){
        $top = $("#content-left div.active div").position().top + $("#content-left div.active div").height() - 1;
        $("#menu-element-patcher-active").css("top", $top+"px").show();
    }

    if($("#content-left div div .menu-element.active").position()){
        $top = $("#content-left div div .menu-element.orange:last").position().top + $("#content-left div div .menu-element.orange:last").height() +15;
        $("#menu-element-patcher-active").css("top", $top+"px").show();
        //alert($("#content-left div div .menu-element.orange:last").html());
    }
}


function powiadomZnajomego() {
    $("a#polec").toggle(
		function()
			{
                        $("#polec_window").fadeIn('slow');
                        $.ajax(
                                {
                                type:		"GET",
                                url:		$(this).attr('rel'),
                                beforeSend:	function(){$("#polec_window").html('<img src="img/ajax-loader.gif" />')},
                                success:	function($msg) {$("#polec_window").html('<a class="zamknij" href="#zamknij">X</a>'+$msg);}
                                });
                        return false;

			},
		function()
			{
			$("#polec_window").fadeOut('fast');
			return false;

			});
		$("#polec_window a.zamknij").live('click',function()
			{
			$("a#polec").trigger('click');
			return false;
			});

}



function addEvents() {
    $(".menu-element.orange").hover(
      function () {
          $(this).addClass("over");

          $top = $(this).position().top+33;
          $("#menu-element-patcher").css("top", $top+"px").show();
      },
      function () {
          $(this).removeClass("over");
          $("#menu-element-patcher").hide();
      }
    );

    $("div.active .menu-element.orange").hover(
      function () {
          $(this).addClass("over");
          $("#menu-element-patcher").hide();
      },
      function () {
          $(this).removeClass("over");
          $("#menu-element-patcher").hide();
      }
    );

    $(".menu-element.gray").hover(
      function () {
          $(this).addClass("over");
          $top = $(this).position().top +$(this).height()+15;
          $("#menu-gray-element-patcher").css("top", $top+"px").show();
      },
      function () {
          $(this).removeClass("over");
          $("#menu-gray-element-patcher").hide();
      }
    );

    $(".news").hover(
      function () {
          $(this).addClass("over");
      },
      function () {
          $(this).removeClass("over");
      }
    );    

    $(".news").click(
      function () {
          $(".news").slideDown("fast");
          $(this).slideUp("fast");

          $(".news-full").slideUp("fast");
          $(this).next(".news-full").slideDown("fast");
      }
    );


}

function setActiveHeader($obj, $nth){
    clearInterval($txs);
    if($stop == 0){
        $txs=setInterval('headerAutoChange()', 4000);
    }

    if ($nth > 0){
        $obj.parent().find(".mainpage-header-st").fadeOut();
        $obj.parent().find(".mainpage-header-st:nth-child(" + $nth + ")").fadeIn();
    }
    
    if ($nth == "next"){
        if ($obj.html() == $obj.parent().find(".mainpage-header-st:last-child").html()){
            $obj.parent().find(".mainpage-header-st:first-child").fadeIn();
        }else{
            $obj.next().fadeIn();
        }
        $obj.fadeOut();
        
    }else if ($nth == "prev"){
        if ($obj.html() == $obj.parent().find(".mainpage-header-st:first-child").html()){
            $obj.parent().find(".mainpage-header-st:last-child").fadeIn();
        }else{
            $obj.prev().fadeIn();
        }
        $obj.fadeOut();
    }
    mainPageConfLogosPatch();
    sifrMainHeader();
    
}

function headerAutoChange() {
    $(".mainpage-header-st:visible .mainpage-header-arrow.right a").trigger('dblclick');
}

function mainHeaderEvents() {

    l = $(".mainpage-header-controls-elements").length * 25;

    $(".mainpage-header-controls-middle-line").css("width", (310-l) + "px");

    $(".mainpage-header-st:first-child").show();
    sifrMainHeader();

    $(".mainpage-header-arrow.left a").click(
        function() {
            $stop = 1;
            setActiveHeader($(".mainpage-header-st:visible"), "prev");
        }
    );

    $(".mainpage-header-arrow.right a").click(
        function() {
            $stop = 1;
            setActiveHeader($(".mainpage-header-st:visible"), "next");
        }
    );

    $(".mainpage-header-arrow.right a").dblclick(
        function() {
            setActiveHeader($(".mainpage-header-st:visible"), "next");
        }
    );

    $(".mainpage-header-controls-elements li").click(
        function() {
            $stop = 1;
            a = $(".mainpage-header-st:visible li").index($(this));
            headerindex = a + 1;
            setActiveHeader($(".mainpage-header-st:visible"), headerindex);
        }
    );

}





//$smi = 1;
$sminterval = 3000;
//$smj = 0;
$elementsClass = ".box-gall";
$container = ".smartGallery";
$fadeSpeed = 1000;
$smt = 0;
$pause = 0;
$galleryCount = 0;

function smartGalleryStart(){
    $($container+" "+$elementsClass+":first-child").fadeIn($fadeSpeed);
//    $smi = $($container+" "+$elementsClass).size();
    $smt=setInterval('smartGalleryAutoChange()',$sminterval);
    //smartGalleryBoxClick();
}

function smartGalleryBoxClick() {
      $($container+" "+$elementsClass).dblclick(
        function () {
            clearInterval($smt);
            $smt=setInterval('smartGalleryAutoChange()',$sminterval);
            if ($(this).html() == $($container+" "+$elementsClass+":first-child").html()){
                $($container+" "+$elementsClass+":last-child").fadeOut($fadeSpeed);
            }else{
                $(this).prev().fadeOut($fadeSpeed);
            }
            $($container+" div").fadeOut($fadeSpeed);
            $(this).fadeIn($fadeSpeed);
        });
}

function switchImg($obj){
    //$obj.parent().find($elementsClass+":last-child").html()
        logosPatch();
    var elem;
    if($obj.parent().find($elementsClass+":first-child").html() != $obj.parent().find($elementsClass+":last-child").html()){
        if ($obj.html() == $obj.parent().find($elementsClass+":last-child").html()){
            elem = $obj.parent().find($elementsClass+":first-child");
        }else{
            elem = $obj.next();
        }
        elem.show();
        //logoPatch(elem);
        elem.hide();
        elem.fadeIn($fadeSpeed);


        $obj.fadeOut($fadeSpeed);
    }
}

function smartGalleryAutoChange(){
      $($elementsClass+":visible").each(
        function(){
            switchImg($(this));
        }
      );
}


function sifrMainHeader(){
    font = futura_shadow;

    sIFR.activate(font);

    sIFR.replace(font, {
      selector: '.mainpage-header-title-text',
      wmode: 'transparent',
                                            sharpness : 100,
                                                    thickness : 40,
      css: '.sIFR-root {color:#ffffff; font-size:27px; font-weight: normal; padding-top:0; letter-spacing:0; cursor: pointer; text-align: center; text-transform: uppercase;}'
    });
}

function sifr() {

    font = arial;

    sIFR.activate(font);

    //sIFR.replace(font, {
    //  selector: '.mainpage-header-longtitle',
    //  wmode: 'transparent',
    //					sharpness : 100,
    //			 			thickness : 40,
    //  css: '.sIFR-root {color:#ffffff; font-size:15px; font-weight: bold; padding-top:0; letter-spacing:0; text-align: left;}'
    //});

    sIFR.replace(font, {
      selector: '.title-orange, h1',
      wmode: 'transparent',
                                            sharpness : 100,
                                                    thickness : 40,
      css: '.sIFR-root {color:#fbaa28; font-size:14px; margin-top: -2px; font-weight: bold; padding-top:0; letter-spacing:0; text-align: left; text-transform: uppercase; background-position: 0 2px;}'
    });

    sIFR.replace(font, {
      selector: 'h2',
      wmode: 'transparent',
                                            sharpness : 100,
                                                    thickness : 40,
      css: '.sIFR-root {color:#004e74; font-size:13px; font-weight: bold; padding:0; margin: 0px; letter-spacing:0; text-align: left; text-transform: uppercase; background-position: 0 2px;}'
    });


//font = arial_shadow;
//
//sIFR.activate(font);
//
//sIFR.replace(font, {
//  selector: '.mainpage-header-controls-elements ul li a',
//  wmode: 'transparent',
//					sharpness : 100,
//			 			thickness : 40,
//  css: '.sIFR-root {color:#ffffff; font-size:12px; font-weight: normal; padding-top:0; letter-spacing:0; text-align: left; cursor: pointer;}'
//});

}

