$(function()
{
    /** Lightbox */
    $('a[rel="lightBox"]').lightBox();
    
    /** Newsletter */
    $('#newsletterPdf p a').attr('href', $('#newsletterPdf h2 a').attr('href'));
    
     /* Division menu */
    $('#productMenu a[rel]').click(function() {
        $('#productMenu li').removeClass('active');
        $('#productMenu li a').removeClass('orangeBackground');
        $(this).addClass('orangeBackground');
        $(this).parent().addClass('active');
        $('#productMainInfo div.contentBox').addClass('none');
        $('#' + this.rel).removeClass('none');
    });
    
    /**
     * Filters - parameters, brands, product action type
     */
    function getFilters()
    {
        filters = '';
        $('#filter select option:selected').each(function() {
            if ($(this).val() > 0) {
                filters += $(this).val() + ',';
            }
        });
        
        brand = '';
        $('#brandFilter input:checked').each(function() {
            brand += $(this).val() + ',';
        });
        
        productTypes = '';
        $('#productTypeFilter input:checked').each(function() {
            productTypes += $(this).val() + ',';
        });
        
        $.get("./application/models/ajax.php", {page: 'filters', url: $('#currentCategoryUrl').val(), filtr: filters, znacka: brand, akce: productTypes}, function(vystup){
            document.location.href = vystup;
        });
    }
        
    $('#filter select').change(function() {getFilters();});
    $('#brandFilter input').click(function() {getFilters();});
    $('#productTypeFilter input').click(function() {getFilters();});
    
    /**
     * Store check - category
     */
    function checkStoreCategory(id)
    {
        parameters = '';
        productId = id;
        showButton = true;

        $('#' + productId + ' select option:selected').each(function() {
            if ($(this).val() != 0) {
                parameters += $(this).val() + ',';
            } else {
                showButton = false;
            }
        });
        
        $.get("./application/models/ajax.php", {page: 'product', id: productId, parameters: parameters}, function(vystup){
            if (vystup > 0) {
                $('#' + productId + ' span.unavailableProduct').hide();
                if (showButton == true) {
                    $('#' + productId + ' a.submitDisable').hide();
                    $('#' + productId + ' input.submit').show();
                } else {
                    $('#' + productId + ' input.submit').hide();
                    $('#' + productId + ' a.submitDisable').show();
                }
            } else {
                $('#' + productId + ' input.submit').hide();
                $('#' + productId + ' a.submitDisable').hide();
                $('#' + productId + ' span.unavailableProduct').show();
            }
        });
    }
    
    //$('.product select').change(function() {checkStoreCategory($(this).parents().get(2).id);});
    
    /**
     * Store check - product
     */
    function checkStoreProduct(id)
    {
        parameters = '';
        productId = id;
        showButton = true;

        $('#productDetail select option:selected').each(function() {
            if ($(this).val() != 0) {
                parameters += $(this).val() + ',';
            } else {
                showButton = false;
            }
        });
        
        $.get("../application/models/ajax.php", {page: 'product', id: productId, parameters: parameters}, function(vystup){
            if (vystup > 0) {
                $('#productDetail span.unavailableProduct').hide();
                if (showButton == true) {
                    $('#productDetail a#productBuySubmitDisable').hide();
                    $('#productDetail input#productBuySubmit').show();
                } else {
                    $('#productDetail input#productBuySubmit').hide();
                    $('#productDetail a#productBuySubmitDisable').show();
                }
            } else {
                $('#productDetail input#productBuySubmit').hide();
                $('#productDetail a#productBuySubmitDisable').hide();
                $('#productDetail span.unavailableProduct').show();
            }
        });
    }
    
    $('#productDetail select').change(function() {checkStoreProduct($('#productDetail input#productId').val());});
    
    /**
     * Payment type
     */
    function setPaymentType(type)
    {
        $.get("../application/models/ajax.php", {page: 'payment', type: type}, function(vystup){
            document.location.href = '/nakupni-kosik';
        });
    }
    
    $('select#paymentType').change(function() {setPaymentType($(this).val());});
     
    /**
     * Transport type
     */
    function setTransportType(type)
    {
        $.get("../application/models/ajax.php", {page: 'transport', type: type}, function(vystup){
            document.location.href = '/nakupni-kosik';
        });
    }
    
    $('select#transportType').change(function() {setTransportType($(this).val());});
    
    /**
     * Own slider
     */
    function mySlider(visibleItemsCount)
    {
        visibleItemsCount = visibleItemsCount - 1;
        itemsCount = $('#togetherBoughtProducts div').size();

        if (itemsCount <= visibleItemsCount + 1) {
            $('a#nextItem').css('display', 'none');
            $('a#lastItem').css('display', 'none');
        }

        startIndex = $('#togetherBoughtProducts div:first').index();

        if (startIndex == 0) {
            $('a#lastItem').css('display', 'none');
        }

        /* Next item button */
        $('a#nextItem').click(function() {
            $('a#lastItem').css('display', 'block');
            index = $('#togetherBoughtProducts div:not(.none):last').index();

            if (index < itemsCount - 1) {
                $('#togetherBoughtProducts div').eq(index + 1).removeClass('none');
                $('#togetherBoughtProducts div').eq(index - visibleItemsCount).addClass('none');

                if ((index + 1) == itemsCount - 1) {
                    $('a#nextItem').css('display', 'none');
                }
            }
        });

        /* Last item button */
        $('a#lastItem').click(function() {
            $('a#nextItem').css('display', 'block');
            index = $('#togetherBoughtProducts div:not(.none):first').index();

            if (index > 0) {
                $('#togetherBoughtProducts div').eq(index - 1).removeClass('none');
                $('#togetherBoughtProducts div').eq(index + visibleItemsCount).addClass('none');

                if (index == 1) {
                    $('a#lastItem').css('display', 'none');
                }
            }
        });
    }

    mySlider(3);

});

