/*
 * script.js - JavaScript kod
 * Autor: Radek Liska, radarfox at seznam.cz
 * Autor: Pavel Ptacek, pavel.ptacek at swallowebsite.com
 */

/**
 * This class handles the carousel, since the guys from netwings are just incapable
 *
 * Author: Pavel Ptacek
 */
var carousel = {
    /**
     * Current element id
     */
    current : 1,

    /**
     * Total number of elements
     */
    total : 0,

    /**
     * One element - open width
     */
    openWidth : 0,

    /**
     * Pauses the carousel
     */
    pause : false,

    /**
     * Opens next element
     */
    openNext : function() {
        if(carousel.pause) {
            return;
        }

        // Close the current one
        carousel.current++;
        if(carousel.current > carousel.total) {
            carousel.current = 1;
        }
        $('.accordition ul li').addClass('closed').animate({'width': '40px'}, {duration: 400, queue: false});
        $('#carousel-' + carousel.current).removeClass('closed').animate({'width': carousel.openWidth + 'px'}, {duration: 400, queue: false});
    },

    /**
     * Opens the clicked one
     */
    openOne : function() {
        carousel.current = $(this).parent('li').attr('id').replace('carousel-', '');
        $('.accordition ul li').addClass('closed').animate({'width': '40px'}, {duration: 400, queue: false});
        $('#carousel-' + carousel.current).removeClass('closed').animate({'width': carousel.openWidth + 'px'}, {duration: 400, queue: false});
        return false;
    },

    /**
     * Bind the functions
     */
    init : function() {
        carousel.total = $('.accordition ul li').length;
        carousel.openWidth = 980 - ((carousel.total-1) * 40);

        // Open the first one
        $('#carousel-' + carousel.current).removeClass('closed').css('width', carousel.openWidth + 'px');

        // Bind the hover
        $('.accordition').hover(function(){ carousel.pause = true; }, function(){ carousel.pause = false; });
        $('.accordition a.handle').click(carousel.openOne);

        // Rebind the heights of each "more" link
        $('.accordition .rest').css('width', (carousel.openWidth - 40) + 'px');

        // Bind the timeout
        setInterval(carousel.openNext, 6000);
    }
};

/* ==============================================[ funkce pro skryvani a zobrazovani neswletteru ] */

var selectorNewsletter = '#head #newsletter';
var selectorNewsletterButton = '#head .box.menu li.n01 a';
var selectorNewsletterClose = '#head .box.subscribe .close';

function initNewsletter() {
	// Nastavime akci po stisknuti buttonu
	$(selectorNewsletterButton)
		.click(function() {
                        // Close fulltext first if open
                        if($('#fulltext').is(':visible')) {
                            $('#fulltext .close').click();
                        }

			// Zobrazime newsletter
			$(selectorNewsletter).show();

			// Aktivujeme tlacitko
			$(selectorNewsletterButton).addClass('active');

			// Odkaz ignorujeme
			return false;
		});
	// Nastavime akci po stisknuti buttonu close
	$(selectorNewsletterClose)
		.click(function() {
			// Skryjeme newsletter
			$(selectorNewsletter).hide();

			// Aktivujeme tlacitko
			$(selectorNewsletterButton).removeClass('active');

                        $(selectorNewsletterButton).blur();

                        return false;
		});
}


function initFulltext() {
	// Nastavime akci po stisknuti buttonu
	$('#head .box.menu li.n02 a')
		.click(function() {
                        if($(selectorNewsletter).is(':visible')) {
                            $(selectorNewsletterClose).click();
                        }

			// Zobrazime newsletter
			$('#fulltext').show();

			// Aktivujeme tlacitko
			$('#head .box.menu li.n02 a').addClass('active');

			// Odkaz ignorujeme
			return false;
		});
	// Nastavime akci po stisknuti buttonu close
	$('#fulltext .close')
		.click(function() {
			// Skryjeme newsletter
			$('#fulltext').hide();

			// Aktivujeme tlacitko
			$('#head .box.menu li.n02 a').removeClass('active');

                        $('#head .box.menu li.n02 a').blur();

                        // Odkaz ignorujeme
                        return false;
		});
}

/* ==============================================[ funkce pro zobrazovani polozek kontaktniho formulare ] */

var selectorContactItem = '#main form.contact .item';
var selectorContactButton = '#main form.contact button.add';
var classesContact = ['n01', 'n02', 'n03', 'n04', 'n05', 'n06'];
var countContact = 0;

function initContactForm() {
	// Nastavime akci po stisknuti buttonu
	$(selectorContactButton)
		.click(function() {
			// Zobrazime dalsi polozku
			$(selectorContactItem + '.' + classesContact[countContact++])
				.show();
			// Pokud jsou zobrazeny vsechny skryjeme tlacitko
			if (countContact >= classesContact.length) {
				$(selectorContactButton)
					.hide();
			}
		});
	// Skryjeme vsechny polozky
	$(selectorContactItem)
		.hide();
	// Aktivace prvni polozky
	$(selectorContactButton)
		.click();
}

/* ==============================================[ funkce document ready ] */

$(document).ready(function(){

	// LIGHTBOX
	$('a.lightbox')
		.lightBox();

	// HORIZONTAL ACCORDITION
        if($('.accordition').attr('class') == 'accordition') {
            carousel.init();
        }

	// HEAD NEWSLETTER
	initNewsletter();


        initFulltext();

	// MAIN CONTACT FORM
	initContactForm();
});
