$(window).load(function() {
    projects_ajax_init_after();
});


$(document).ready(function() {     
 
    
    var origContent = "";

    // hover
    $('div.news p').hover(  
        function (){            
            $(this).animate({
                backgroundColor: "#ede479"
            }, "fast");
        }
        , function() {
            $(this).animate({
                backgroundColor: "#ffffff"
            }, "fast");
        }                   
        ); 



    //filter ajax
    $('.filter select[name!=projekt]').live('change',function(){
        projects_ajax_init_before($(this));
        var obj = $(this).parents('form:first');
        var url = obj.attr('action')+"?";
        obj .find("input,select").each(function(){
            url+=""+$(this).attr("name")+"="+encodeURIComponent($(this).attr("value"))+"&";
        })
        $('#site').data('filterurl',url);
        //url+="showtemplate=false";
        obj.parents('.abox:first').load(url + " .acase", '', function(html){            
            projects_ajax_init_after();
        });
        return false;
    });
    
    //filter detail
    $('select[name=projekt]').live('change',function(){
        projects_ajax_init_before($(this));
        var url = $(this).val();
        var obj = $(this).parents('form:first');
        //url+="?&showtemplate=false";
        obj.parents('.abox:first').load(url + " .acase", '', function(html){            
            projects_ajax_init_after();
        });
        return false;
    });
    $('.projects .project').live('click',function(){
        projects_ajax_init_before($(this));
        var url = $(this).find('h2').children('a').attr('href');
        var obj = $(this).parents('.abox:first');
        //url+="?&showtemplate=false";
        obj.load(url + " .acase", '', function(html){            
            projects_ajax_init_after();
        });
        return false;
    });
    


    //ajax close
    $('.close a').live('click',function(){
        projects_ajax_init_before($(this));
        var url = $(this).attr('href');
        var obj = $(this).parents('.abox:first');
        //url+="?&showtemplate=false";
        if($('#site').data('filterurl') && $(this).parent().hasClass('reset') == false)
            url = $('#site').data('filterurl');
        obj.load(url + " .acase", '', function(html){            
            projects_ajax_init_after();
        });
        return false;
    });
    
    //media ajax
    $('.media-menu a').live('click',function(){
        var url = $(this).attr('href');
        var obj = $(this).parents('.abox:first');
        //url+="?&showtemplate=false";
        obj.load(url + " .acase", '', function(html){            
            projects_ajax_init_after();
        });
        return false;
    });
    
});

function projects_ajax_init_after(){
   
    $('.projects .project').hover(  
        function (){ 
            $(this).animate({
                backgroundColor: "#534741"
            }, "fast");
  		 
            $(this).children("span.perex").fadeIn("fast");
        }
        , function() {	          		  
            $(this).animate({
                backgroundColor: $(this).data('bg')
            }, "fast"); 
        	
            $(this).children("span.perex").fadeOut("fast");       
        }                   
        );
   
    $('.flexslider').flexslider({
        manualControls:'.cleaner ul li a',
        controlNav: true,     
        slideshow: false, 
        directionNav: true,        
        start: function(slider) {
        
            $(".prev").height($("ul.slides li:first-child div img").attr("height"));
            $(".next").height($("ul.slides li:first-child div img").attr("height"));
            
            $(".prev").parent().height($("ul.slides li:first-child div img").attr("height"));
            $(".next").parent().height($("ul.slides li:first-child div img").attr("height"));
            
             $(".prev").parent().parent().height($("ul.slides li:first-child div img").attr("height"));
            $(".next").parent().parent().height($("ul.slides li:first-child div img").attr("height"));
        
            $('.right').click(function(e){
                e.preventDefault();
                slider.flexAnimate(slider.getTarget("next"));
            });
            $('.prev').click(function(e){
                e.preventDefault();
                slider.flexAnimate(slider.getTarget("prev"));
            });
           
        },
        after: function(slider) {
          
            $("#flexiNavigator li a").removeClass("active");
            
            $(".prev").height($("ul.slides li:nth-child("+slider.currentSlide+") div img").attr("height"));
            $(".next").height($("ul.slides li:nth-child("+slider.currentSlide+") div img").attr("height"));
            
            $(".prev").parent().height($("ul.slides li:nth-child("+slider.currentSlide+") div img").attr("height"));
            $(".next").parent().height($("ul.slides li:nth-child("+slider.currentSlide+") div img").attr("height"));
            
            $(".prev").parent().parent().height($("ul.slides li:nth-child("+slider.currentSlide+") div img").attr("height"));
            $(".next").parent().parent().height($("ul.slides li:nth-child("+slider.currentSlide+") div img").attr("height"));
            
            $("#flexiNavigator li:eq("+slider.currentSlide+") a").addClass("active");
        }
    });
    
    $('#carousel').elastislide({
        imageW 	: 115,
        minItems	: 5,
        margin		: 0,	// image margin right
        border		: 0,	// image border,
        onClick     : function(e) { 
            $(e).addClass('current');
        }
    });
    $('form').jqTransform();
}
function projects_ajax_init_before(obj){
    obj.parents('.acase:first').fadeTo(200,0.7,function(){
        obj.parent().append("<div class='loading'>&nbsp;</div>");
    })
    
    
}

