/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


$(document).ready(function(){


    $('a.gallery').lightbox();
    $('div#step1').find('span.category-step').mouseenter(function(){
        $(this).addClass('active');

    });
    $('div#step1').find('span.category-step').mouseout(function(){
        $(this).removeClass('active');

    });

      Cufon.replace('.search-cufon',{
            hover:true
      });
    Cufon.replace('.cufon', {
        hover: true
    });
 	
    Cufon.replace('ul.cuffon > li > a > span', {
        hover: true
    });
    $('ul.carousel-pagination > li').click(function(){

        position = $(this).attr('rel');
        $(this).parent().find('li').removeClass('on');
        $(this).addClass('on');

        $(this).parents('div').find('ul.jcarousel-list').jcarousel('scroll',parseInt(position));

    });


    $('input#search-input').click(function(){
        $(this).val('');


    });
	

    $('div#search-box-content').searchbox();

    $('div.popular > ul.carousell').jcarousel({
        visible:4
    });


    $('div.big-box > ul.carousel-main-big').jcarousel({


        auto: 2,
        scroll:1,
        wrap: 'last',
        initCallback: mycarousel_initCallback,
        itemFirstInCallback : {
            onBeforeAnimation: carousel_pagination
           
        }



    });

    $('div.new-products-content >ul.carousel').jcarousel({



        scroll:1,
        visible:1

      




    });

    $('div#step3').find('ul.carousel').jcarousel({
        scroll:1,
        visible:1,
        wrap:'last',
        itemFirstInCallback : {
            onBeforeAnimation:make_link
        }
    });



    $('div#fix-category-select').find('input[name="fix-category"]').change(function(){
        val = $(this).val();


        if(val=='wszystkie'){
            $('div#products-list').find('div.product').show();

        }
        else{
            $('div#products-list').find('div.product').hide();
            $('div#products-list').find('div[rel="'+val+'"]').show();
        }



    });



    $('div.big-box >div> ul.product-carousel').jcarousel({



        scroll:1,
        visible:1,
    
        initCallback: mycarousel_initCallback,
        itemFirstInCallback : {
            onBeforeAnimation: change_header           
        }



    });

    /*$('div.services-content > ul.services-carousel').jcarousel({



      scroll:1,
      visible:1,

      initCallback: mycarousel_initCallback




});*/

    $('ul.baner-carousel').jcarousel({
        scroll:1,
        visible:1,
        wrap:'last',
        auto: 3,
        itemFirstInCallback : {
            onBeforeAnimation: carousel_pagination
           
        }


    });
    function isValidEmail(strEmail){
    
        var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
        // strEmail = document.forms[0].email.value;

        // search email text for regular exp matches
        if(reg.test(strEmail) == false) {
     
            return false;
        }
        return true;
    }

    $('form#PageContactForm').submit(function(){
        valid = true ;
        if($('#PageName').val()==''){
            $('#PageName').css('border-color','red');
            valid = false;
        }else{
            $('#PageName').css('border-color','#DADADC');
        }

        if(isValidEmail($('#PageMail').val())){
            $('#PageMail').css('border-color','#DADADC');
           
        }else{
           
            $('#PageMail').css('border-color','red');
            $('#PageMail').css('border-color','red');
            valid = false;
        }

        if($('#PageSubject').val()==''){
            $('#PageSubject').css('border-color','red');
            valid = false;
        }else{
            $('#PageSubject').css('border-color','#DADADC');
        }

        if($('#PageBody').val()==''){
            $('#PageBody').css('border-color','red');
            valid = false;
        } else{
            $('#PageBody').css('border-color','#DADADC');
        }

        return valid;
    });


    $('div#recipes >ul.tabs > li >a').click(function(){

        rel = $(this).attr('rel');
        clas = $(this).attr('class');
        if(clas!='on'){
            $(this).parents('ul').find('a').removeClass('on');
            $('div#recipes > div.content > div > div:visible').slideUp(function(){
                $('div#recipes > div.content> div > div#content-'+rel).slideDown('slow');
            });
        }


        $(this).addClass('on');
        Cufon.replace('div#recipes >ul.tabs > li');
    });


});


function change_header(carousel,item,idx){

    category = $(item).find('a.more').attr('rel');

    $('span.new-product-header').html(category);
    Cufon.replace('.new-product-header', {
        });

}

