/* use to load functions after loading page - jquery util*/
$(document).ready(function() {
    //$("#homePageTitle").remove();
  
    // przypisanie id wszystkim news
    iBlogId = 1;
  
    $('.blogRow').each(function() {
        // pokazywanie tylko 3 pierwszych wpisow bloga
        if (iBlogId > 3) {
            $(this).hide();    
        }
            
        $(this).attr('id', 'blog_'+iBlogId);
        iBlogId++;
    });
    
    // ukrywanie przewijania w gore po zaladowaniu strony
    var tmpId = $('.blogRow:visible:first').attr('id');
    if(tmpId){
        iFirstVisibleId = parseInt(tmpId.match(/\d+/));
    }
    else{
        iFirstVisibleId = null;
    }
    var tmp2Id = $('.blogRow:first').attr('id');
    if(tmp2Id){
        iFirstId = parseInt(tmp2Id.match(/\d+/));
    }
    else{
        iFirstId = null;
    }
       
    if (iFirstVisibleId == iFirstId) {
        $('#blog_up').hide();
        $('#blog_down').before('<img style="margin-right: 4px;" id="blogUpNoHover" src="images/index/blog_up.png" />');

    }
    
    $('#blog_down').click(function() {
        // pobieranie id ostatniego widoczengo newsa
        idLast = $('.blogRow:visible:last').attr('id').match(/\d+/);
        
        // ukrwywanie widocznych newsow
        $('.blogRow:visible').each(function() {
            $(this).hide();            
        });
                
        // pokazywanie kolejnych 3 newsow w gore wiec wiecej niz ten co teraz
        for (i = parseInt(idLast)+1; i <= parseInt(idLast)+3; i++) {
            $('#blog_'+i).show();
        }
        
        // ukrywanie dalszego przewijania w dol jesli jest ostatnia strona
        iLastVisibleId = parseInt($('.blogRow:visible:last').attr('id').match(/\d+/));
        iLastId = parseInt($('.blogRow:last').attr('id').match(/\d+/));
        
        if (iLastVisibleId == iLastId) {
            $('#blog_down').hide();   
            $('#blog_up').after('<img style="margin-left: 3px;" id="blogDownNoHover" src="images/index/blog_down.png" />');
        }
        
        // pokazywanie przewijania w gore jesli nie jest pierwsza strona
        iFirstVisibleId = parseInt($('.blogRow:visible:first').attr('id').match(/\d+/));
        iFirstId = parseInt($('.blogRow:first').attr('id').match(/\d+/));
        
        if (iFirstVisibleId !== iFirstId) {
            $('#blog_up').show();
            $('#blogUpNoHover').remove();
        }
        
        return false;
    });
    
    $('#blog_up').click(function() {
        // pobieranie id pierwszego widoczengo newsa
        idFirst = parseInt($('.blogRow:visible:first').attr('id').match(/\d+/));
        
        // ukrywanie widocznych newsow
        $('.blogRow:visible').each(function() {
            $(this).hide();
        });
        
        // pokazywanie poprzednich 3 newsow w dol wiec mniej niz ten co teraz
        for (i = idFirst-1; i >= idFirst-3; i--) {
            $('#blog_'+i).show();    
        }
        
        // pokazywanie przewijania w dol jesli juz nie jest ostatnia strona
        iLastVisibleId = parseInt($('.blogRow:visible:last').attr('id').match(/\d+/));
        iLastId = parseInt($('.blogRow:last').attr('id').match(/\d+/));
        
        if (iLastVisibleId !== iLastId) {
            $('#blog_down').show();    
            $('#blogDownNoHover').remove();
        }
        
        // ukrywanie dalszego przewijania w gore jesli jest pierwsza strona
        iFirstVisibleId = parseInt($('.blogRow:visible:first').attr('id').match(/\d+/));
        iFirstId = parseInt($('.blogRow:first').attr('id').match(/\d+/));
        
        if (iFirstVisibleId == iFirstId) {
            $('#blog_up').hide();
            $('#blog_down').before('<img style="margin-right: 4px;" id="blogUpNoHover" src="images/index/blog_up.png" />');
        }
        
        return false;
    });
  
  
    showHideRealisations('internet', 0);
    showHideRealisations('poligrafia', 0);
    showHideRealisations('inne', 0);
    $('.lightbox').lightbox();
	
    $('div.selectCont').bind("mouseleave", function() {
        $(this).parent().children('div.selectCont').hide();
        $(this).parent().children('div.selectContWind').show();
    });
    $('textarea').bind("mouseover", function() {
        $('div.selectCont').hide();
        $('div.selectContWind').show();
    });
    $('div.selectContWind').bind("mouseenter", function() {
        $(this).parent().children('div.selectContWind').hide();
        $(this).parent().children('div.selectCont').show();
    });
       

    $('a.mails').each(function(i) {
        var email = $(this).html();
        var replaced = email.replace(/ymaspy/,".");
        $(this).html(replaced);
        $(this).attr('href', 'mailto:'+replaced);
    });
        
    startGallery();
        
    $('img.leftMenuImg').parent().bind({
        click: function(e) {
            var flag = false;
            if($(this).hasClass('leftIcoHoover')) {
                flag = true;
            }
            $('div').removeClass('leftIcoHoover');
            $('div.leftMenuHoover').hide();
            e.stopPropagation();
            if(flag == false) {
                $(this).addClass('leftIcoHoover');
                $(this).next('ul').children('li').children('div.leftMenuHoover').show();
            }
        }
    });
        
    $('div.leftMenuHoover').bind({
        click: function(e) {
            e.stopPropagation();
        }
    })
        
    $('html').bind('click', function(){
        $('div').removeClass('leftIcoHoover');
        $('div.leftMenuHoover').hide();
    });
        
    $('div.subIco').bind({
        mouseenter: function() {
            $(this).addClass('subIcoWithBg');
        },
        mouseleave: function() {
            $(this).removeClass('subIcoWithBg');
        }
    });
        
    $('div.subIco').bind({
        mouseenter: function(){
            $(this).children('div.icoMenu').addClass('icoMenuBg');
        },
        mouseleave: function() {
            $(this).children('div.icoMenu').removeClass('icoMenuBg');
        }
    });
        
    //doładuj wszystkie obrazki z sub-menu
    $('div.subIcoBg').each(function(){
        $(this).css('background-image',"url('images/index/menu/"+$(this).attr('bg')+".png')");
    });

    $("#mainLogo").load("/index/index/hplogo", function(){
        $('#mainLogo .lightbox').lightbox();    
    });
    $("#mainIdentity").load("/index/index/hpident", function(){
        $('#mainIdentity .lightbox').lightbox();    
    });
    $("#mainPrint").load("/index/index/hppoli", function(){
        $('#mainPrint .lightbox').lightbox();    
    });
    $("#mainGadgets").load("/index/index/hpgadz", function(){
        $('#mainGadgets .lightbox').lightbox();    
    });
    $("#zaufaliNam").load('index/index/zaufali');
        
    $('ul#ulLeftMenu').removeClass('displayNoneI');
        
    $('.scrollToForm').click(function() {
        scrollWin('ToFormGoing');
        $("#imie_nazwisko").focus();
        return false;
    });
    $('.scrollToCalcPen').click(function() {
        scrollWin('ToCalcGoing');
        $("#dlugopisIlosc").focus();
        return false;
    });
        
    //wywołanie popup dla briefu
    $("#briefPopup").mouseover(function(){
        $("#briefPopup .popupItemContent").show();
    }).mouseout(function(){
        $("#briefPopup .popupItemContent").hide();
    });
}
);
   


