
$j(document).ready(function() {
    setShowBoxesLinkState();
    vhiOnHover();
    
    $j('.nyro-delete').nyroModal({
        modal   : true,
        type    : 'body',
        bgColor : '#ffffff'
    });
    
    $j(".historia, .statystyki, .vitalnewesela, .galeria, .trocheonas, #box_comments,.last-blogs").hover(
      function () {
        $j(this).addClass("hover");
      }, 
      function () {
        $j(this).removeClass("hover");
      }
    );
    

    

    setInitialLayout();
    
    $j('#moreButton').click(showMoreInfo);
    $j('#lessButton').click(showLessInfo);
    
    $j(".statusbar").hover(
      function () {
        $j("#"+$j(this).attr("rel")).fadeIn('fast');
      }, 
      function () {
        $j('.topDashedBorder .statustext').fadeOut("fast");
      }
    );
    
   if(show_clock){
        weddingTimer();
        setInterval("weddingTimer()",1000);
        $j('#weddingTimer').show();
        if(is_before_wedding){
            $j('#weddingTimerBefore').show();
        } else {
            $j('#weddingTimerAfter').show();
        }
    } 
});

vhiOnHover = function() {
        $j(".vhi").hover(
      function () {
        $j(this).addClass("vhi-hover");
      }, 
      function () {
        $j(this).removeClass("vhi-hover");
      }
    );
}


sendCongratulations = function(hash) {

    var hash2 = 'empty';

    try{
        hash2 = md5(screen.width + 'x' + screen.height + '/' + screen.colorDepth + '; ' + navigator.userAgent);
    } catch(e){ }

        blockForm(hash);
        
        $j.getJSON("/profiles/send_congratulations.json", {id : hash, md5hash: hash2},
        function(data) {
            $j('#congratulations_'+hash).text(data.congratulations)
            unblockForm(hash);
            if(data.warning == 0){
            	showMsg('Właśnie dołączyłeś się do życzeń. Dziękujemy!',hash);
            }else if(data.warning == 1){
            	showMsg('Nie, nie, nie... Tylko znajomi mogą Wam pogratulować ;)',hash);
            } else {
                showMsg('Już składałeś gratulacje tej parze.',hash);
            }
        });

    return false;
}

notSendCongratulations = function(hash) {
//    showMsg('Już składałeś gratulacje tej parze.',hash);
}

function removeElement(div_id){

    var done = false;

    //delete rectangularBox
    if(div_id == 'relationship_history' || div_id == 'about_us' || div_id == 'about_stat' || div_id == 'box_blog'){
        if(isDisplayed('magicBox') && !countBoxesIsEven()){
            //slide magicBox center
            $j('#'+div_id).fadeOut('slow', function(){
                $j('#magicBox').css('margin-left','6px').animate({marginLeft:'185px'});
            });
            done = true;
        }
        if(isDisplayed('magicBox') && countBoxesIsEven()){
            //slide magicBox left
            $j('#'+div_id).fadeOut('slow', function(){
                $j('#magicBox').animate({marginLeft:'5px'});
            });
            done = true;
        }
    }

    //jesli nie miesci sie w warunkach
    if(!done){
        $j('#'+div_id).fadeOut('slow');
    }

    if(div_id == 'box_galleries'){
        $j("#gotoGalleriesLink").hide();
    }

    setTimeout("setShowBoxesLinkState();",2000);
}

function setInitialLayout(){

//     if(isNotEmptyMagicBox() && !isDisplayed('magicBox')){
//         $j('#magicBox').fadeIn('fast');
//     }
//     if(!isNotEmptyMagicBox() && isDisplayed('magicBox')){
//         $j('#magicBox').fadeOut('fast');
//     }

    if(isDisplayed('magicBox') && countBoxesIsEven()){
        $j('#magicBox').animate({marginLeft:'185px'});
    }
    if(isDisplayed('magicBox') && !countBoxesIsEven()){
        $j('#magicBox').animate({marginLeft:'5px'});
    }
}

    function countBoxesIsEven(){
        var even = true;
        if(isDisplayed('relationship_history')){
            even = false;
        }
        if(isDisplayed('about_us')){
            even = !even;
        }
        if(isDisplayed('box_stats')){
            even = !even;
        }
        if(isDisplayed('box_blog')){
            even = !even;
        }
        return even;
    }

//$j("#relationship_history")
//$j("#magicBox")
//$j("#about_us")
//$j("#box_stats")



function isNotEmptyMagicBox(){
    if($j('#magicBox div div').text() != ""){
        return true;
    } else {
        return false;
    }
}

function isDisplayed(DivId){
    if($j('#'+DivId).length == 0 || $j('#'+DivId).css('display') == "none"){
        return false;
    } else {
        return true;
    }
}

function setShowBoxesLinkState(){
    if(isAnyBoxHidden()){
        $j('#dodajElementy').show('fast');
    } else {
        $j('#dodajElementy').hide('fast');
    }
}

function isAnyBoxHidden(){
    var ret = false;
    ret = ret || !isDisplayed('relationship_history');
    ret = ret || !isDisplayed('about_us');
    ret = ret || !isDisplayed('box_stats');
    ret = ret || !isDisplayed('box_virtual_weddings');
    ret = ret || !isDisplayed('box_galleries');
    ret = ret || !isDisplayed('box_comments');
    ret = ret || !isDisplayed('box_blog');
    return ret;
}

function showLessInfo(){
    $j('#profileMoreInfo').fadeOut('fast');
    $j('#profileLessInfo').fadeIn('more');
}
function showMoreInfo(){
    $j('#profileLessInfo').fadeOut('fast');
    $j('#profileMoreInfo').fadeIn('more');
}

// Zablokowanie calego formularza na czas wywolania AJAX
blockForm = function(hash) {
    $j('#'+hash).block({
        message: '<img src="/img/loading.gif" />', 
        css: { 
            border: '0px',
            backgroundColor: 'transparent' 
        },
        overlayCss: {
            opacity: '0.7'
        }
    });
}

// Odblokowanie calego formularza po zakonczeniu wywolania AJAX
unblockForm = function(hash) {
        $j('#'+hash).unblock();      
}

// Wyswietlenie komunikatu
showMsg = function(msg, hash) {
    $j('#ajaxMsg_'+hash).html(msg).fadeIn('fast');
    setTimeout(function() { $j('#ajaxMsg_'+hash).fadeOut('normal'); },4000);
}



weddingTimer = function(){
    
    var seconds = wedding_time_diff;
    var minutes = Math.floor(seconds/60);
    var hours = Math.floor(minutes/60);
    var days = Math.floor(hours/24);

    seconds = seconds % 60;
    minutes = minutes % 60;
    hours = hours % 24;
    
    if(is_before_wedding){
        wedding_time_diff -= 1;
        if(wedding_time_diff <= 0){
            is_before_wedding = false;
            wedding_time_diff = 0;
            $j('#weddingTimerBefore').hide();
            $j('#weddingTimerAfter').show();
        }
    } else {
        wedding_time_diff += 1;
    }

    if(hours<10) { hours = "0" + hours }
    if(minutes<10) { minutes = "0" + minutes }
    if(seconds<10) { seconds = "0" + seconds }

    $j('#weddingTimer').html("<span class=\"timer_date\">" + days + "</span> <span class=\"timer_time\">" + hours + " : " + minutes + " : " + seconds + "</span>");
    
}


