$(document).ready(function() {

	$('#fb_pages_details .slider').nivoSlider({
        effect: 'fade', // Specify sets like: 'fold,fade,sliceDown'
        slices: 15, // For slice animations
        boxCols: 8, // For box animations
        boxRows: 4, // For box animations
        animSpeed: 800, // Slide transition speed
        pauseTime: 5000, // How long each slide will show
        startSlide: 0, // Set starting Slide (0 index)
        directionNav: true, // Next & Prev navigation
        directionNavHide: true, // Only show on hover
        controlNav: true, // 1,2,3... navigation
        controlNavThumbs: false, // Use thumbnails for Control Nav
        controlNavThumbsFromRel: false, // Use image rel for thumbs
        controlNavThumbsSearch: '.jpg', // Replace this with...
        controlNavThumbsReplace: '_thumb.jpg', // ...this in thumb Image src
        keyboardNav: true, // Use left & right arrows
        pauseOnHover: true, // Stop animation while hovering
        manualAdvance: false, // Force manual transitions
        captionOpacity: 0.8, // Universal caption opacity
        prevText: 'Wstecz', // Prev directionNav text
        nextText: 'Dalej', // Next directionNav text
        randomStart: false, // Start on a random slide
        beforeChange: function(){}, // Triggers before a slide transition
        afterChange: function(){}, // Triggers after a slide transition
        slideshowEnd: function(){}, // Triggers after all slides have been shown
        lastSlide: function(){}, // Triggers when last slide is shown
        afterLoad: function(){} // Triggers when slider has loaded
    });

	$('#cards_landing_page .boxes > span').click(function(){
		if (!$(this).hasClass('active')) {
			$('#cards_landing_page .boxes > span').removeClass('active');
			var tempArray = $(this).attr('class');
			tempArray = tempArray.split('_');
			var card_number = tempArray[1];
			$('#cards_landing_page .right img').fadeOut('fast', function(){
				$('#cards_landing_page .right img').attr('src', urlBase + 'img/eko-kartka/card' + card_number + '.jpg');
				$('#cards_landing_page .right img').fadeIn('fast');
			})
			$('#cards_landing_page input#card_number').val(card_number.toString());			
			$(this).addClass('active');
			
		} else {
			$(this).removeClass('active');
			$('#cards_landing_page .right img').fadeOut('fast', function(){
				$('#cards_landing_page .right img').attr('src', urlBase + 'img/eko-kartka/price_card.jpg');
				$('#cards_landing_page .right img').fadeIn('fast');
			})
			//$('#cards_landing_page .right img').attr('src', urlBase + 'img/eko-kartka/price_card.jpg');
			$('#cards_landing_page input#card_number').val('');
		}
	});
	$('#cards_landing_page .wishes').click(function(){
		$('#cards_landing_page #card_text').val('');
		if (!$(this).hasClass('active')) {
			$('#cards_landing_page .wishes').removeClass('active');
			$(this).addClass('active');
			$('#cards_landing_page #card_text').val($(this).text());
		} else {
			$(this).removeClass('active');			
		}
	})
    //    $("#slider").easySlider({
    //        auto: true,
    //        continuous: true
    //    });
	
	$('.boxgrid.captionfull').hover(function(){
		$(".cover", this).stop().animate({top:'115px'},{queue:false,duration:320});
	}, function() {
		$(".cover", this).stop().animate({top:'150px'},{queue:false,duration:320});
	});

    $('#poligrafia').click(function(){
        $('#poligrafia_list').slideToggle('slow');
    });

    $('#clients-slidshow').cycle({
        fx:     'scrollDown',
        easing: 'bounceout',
        delay:  -3000
    });

    $('#realization-pagination li').click(function() {
        var oldPage;
        $('#realization-pagination li').each(function() { // najpierw usuwamy ktory podglad jest wyswietlany
            if($(this).attr('class')=='selected') {
                $(this).removeAttr('class');
                oldPage = parseInt($(this).html()); // pobieramy jaka strona była wybrana
            }
        });
        var iPage = parseInt($(this).html()); // ustalamy ktora strone wybrano

        if(iPage==oldPage) { // jeśli wybrano tą samą strone to nic sie nie dzieje
            return true;
        }
        else { // jesli wybrano inna strone
            $(this).addClass('selected'); // wybranej stronie nadajemy klase ze zaznaczona
            var PxMove;
            var Pixels;
            if(iPage>oldPage) { // jedziemy w góre
                Pixels = (iPage-oldPage) * 740;
                PxMove = '-='+Pixels;
            }
            else {
                Pixels = (oldPage-iPage) * 740;
                PxMove = '+='+Pixels;
            }
            $("#realizations").animate({"margin-left": PxMove}, "slow");
        }



    });

    try {
        $('#slideshow').s3Slider({
            timeOut: 5000
        });
    }
    catch (e) {

    }
    $('.form_close').click(function() {
        if($.browser.msie) {
            $('#contact_form_big').css('display','none');
            $('#contact_form_big_content').css('display','none');
        } else {
            $('#contact_form_big').fadeOut('slow');
            $('#contact_form_big_content').fadeOut('slow');
        }
    });

    $('.form_open').click(function() {
        if($.browser.msie) {
            $('#contact_form_big').css('display','block');
            $('#contact_form_big_content').css('display','block');
        } else {
            $('#contact_form_big').fadeIn('slow');
            $('#contact_form_big_content').fadeIn('slow');
        }
        $('html, body').animate({
            scrollTop: 0
        }, 'slow');
    });

    $('.captcha_answer').click(function() {
        $(this).css('color','#555555');
        $(this).attr('value','');
    });
    
    
    /*
     *  Funkcja dla ajaxowej walidacji
     *  @param Object FormId
     *  @param String DataString (string dla POST)
     *  @param String ControllerLink (Link do kontrolera)
     *  @return Bool retValue
     */
    var retValue = false;
    function AjaxValidate(FormId, DataString, ControllerLink) {
        try {
            $('.form_validation', FormId).hide();
            $.ajax({
                type: "POST",
                url: urlBase+ControllerLink,
                data: DataString,
                async: false,
                success: function(serverResponse) {
                    var valid = serverResponse.getElementsByTagName('validation');
                    var errorsCount = valid[0].getAttribute('counter');
                    if(errorsCount > 0) {
                        var mainElement = serverResponse.getElementsByTagName('error');
                        for(i = 0 ; i < mainElement.length ; ++i) {
                            var att = mainElement[i].getAttribute('id');
                            att = '#'+att+'_error';
                            $(att, FormId).html(mainElement[i].firstChild.nodeValue);
                            $(att, FormId).show();
                        }
                    } else {
                        retValue = true;
                    }
                }
            });
            return retValue;
        } catch(e) {
            alert(e);
            return false;
        }
    }

    $('#big_contact_form').submit(function() {        
        var FormId = $(this);
        var DataString = 'message=' + encodeURIComponent($('#contact-form-message').val()) +
        '&imie=' + encodeURIComponent($('#contact-form-imie').val()) +
        '&telefon=' + encodeURIComponent($('#contact-form-telefon').val()) +
        '&email=' + encodeURIComponent($('#contact-form-email').val()) +
        '&s3capcha=' + encodeURIComponent($('#big_contact_form input:radio[name=s3capcha]:checked').val()) +
		'&lang=' + encodeURIComponent($('#lang').val());		
        var ControllerLink = "index_ajax/validate_contact_form";
        return AjaxValidate(FormId, DataString, ControllerLink)
    });

    $('#contact_form2').submit(function() {
        var FormId = $(this);
        var DataString = 'message=' + encodeURIComponent($('.kontener_main #contact-form-message').val()) +
        '&imie=' + encodeURIComponent($('.kontener_main #contact-form-imie').val()) +
        '&telefon=' + encodeURIComponent($('.kontener_main #contact-form-telefon').val()) +
        '&email=' + encodeURIComponent($('.kontener_main #contact-form-email').val()) +
        '&s3capcha=' + encodeURIComponent($('.kontener_main input:radio[name=s3capcha]:checked').val()) +
		'&lang=' + encodeURIComponent($('#lang').val());
        var ControllerLink = "index_ajax/validate_contact_form";
        return AjaxValidate(FormId, DataString, ControllerLink)
    });


    $('#gall').hover(function() {
        $('#prevBtn').fadeIn('slow');
        $('#nextBtn').fadeIn('slow');
    },
    function() {
        $('#prevBtn').fadeOut('slow');
        $('#nextBtn').fadeOut('slow');
    }
    )

    $("a[rel^='lightbox']").prettyPhoto();


    $('#partnership_logos').jcarousel({
		auto : 4,
		visible: 5,
		scroll : 5,
		animation : 'slow',
		wrap : 'last'
	});

    $('.logo').mouseover(function() {
        var link = $(this).attr('src');
        var image = link.split('logos/');
        var imagelink = image[1].split('_');
        if(imagelink[1]!=undefined) {
            var imagelinkNoExt = imagelink[1].split('.');
            if(imagelinkNoExt[0]=='color') {
                imagelinkNoExt[0]='gray';
            }
            else {
                imagelinkNoExt[0]='color';
            }

            //alert(imagelink[0]+'-'+imagelink[1]+'-'+imagelink[2]);
            $(this).attr('src', urlBase+'img/logos/'+imagelink[0]+'_'+imagelinkNoExt[0]+'.'+imagelinkNoExt[1]);
        }
    });

    $('.logo').mouseout(function() {
        var link = $(this).attr('src');
        var image = link.split('logos/');
        var imagelink = image[1].split('_');
        if(imagelink[1]!=undefined) {
            var imagelinkNoExt = imagelink[1].split('.');
            if(imagelinkNoExt[0]=='color') {
                imagelinkNoExt[0]='gray';
            }
            else {
                imagelinkNoExt[0]='color';
            }

            //alert(imagelink[0]+'-'+imagelink[1]+'-'+imagelink[2]);
            $(this).attr('src', urlBase+'img/logos/'+imagelink[0]+'_'+imagelinkNoExt[0]+'.'+imagelinkNoExt[1]);
        }
    });


    //    //cufon
    //    Cufon.replace('#nav a', {
    //        hover:true
    //    });
    //    Cufon.replace('h2.cufon');
    //    Cufon.replace('h3.cufon');
    //    Cufon.replace('h4.cufon', {hover: true});
	Cufon.replace('.subbox-text');

    $('.why-olicom-btn').click(function() {
        if($('.why-olicom').css('display') == 'none') {
            $('.why-olicom').slideDown('slow');
        } else {
            $('.why-olicom').slideUp('slow');
        }
    })
    $('.how-much-btn').click(function() {
        if($('.how-much').css('display') == 'none') {
            $('.how-much').slideDown('slow');
        } else {
            $('.how-much').slideUp('slow');
        }
    });

    //    $('.why-our-hosting-btn').click(function() {
    //        if($('.why-our-hosting').css('display') == 'none') {
    //            $('.why-our-hosting').slideDown('slow');
    //        } else {
    //            $('.why-our-hosting').slideUp('slow');
    //        }
    //    });

    $('.how-long-btn').click(function() {
        if($('.how-long').css('display') == 'none') {
            $('.how-long').slideDown('slow');
        } else {
            $('.how-long').slideUp('slow');
        }
    });

    $('.webmaster-btn').click(function() {
        if($('.webmaster').css('display') == 'none') {
            $('.webmaster').slideDown('slow');
        } else {
            $('.webmaster').slideUp('slow');
        }
    });

    $('.webdesigner-btn').click(function() {
        if($('.webdesigner').css('display') == 'none') {
            $('.webdesigner').slideDown('slow');
        } else {
            $('.webdesigner').slideUp('slow');
        }
    });

    $('.sales-representative-btn').click(function() {
        if($('.sales-representative').css('display') == 'none') {
            $('.sales-representative').slideDown('slow');
        } else {
            $('.sales-representative').slideUp('slow');
        }
    });
    

    $('#nav ul > li').hover(function() {
        $('ul[class!="not_show"]', this).show();
        $('ul ul[class!="not_show"]', this).hide();
        
        $('div.submenu-1', this).show();
        $('div.submenu-2', this).show();
        $('div.submenu-3', this).show();        
    },
    function() {
        $('ul[class!="not_show"]', this).hide();
        
        $('div.submenu-1', this).hide();
        $('div.submenu-2', this).hide();
        $('div.submenu-3', this).hide();  
    });

    // drukowanie
    $('#print').click(function() {
        window.print();
        return false;
    });

    // Ladowanie prettyPhoto
    $(document).ready(function(){
        $("a[rel^='prettyPhoto']").prettyPhoto();
    });

    // wstecz
    $('.back').click(function() {
        history.go(-1);
    });

    $('#poll_form').submit(function() {
        if ($("input[name=answer]:checked").val() == undefined){
            return false;
        }
        else {
            var dataString = 'answer=' + $("input[name=answer]:checked").val() + 'question=' + $("#pool_question_id").val();
            $.ajax({
                type: "POST",
                url: urlBase+"polls/add_vote/",
                data: dataString,
                async: true,
                success: function(serverResponse) {
                    $('#answers').fadeOut();
                    $('#answers').html(serverResponse);
                    $('#answers').css('text-align','center');
                    $('#answers').css('padding-bottom','15px');
                    $('#answers').fadeIn();
                }
            });
        }
        return false
    });

    $('#why_olicom, #how_much').click(function() {
        if($(this).attr('id') == 'why_olicom') {
            $('#how_much_collapse').fadeOut(400);
            $('#why_olicom_collapse').delay(600).fadeIn(400);
        }
        if($(this).attr('id') == 'how_much') {
            $('#how_much_collapse').fadeOut(400);
            $('#why_olicom_collapse').delay(600).fadeIn(400);
        }
    });

//    $('#big_contact_form #s3capcha').s3Capcha('#big_contact_form');
    //$('.kontener_main #s3capcha').s3Capcha('.kontener_main');
}) ;