/* @START galeria na str glownej */
function startGallery() {
    //wstepne wyswietlanie galerii
    $('ul.realisations').hide(); //chowaj wszystkie galerie
    $('ul.realisations:first').show(); //pokaz pierwsza galerie
    
    var ulid = $('ul.realisations:visible').attr('id');
    $('ul.realisations:visible li').hide(); //chowaj wszystkie li z widocznej galerii
    $('ul.realisations:visible li:first').show(); //pokaz pierwsze li z widocznej galerii
    
    realisationDesc();
    mainNavigation();
    
}

function realisationDesc() {
    $('div#mainRealisations').bind('mouseover', function() {
        $('ul.realisations:visible li:visible').children('div.realisationDesc').fadeIn();
    });
    $('div#mainRealisations').bind('mouseleave', function() {
        $('ul.realisations:visible li:visible').children('div.realisationDesc').fadeOut();
    });
}

function mainNavigation() {
    //BIND NAWIGACJI
    $('a#main_nav_left, a#main_nav_right').bind("mouseover", function() {
        $(this).children('img').fadeIn();
    });
    $('a#main_nav_left, a#main_nav_right').bind("mouseleave", function() {
        $(this).children('img').fadeOut();
    });
    
    //AKCJE
    $('a#main_nav_left').bind('click', function(){
        if($('ul.realisations:visible li:visible').prev().html()) {
            $('ul.realisations:visible li:visible').fadeOut().prev().fadeIn();
        }
        else {
            $('ul.realisations:visible li:visible').fadeOut().parent().children('li').last().fadeIn();
        }
        $('ul.realisations:visible li:visible div.realisationDesc').show();
    });
    $('a#main_nav_right').bind('click', function(){
        if($('ul.realisations:visible li:visible').next().html()) {
            $('ul.realisations:visible li:visible').fadeOut().next().fadeIn();
        }
        else {
            $('ul.realisations:visible li:visible').fadeOut().parent().children('li').first().fadeIn();
        }
        $('ul.realisations:visible li:visible div.realisationDesc').show();
    });
}

