$productImages = null;
productImagesIndex = 0;

(function ($){
	$.extend($, {
		page: function (node) {
                    /* tabs */
                    jQuery('#j-sort > ul.tabs > li > a').unbind('click').click(function () {
                        var isActive = $($(this).attr('href')).hasClass('active')

                        $('#j-sort > ul.tabs > li > a').removeClass('active');
                        $(this).parents('.sort').find('div.pole').removeClass('active');

                        if ( ! isActive) {
                            $($(this).attr('href')).addClass('active');
                            $(this).addClass('active');
                        }
                        /* Bug #1009: We must call it again, becouse of filtration panel, which move all down. */
                        if (typeof zabezpeciObrazky == 'function') {
                            zabezpeciObrazky();
                        }
                        return false;
                    });

                    // tabulkove zobrazeni - osetreni formulare
                    $("#frm_tableview_buy td input[type=submit]").click(function () {
                       var $form = $(this).parents('form');
                       
                       $form.attr('action', $form.attr('action').substring(0, $form.attr('action').indexOf('/pridat/zbozi/') + 14) + $(this).attr('name') + '/');
                       return true;
                    });

		},
                productImages: function ($node) {
                    $productImages = $node.find('a.j-product-image');

                    if ($productImages.length != 0) {
                        $node.find('a.j-product-images-next').click(function () {
                            if (productImagesIndex < $productImages.length - 1) {
                                $($productImages.get(productImagesIndex++)).removeClass('active');
                                $($productImages.get(productImagesIndex)).addClass('active');
                                $node.find('a.j-product-image-detail').attr('href', $($productImages.get(productImagesIndex)).attr('href'));
                                if (productImagesIndex == $productImages.length - 1) {
                                    $node.find('a.j-product-images-next').addClass('inactive');
                                }

                                $node.find('a.j-product-images-previous').removeClass('inactive');

                            }

                            return false;
                        });

                        $node.find('a.j-product-images-previous').click(function () {
                            if (productImagesIndex > 0) {
                                $($productImages.get(productImagesIndex--)).removeClass('active');
                                $($productImages.get(productImagesIndex)).addClass('active');
                                $node.find('a.j-product-image-detail').attr('href', $($productImages.get(productImagesIndex)).attr('href'));

                                if (productImagesIndex == 0) {
                                    $node.find('a.j-product-images-previous').addClass('inactive');
                                }

                                $node.find('a.j-product-images-next').removeClass('inactive');
                            }

                            return false;
                        });
                    }

                    $('.j-product-image-detail').click(function () {
                        jQuery("#product-images").find('a.active:first').trigger('click');
                        return false;
                    })

                },
		webroot: function () {
                    return $('#j-webroot').attr('href');
                },
		postRequest: function (url, callback) {
                    $.post(url + "?" + (new Date).getTime(), {}, callback);
		},
                createDialog: function (content, title, width, minHeight, modal, bindFnc) {
		    var container = $('<div>' + content + '</div>');
		    container.dialog({
    		       'modal': modal,
    		       'title': title,
    		       'width': width,
    		       'minHeight': minHeight,
                       'open': function (event, ui) {
                           if (typeof bindFnc == 'function') {
                               bindFnc();
                           }
                       },
                       'close': function (event, ui) {
                           $(this).remove();
                       }
                    });
                },
                watchDog: function (nodes) {
                    $.bindForm = function () {
                        $("#frm_hlidacipes").submit(function () {
                            $.ajax({ // create an AJAX call...
                                data: $(this).serialize(), // get the form data
                                type: 'get',
                                url: $(this).attr('action'), // the file to call
                                success: function(response) { // on success..
                                    $("#frm_hlidacipes").parents('.ui-dialog-content').html(response);
                                    $.bindForm();
                                }
                            });

                            return false;
                        });
                    };

                    nodes.each(function () {
                        $(this).click(function () {
                            $.get($(this).attr('href'), {}, function (html) {
                            $.createDialog(
                                html,
                                'Hlídací pes',
                                500,
                                250,
                                true,
                                function () {
                                    $.bindForm();
                                }
                                );
                            });

                            return false;
                        });
                    });
                },
                productPage: function () {
                    $('#j-product-images').productImages();
                    $('.j-watchdog-link').watchDog();
                    $('#j-uverova-kalkulacka').popupWindow({ 
                		width: 739,
                		centerBrowser:1 
                	});
                    $('.j-all-prices-link').click(function () {
                        $.get($(this).attr('href'), {}, function (html) {
                            $.createDialog(
                                html,
                                'Ceny produktu',
                                700,
                                600,
                                true
                            );
                        });

                        return false;
                    });

                    $('.j-euro-link').click(function () {
                        $.get($(this).attr('href'), {}, function (html) {
                            $.createDialog(html, 'Převodní kurz eura', 300, 100, false);
                        });

                        return false;
                    });
                },
                notOnStock: function (zboziId) {
                	var pocet = $('#frm_koupit' + zboziId + ' > div > input').val();
                	if (typeof(pocet) == 'undefined') {
                		pocet = $('#frm_koupit > div > input').val();
                		if (typeof(pocet) == 'undefined') {
                			pocet = 1;
                		}
                	}
                    $.get('/zbozineniskladem/'+ zboziId + '/?pocet=' + pocet + '', {}, function (html) {
                        $.createDialog(
                            html,
                            'Zboží není skladem',
                            300,
                            100,
                            true
                        );
                    });
                }
	});
	
	$.fn.extend({
		page: function () {
			return $.page(this);
		},
                productImages: function () {
                    return $.productImages(this);
                },
                watchDog: function () {
                    return $.watchDog(this);
                }
	});
	
	$(document).ready($(document).page);
})(jQuery);