function make_link(carousel,item,idx){
    slug = $('div#step3').find('li[jcarouselindex='+idx+']').attr('rel');

    if(typeof(slug) == 'undefined'){
        $('a#recipe-link').attr('href','http://przepisy.pl');
        $('a#recipe-link').html('<img src="/img/backgrounds/przepisy-search-link.png" />');
    }else{	
        $('a#recipe-link').attr('href','http://przepisy.pl/przepis/'+slug);
        $('a#recipe-link').html('<img src="/img/buttons/go_to_recipe.png" />');
    }
}
function carousel_pagination_mark(carousel,item,idx){
        
}
function carousel_pagination(carousel,item,idx){
    ul =$(item).parents('div.wrap').find('ul.carousel-pagination');

    ul.find('li').removeClass('on');
    ul.find('li[rel='+idx+']').addClass('on');
  
}

function mycarousel_initCallback(carousel)
{
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};







(function($){
    var products= new Array();

    var newproducts = {};
    $.fn.extend({
        //w miejce "przenosiciel" wpisz nazwę własnej funkcji

        searchbox: function(list) {
            $.fn.searchbox.set = function(data){
                        
                products = data;
            }
            $.fn.searchbox.fill_select = function(list){
                        
                $('select#recipes-select').html('');
                //  listproducts = new Array();
                for(key in list) {

                    $('select#recipes-select').append('<option value="'+key+'">'+list[key]+'</option>')
                //  listproducts.push(list[key]);
                //  alert(products[i])
                }
                                   
            }
                
            //tu zostanie wprowadzony kod wykonywany na tablicy przesłanych elementów
            $('span.category-step').click(function(){
                category = $(this).attr('rel');
                name = $(this).html();
  
  
                $('div#step2 >  input#product-name').attr('rel',category);
                $.ajax({
                    dataType:'json',
                    url:'/recipes/getproductlist/'+category,
                    success: function(data){
                        products = data;
                        $.fn.searchbox.fill_select(products);
                        //fill_select(products);
                                   
                        $('div#step2 > p >span.category').html(name);
                        $('div#step3 > p >span.category').html(name);
                        animate('step1','step2');
                                  

 
                                   
                                     
                    }
                });
                       
            });

            $('input#product-name').keyup(function(){

                value = $(this).val();
                newproducts = {};
                     

                for(key in products) {
                          
                    if(products[key].toLowerCase().match(value.toLowerCase())){
                        newproducts[key]=products[key];
                                            
                    }

                //  alert(products[i])
                }

                $.fn.searchbox.fill_select(newproducts);

            });
            $('ul.tabs li').click(function(){

                on = $('ul.tabs').find('li.on').attr('rel');
                clicked = $(this).attr('rel');
                if(clicked<on){
                    animate('step'+on,'step'+clicked);
                }

            });

            $('span.search-back').click(function(){
                $('div#step2').find('input#product-name').val('Zacznij wpisywać nazwę...');
                 
                $.fn.searchbox.fill_select(products);
                animate($(this).parents('div').attr('id'),$(this).attr('rel'));

            });

            $('select#recipes-select').click(function(){
     
                value = $(this).val();
                getrecipes(value);
            })

            function getrecipes(value){
                      
                $('div#step3').find('div.content').load('/recipes/getrecipes/'+value,  animate('step2','step3'));

                      
				

            }

            $('input#product-name').click(function(){
                $(this).val('');
            });
            /*$('input#product-name').keyup(function(){
                        category = $(this).attr('rel');
                      
              
                 /*    $.ajax({
                              dataType:'json',
                              url:'/recipes/getproductlist/'+category+'/'+value,
                              success: function(data){
                                    products = data;

                                    fill_select();
                              }
                        });
                  });*/
    

            function animate(firstcontent,secondcontent){

                $('div#'+firstcontent).fadeOut('fast',function(){
                    $('div#'+secondcontent).show('fast',function(){
                        if(secondcontent=='step3'){
                                 
                            $('div#step3').find('ul.carousel').jcarousel({
                                scroll:1,
                                visible:1,
                                wrap:'last',
                                itemFirstInCallback : {
                                    onBeforeAnimation:make_link
                                }

    
                            });
                        }
                    });

    

                             
                    $('div#recipe-search-box').find('ul.tabs > li').removeClass('on');
                    Cufon.replace($('div#recipe-search-box').find('ul.tabs > li.'+firstcontent));
                    $('div#recipe-search-box').find('ul.tabs > li.'+secondcontent).addClass('on');
                    Cufon.replace($('div#recipe-search-box').find('ul.tabs > li.'+secondcontent));
                });
     
      
            }

 
        }


    });
})(jQuery);