function changeGallery(gallname) {
    //tab
    $('div#realisationsTab h2').removeClass('mainTab1_active').removeClass('mainTab2_active');
    $('h2#tab'+gallname).addClass($('h2#tab'+gallname).attr('class')+'_active');
    
    //gallery
    $('ul.realisations:visible').fadeOut();
    $('ul#main'+gallname+' li').hide();
    $('ul#main'+gallname+' li:first').show();
    $('ul#main'+gallname).fadeIn();
    
    
    
    realisationDesc();
}

/* @END galeria na str glownej */

function showHideRealisations(divRealizations, iIndex) {
    var notFadedOut = true;
    var wait = false;
    $('div#' + divRealizations + '_realizations div.realisation').each(function(index) {
        if($(this).css('opacity') != 1) {
            wait = true;
        }
    });
    if (!wait) {
        //pager
        if ($('div#' + divRealizations + '_pager div a:eq(' + iIndex + ')').hasClass('first')) {
            $('div#' + divRealizations + '_pager div a:eq(' + iIndex + ')').addClass('first_checked');
        }
        else {
            $('div#' + divRealizations + '_pager div a:eq(' + iIndex + ')').addClass('checked');
        }
        $('div#' + divRealizations + '_pager div a:lt(' + iIndex + ')').removeClass('checked').removeClass('first_checked');
        $('div#' + divRealizations + '_pager div a:gt(' + iIndex + ')').removeClass('checked').removeClass('first_checked');
		
        //content
        $('div#' + divRealizations + '_realizations div.realisationWaiting').remove();
        $('div#' + divRealizations + '_realizations div.realisation').each(function(index){
            if ($(this).css('display') == 'block') {
                $(this).fadeOut('slow', function(){
                    $('div#' + divRealizations + '_realizations div.realisation:eq(' + iIndex + ')').fadeIn('slow');
                });
                notFadedOut = false;
            }
        });
        if (notFadedOut) {
            $('div#' + divRealizations + '_realizations div.realisation:eq(' + iIndex + ')').fadeIn('slow');
        }
    }
}

function showHideSelect(id) {
    $('div#'+id+' div.selectContWind, div#'+id+' div.selectCont').toggle();
}

function insertemail(){
    $('div#kontakt_email').html('<strong>E-mail:</strong> <a href="mailto:biuro@vert.pl">biuro@vert.pl</a>');
}

function insertToHtmlSelect(obj) {
    //removes active
    $(obj).parent().parent().find('div.selectCont a.box').removeClass('active');
    $(obj).addClass('active');
    $(obj).parent().parent().find("input[type='hidden']").val($(obj).html());
    $(obj).parent().parent().find('div.selectContWind div.centerCont, div.selectCont div.centerCont').html($(obj).html());
    $(obj).parent().parent().find('div.selectContWind').show();
    $(obj).parent().parent().find('div.selectCont').hide();
}

function disableAnimation(){
    $.ajax({
        type: "GET",
        url: "http://www.vert.pl/index/index/disableanim/noreturn/true",
        data: "",
        success: function(msg){
        }
    });

}

function scrollWin(target){
    $('html, body').animate({
        scrollTop: $("#"+target).offset().top
    }, 500);
}