var popup_player = null;
var player_current_time = 0;
var popup_player_checker = null;

function check_popup_player_status()
{
	if (!popup_player || popup_player.closed)
	{
		window.clearInterval(popup_player_checker);
		//$('.player-popup').removeClass('turned-off');
		//$('.player-switch').show();
		$('.player-switch').removeClass('jp-pause').addClass('jp-play');
	}
}

$(document).ready(function() {
	
    $('#animation ul').cycle({
		fx: 'fade',
		timeout: 5000
	});
	$('.player-switch.jp-pause').live('click',function(e){
		$('.player-switch').removeClass('jp-pause').addClass('jp-play');
		if (popup_player)
		{
			popup_player.close();
		}
	});
	$('.player-switch.jp-play').live('click',function(e){
		$('.player-switch').removeClass('jp-play').addClass('jp-pause');
		popup_player = window.open("player.html",'player_window','width=420,height=107');
		popup_player.onload = function(e){
			popup_player.document.getElementById('player_position').innerHTML = player_current_time;
			popup_player_checker = window.setInterval("check_popup_player_status()",1000);
		};
	});
	/*$('.player-switch.jp-pause').live('click',function(e){
		$('#jplayer').jPlayer("pause");
	});
	$('.player-switch.jp-play').live('click',function(e){
		$('#jplayer').jPlayer("play");
	});*/
	/*$('.player-popup').live('click',function(e){
		$(this).addClass('turned-off');
		
		popup_player = window.open("player.html",'player_window','width=420,height=107');
		popup_player.onload = function(e){
			popup_player.document.getElementById('player_position').innerHTML = player_current_time;
			popup_player_checker = window.setInterval("check_popup_player_status()",1000);
		};
		$('#jplayer').jPlayer("stop");
		$('.player-switch').hide();
	});*/
	/*$('#jplayer').jPlayer({
		ready: function () {
			$(this).jPlayer( "setMedia", {
		      mp3: "/media/audio/christmas-song.mp3"
		    });
		    $(this).jPlayer("play");
		    
		},
		timeupdate: function(event) {
			player_current_time = event.jPlayer.status.currentTime; 
		},
		play: function(event) {
			$('.player-switch').removeClass('jp-play').addClass('jp-pause');
			$('.player-switch').attr('title','Vypnout hudbu');
		},
		pause: function(event) {
			$('.player-switch').removeClass('jp-pause').addClass('jp-play');
			$('.player-switch').attr('title','Zapnout hudbu');
		},
		stop: function(event) {
			$('.player-switch').removeClass('jp-pause').addClass('jp-play');
			$('.player-switch').attr('title','Zapnout hudbu');
		},
		ended: function(event) {
			
		},
		swfPath: "/media/js",
		supplied: "mp3",
		wmode: "window",
		loop: true
	});*/
});
