﻿/*
* Alternate Select Multiple (asmSelect) 1.0.4a beta - jQuery Plugin
* http://www.ryancramer.com/projects/asmselect/
* 
* Copyright (c) 2009 by Ryan Cramer - http://www.ryancramer.com
* 
* Dual licensed under the MIT (MIT-LICENSE.txt)
* and GPL (GPL-LICENSE.txt) licenses.
*
*/

(function($) {

    $.fn.asmSelect = function(customOptions) {

        var options = {

            listType: 'ol', 					// Ordered list 'ol', or unordered list 'ul'
            sortable: false, 					// Should the list be sortable?
            highlight: false, 				// Use the highlight feature? 
            animate: false, 					// Animate the the adding/removing of items in the list?
            addItemTarget: 'bottom', 			// Where to place new selected items in list: top or bottom
            hideWhenAdded: false, 				// Hide the option when added to the list? works only in FF
            debugMode: false, 				// Debug mode keeps original select visible 

            removeLabel: 'usuń', 				// Text used in the "remove" link
            highlightAddedLabel: 'Dodano: ', 			// Text that precedes highlight of added item
            highlightRemovedLabel: 'Usunieto: ', 		// Text that precedes highlight of removed item

            containerClass: 'asmContainer', 			// Class for container that wraps this widget
            selectClass: 'asmSelect', 			// Class for the newly created <select>
            optionDisabledClass: 'asmOptionDisabled', 	// Class for items that are already selected / disabled
            listClass: 'asmList', 				// Class for the list ($ol)
            listSortableClass: 'asmListSortable', 		// Another class given to the list when it is sortable
            listItemClass: 'asmListItem', 			// Class for the <li> list items
            listItemLabelClass: 'asmListItemLabel', 		// Class for the label text that appears in list items
            removeClass: 'asmListItemRemove', 		// Class given to the "remove" link
            highlightClass: 'asmHighlight'				// Class given to the highlight <span>

        };

        $.extend(options, customOptions);

        return this.each(function(index) {

            var $original = $(this); 				// the original select multiple
            var $container; 					// a container that is wrapped around our widget
            var $select; 						// the new select we have created
            var $ol; 						// the list that we are manipulating
            var buildingSelect = false; 				// is the new select being constructed right now?
            var ieClick = false; 				// in IE, has a click event occurred? ignore if not
            var ignoreOriginalChangeEvent = false; 		// originalChangeEvent bypassed when this is true

            function init() {

                // initialize the alternate select multiple

                // this loop ensures uniqueness, in case of existing asmSelects placed by ajax (1.0.3)
                while ($("#" + options.containerClass + index).size() > 0) index++;

                $select = $("<select></select>")
					.addClass(options.selectClass)
					.attr('name', options.selectClass + index)
					.attr('id', options.selectClass + index);

                $selectRemoved = $("<select></select>");

                $ol = $("<" + options.listType + "></" + options.listType + ">")
					.addClass(options.listClass)
					.attr('id', options.listClass + index);

                $container = $("<div></div>")
					.addClass(options.containerClass)
					.attr('id', options.containerClass + index);

                buildSelect();

                $select.change(selectChangeEvent)
					.click(selectClickEvent);

                $original.change(originalChangeEvent)
					.wrap($container).before($select).before($ol);

                if (options.sortable) makeSortable();

                if ($.browser.msie && $.browser.version < 8) $ol.css('display', 'inline-block'); // Thanks Matthew Hutton
            }

            function makeSortable() {

                // make any items in the selected list sortable
                // requires jQuery UI sortables, draggables, droppables

                $ol.sortable({
                    items: 'li.' + options.listItemClass,
                    handle: '.' + options.listItemLabelClass,
                    axis: 'y',
                    update: function(e, data) {

                        var updatedOptionId;

                        $(this).children("li").each(function(n) {

                            $option = $('#' + $(this).attr('rel'));

                            if ($(this).is(".ui-sortable-helper")) {
                                updatedOptionId = $option.attr('id');
                                return;
                            }

                            $original.append($option);
                        });

                        if (updatedOptionId) triggerOriginalChange(updatedOptionId, 'sort');
                    }

                }).addClass(options.listSortableClass);
            }

            function selectChangeEvent(e) {

                // an item has been selected on the regular select we created
                // check to make sure it's not an IE screwup, and add it to the list

                if ($.browser.msie && $.browser.version < 7 && !ieClick) return;
                var id = $(this).children("option:selected").slice(0, 1).attr('rel');
                addListItem(id);
                ieClick = false;
                triggerOriginalChange(id, 'add'); // for use by user-defined callbacks
            }

            function selectClickEvent() {

                // IE6 lets you scroll around in a select without it being pulled down
                // making sure a click preceded the change() event reduces the chance
                // if unintended items being added. there may be a better solution?

                ieClick = true;
            }

            function originalChangeEvent(e) {

                // select or option change event manually triggered
                // on the original <select multiple>, so rebuild ours

                if (ignoreOriginalChangeEvent) {
                    ignoreOriginalChangeEvent = false;
                    return;
                }

                $select.empty();
                $ol.empty();
                buildSelect();

                // opera has an issue where it needs a force redraw, otherwise
                // the items won't appear until something else forces a redraw
                if ($.browser.opera) $ol.hide().fadeIn("fast");
            }

            function buildSelect() {

                // build or rebuild the new select that the user
                // will select items from

                buildingSelect = true;

                // add a first option to be the home option / default selectLabel
                $select.prepend("<option>" + $original.attr('title') + "</option>");

                $original.children("option").each(function(n) {

                    var $t = $(this);
                    var id;

                    if (!$t.attr('id')) $t.attr('id', 'asm' + index + 'option' + n);
                    id = $t.attr('id');

                    if ($t.is(":selected")) {
                        addListItem(id);
                        addSelectOption(id, true);
                    } else {
                        addSelectOption(id);
                    }
                });

                if (!options.debugMode) $original.hide(); // IE6 requires this on every buildSelect()
                selectFirstItem();
                buildingSelect = false;
            }

            function addSelectOption(optionId, disabled) {

                // add an <option> to the <select>
                // used only by buildSelect()

                if (disabled == undefined) var disabled = false;

                var $O = $('#' + optionId);
                var $option = $("<option>" + $O.text() + "</option>")
					.val($O.val())
					.attr('rel', optionId);

                if (disabled) disableSelectOption($option);

                $select.append($option);
            }

            function selectFirstItem() {

                // select the firm item from the regular select that we created

                $select.children(":eq(0)").attr("selected", true);
            }

            function disableSelectOption($option) {

                // make an option disabled, indicating that it's already been selected
                // because safari is the only browser that makes disabled items look 'disabled'
                // we apply a class that reproduces the disabled look in other browsers

                $option.addClass(options.optionDisabledClass)
					.attr("selected", false)
					.attr("disabled", true);

                if (options.hideWhenAdded) $option.hide();
                if ($.browser.msie) $select.hide().show(); // this forces IE to update display
            }

            function enableSelectOption($option) {

                // given an already disabled select option, enable it

                $option.removeClass(options.optionDisabledClass)
					.attr("disabled", false);

                if (options.hideWhenAdded) $option.show();
                if ($.browser.msie) $select.hide().show(); // this forces IE to update display
            }

            function addListItem(optionId) {

                // add a new item to the html list

                var $O = $('#' + optionId);

                if (!$O) return; // this is the first item, selectLabel

                var $removeLink = $("<a></a>")
					.attr("href", "#")
					.addClass(options.removeClass)
					.prepend(options.removeLabel)
					.click(function() {
					    dropListItem($(this).parent('li').attr('rel'));
					    return false;
					});

                var $itemLabel = $("<span></span>")
					.addClass(options.listItemLabelClass)
					.html($O.html());

                var $item = $("<li></li>")
					.attr('rel', optionId)
					.addClass(options.listItemClass)
					.append($itemLabel)
					.append($removeLink)
					.hide();

                if (!buildingSelect) {
                    if ($O.is(":selected")) return; // already have it
                    $O.attr('selected', true);
                }

                if (options.addItemTarget == 'top' && !buildingSelect) {
                    $ol.prepend($item);
                    if (options.sortable) $original.prepend($O);
                } else {
                    $ol.append($item);
                    if (options.sortable) $original.append($O);
                }

                addListItemShow($item);

                disableSelectOption($("[rel=" + optionId + "]", $select));

                if (!buildingSelect) {
                    setHighlight($item, options.highlightAddedLabel);
                    selectFirstItem();
                    if (options.sortable) $ol.sortable("refresh");
                }

            }

            function addListItemShow($item) {

                // reveal the currently hidden item with optional animation
                // used only by addListItem()

                if (options.animate && !buildingSelect) {
                    $item.animate({
                        opacity: "show",
                        height: "show"
                    }, 100, "swing", function() {
                        $item.animate({
                            height: "+=2px"
                        }, 50, "swing", function() {
                            $item.animate({
                                height: "-=2px"
                            }, 25, "swing");
                        });
                    });
                } else {
                    $item.show();
                }
            }

            function dropListItem(optionId, highlightItem) {

                // remove an item from the html list

                if (highlightItem == undefined) var highlightItem = true;
                var $O = $('#' + optionId);

                $O.attr('selected', false);
                $item = $ol.children("li[rel=" + optionId + "]");

                dropListItemHide($item);
                enableSelectOption($("[rel=" + optionId + "]", options.removeWhenAdded ? $selectRemoved : $select));

                if (highlightItem) setHighlight($item, options.highlightRemovedLabel);

                triggerOriginalChange(optionId, 'drop');

            }

            function dropListItemHide($item) {

                // remove the currently visible item with optional animation
                // used only by dropListItem()

                if (options.animate && !buildingSelect) {

                    $prevItem = $item.prev("li");

                    $item.animate({
                        opacity: "hide",
                        height: "hide"
                    }, 100, "linear", function() {
                        $prevItem.animate({
                            height: "-=2px"
                        }, 50, "swing", function() {
                            $prevItem.animate({
                                height: "+=2px"
                            }, 100, "swing");
                        });
                        $item.remove();
                    });

                } else {
                    $item.remove();
                }
            }

            function setHighlight($item, label) {

                // set the contents of the highlight area that appears
                // directly after the <select> single
                // fade it in quickly, then fade it out

                if (!options.highlight) return;

                $select.next("#" + options.highlightClass + index).remove();

                var $highlight = $("<span></span>")
					.hide()
					.addClass(options.highlightClass)
					.attr('id', options.highlightClass + index)
					.html(label + $item.children("." + options.listItemLabelClass).slice(0, 1).text());

                $select.after($highlight);

                $highlight.fadeIn("fast", function() {
                    setTimeout(function() { $highlight.fadeOut("slow"); }, 50);
                });
            }

            function triggerOriginalChange(optionId, type) {

                // trigger a change event on the original select multiple
                // so that other scripts can pick them up

                ignoreOriginalChangeEvent = true;
                $option = $("#" + optionId);

                $original.trigger('change', [{
                    'option': $option,
                    'value': $option.val(),
                    'id': optionId,
                    'item': $ol.children("[rel=" + optionId + "]"),
                    'type': type
}]);
                }

                init();
            });
        };

    })(jQuery); 


$(function() {
    // Remove the coda-slider-no-js class from the body
    $("body").removeClass("coda-slider-no-js");
    // Preloader
    $(".coda-slider").children('.panel').hide().end().prepend('<p class="loading">Loading...<br /><img src="images/ajax-loader.gif" alt="loading..." /></p>');
});

var sliderCount = 1;

$.fn.codaSlider = function(settings) {

    settings = $.extend({
        autoHeight: true,
        autoHeightEaseDuration: 1000,
        autoHeightEaseFunction: "easeInOutExpo",
        autoSlide: false,
        autoSlideInterval: 7000,
        autoSlideStopWhenClicked: true,
        crossLinking: true,
        dynamicArrows: true,
        dynamicArrowLeftText: "&#171; left",
        dynamicArrowRightText: "right &#187;",
        dynamicTabs: true,
        dynamicTabsAlign: "center",
        dynamicTabsPosition: "top",
        externalTriggerSelector: "a.xtrig",
        firstPanelToLoad: 1,
        panelTitleSelector: "h2.title",
        slideEaseDuration: 1000,
        slideEaseFunction: "easeInOutExpo"
    }, settings);

    return this.each(function() {

        // Uncomment the line below to test your preloader
        // alert("Testing preloader");

        var slider = $(this);

        // If we need arrows
        if (settings.dynamicArrows) {
            slider.parent().addClass("arrows");
            slider.before('<div class="coda-nav-left" id="coda-nav-left-' + sliderCount + '"><a href="#">' + settings.dynamicArrowLeftText + '</a></div>');
            slider.after('<div class="coda-nav-right" id="coda-nav-right-' + sliderCount + '"><a href="#">' + settings.dynamicArrowRightText + '</a></div>');
        };

        var panelWidth = slider.find(".panel").width();
        var panelCount = slider.find(".panel").size();
        var panelContainerWidth = panelWidth * panelCount;
        var navClicks = 0; // Used if autoSlideStopWhenClicked = true

        // Surround the collection of panel divs with a container div (wide enough for all panels to be lined up end-to-end)
        $('.panel', slider).wrapAll('<div class="panel-container"></div>');
        // Specify the width of the container div (wide enough for all panels to be lined up end-to-end)
        $(".panel-container", slider).css({ width: panelContainerWidth });

        // Specify the current panel.
        // If the loaded URL has a hash (cross-linking), we're going to use that hash to give the slider a specific starting position...
        if (settings.crossLinking && location.hash && parseInt(location.hash.slice(1)) <= panelCount) {
            var currentPanel = parseInt(location.hash.slice(1));
            var offset = -(panelWidth * (currentPanel - 1));
            $('.panel-container', slider).css({ marginLeft: offset });
            // If that's not the case, check to see if we're supposed to load a panel other than Panel 1 initially...
        } else if (settings.firstPanelToLoad != 1 && settings.firstPanelToLoad <= panelCount) {
            var currentPanel = settings.firstPanelToLoad;
            var offset = -(panelWidth * (currentPanel - 1));
            $('.panel-container', slider).css({ marginLeft: offset });
            // Otherwise, we'll just set the current panel to 1...
        } else {
            var currentPanel = 1;
        };

        // Left arrow click
        $("#coda-nav-left-" + sliderCount + " a").click(function() {
            navClicks++;
            if (currentPanel == 1) {
                offset = -(panelWidth * (panelCount - 1));
                alterPanelHeight(panelCount - 1);
                currentPanel = panelCount;
                slider.siblings('.coda-nav').find('a.current').removeClass('current').parents('ul').find('li:last a').addClass('current');
            } else {
                currentPanel -= 1;
                alterPanelHeight(currentPanel - 1);
                offset = -(panelWidth * (currentPanel - 1));
                slider.siblings('.coda-nav').find('a.current').removeClass('current').parent().prev().find('a').addClass('current');
            };
            $('.panel-container', slider).animate({ marginLeft: offset }, settings.slideEaseDuration, settings.slideEaseFunction);
            if (settings.crossLinking) { location.hash = currentPanel }; // Change the URL hash (cross-linking)
            return false;
        });

        // Right arrow click
        $('#coda-nav-right-' + sliderCount + ' a').click(function() {
            navClicks++;
            if (currentPanel == panelCount) {
                offset = 0;
                currentPanel = 1;
                alterPanelHeight(0);
                slider.siblings('.coda-nav').find('a.current').removeClass('current').parents('ul').find('a:eq(0)').addClass('current');
            } else {
                offset = -(panelWidth * currentPanel);
                alterPanelHeight(currentPanel);
                currentPanel += 1;
                slider.siblings('.coda-nav').find('a.current').removeClass('current').parent().next().find('a').addClass('current');
            };
            $('.panel-container', slider).animate({ marginLeft: offset }, settings.slideEaseDuration, settings.slideEaseFunction);
            if (settings.crossLinking) { location.hash = currentPanel }; // Change the URL hash (cross-linking)
            return false;
        });

        // If we need a dynamic menu
        if (settings.dynamicTabs) {
            var dynamicTabs = '<div class="coda-nav" id="coda-nav-' + sliderCount + '"><ul></ul></div>';
            switch (settings.dynamicTabsPosition) {
                case "bottom":
                    slider.parent().append(dynamicTabs);
                    break;
                default:
                    slider.parent().prepend(dynamicTabs);
                    break;
            };
            ul = $('#coda-nav-' + sliderCount + ' ul');
            // Create the nav items
            $('.panel', slider).each(function(n) {
                ul.append('<li class="tab' + (n + 1) + '"><a href="#' + (n + 1) + '">' + $(this).find(settings.panelTitleSelector).text() + '</a></li>');
            });
            navContainerWidth = slider.width() + slider.siblings('.coda-nav-left').width() + slider.siblings('.coda-nav-right').width();
            ul.parent().css({ width: navContainerWidth });
            switch (settings.dynamicTabsAlign) {
                case "center":
                    ul.css({ width: ($("li", ul).width() + 2) * panelCount });
                    break;
                case "right":
                    ul.css({ float: 'right' });
                    break;
            };
        };

        // If we need a tabbed nav
        $('#coda-nav-' + sliderCount + ' a').each(function(z) {
            // What happens when a nav link is clicked
            $(this).bind("click", function() {
                navClicks++;
                $(this).addClass('current').parents('ul').find('a').not($(this)).removeClass('current');
                offset = -(panelWidth * z);
                alterPanelHeight(z);
                currentPanel = z + 1;
                $('.panel-container', slider).animate({ marginLeft: offset }, settings.slideEaseDuration, settings.slideEaseFunction);
                if (!settings.crossLinking) { return false }; // Don't change the URL hash unless cross-linking is specified
            });
        });

        // External triggers (anywhere on the page)
        $(settings.externalTriggerSelector).each(function() {
            // Make sure this only affects the targeted slider
            if (sliderCount == parseInt($(this).attr("rel").slice(12))) {
                $(this).bind("click", function() {
                    navClicks++;
                    targetPanel = parseInt($(this).attr("href").slice(1));
                    offset = -(panelWidth * (targetPanel - 1));
                    alterPanelHeight(targetPanel - 1);
                    currentPanel = targetPanel;
                    // Switch the current tab:
                    slider.siblings('.coda-nav').find('a').removeClass('current').parents('ul').find('li:eq(' + (targetPanel - 1) + ') a').addClass('current');
                    // Slide
                    $('.panel-container', slider).animate({ marginLeft: offset }, settings.slideEaseDuration, settings.slideEaseFunction);
                    if (!settings.crossLinking) { return false }; // Don't change the URL hash unless cross-linking is specified
                });
            };
        });

        // Specify which tab is initially set to "current". Depends on if the loaded URL had a hash or not (cross-linking).
        if (settings.crossLinking && location.hash && parseInt(location.hash.slice(1)) <= panelCount) {
            $("#coda-nav-" + sliderCount + " a:eq(" + (location.hash.slice(1) - 1) + ")").addClass("current");
            // If there's no cross-linking, check to see if we're supposed to load a panel other than Panel 1 initially...
        } else if (settings.firstPanelToLoad != 1 && settings.firstPanelToLoad <= panelCount) {
            $("#coda-nav-" + sliderCount + " a:eq(" + (settings.firstPanelToLoad - 1) + ")").addClass("current");
            // Otherwise we must be loading Panel 1, so make the first tab the current one.
        } else {
            $("#coda-nav-" + sliderCount + " a:eq(0)").addClass("current");
        };

        // Set the height of the first panel
        if (settings.autoHeight) {
            panelHeight = $('.panel:eq(' + (currentPanel - 1) + ')', slider).height();
            slider.css({ height: panelHeight });
        };

        // Trigger autoSlide
        if (settings.autoSlide) {
            slider.ready(function() {
                setTimeout(autoSlide, settings.autoSlideInterval);
            });
        };

        function alterPanelHeight(x) {
            if (settings.autoHeight) {
                panelHeight = $('.panel:eq(' + x + ')', slider).height()
                slider.animate({ height: panelHeight }, settings.autoHeightEaseDuration, settings.autoHeightEaseFunction);
            };
        };

        function autoSlide() {
            if (navClicks == 0 || !settings.autoSlideStopWhenClicked) {
                if (currentPanel == panelCount) {
                    var offset = 0;
                    currentPanel = 1;
                } else {
                    var offset = -(panelWidth * currentPanel);
                    currentPanel += 1;
                };
                alterPanelHeight(currentPanel - 1);
                // Switch the current tab:
                slider.siblings('.coda-nav').find('a').removeClass('current').parents('ul').find('li:eq(' + (currentPanel - 1) + ') a').addClass('current');
                // Slide:
                $('.panel-container', slider).animate({ marginLeft: offset }, settings.slideEaseDuration, settings.slideEaseFunction);
                setTimeout(autoSlide, settings.autoSlideInterval);
            };
        };

        // Kill the preloader
        $('.panel', slider).show().end().find("p.loading").remove();
        slider.removeClass("preload");

        sliderCount++;

    });
};

function ajax_url_request(url) {
    var url = url;


    $.ajax({
        type: "GET",
        url: url,
        pobierz: function(XMLHttpRequest) {
        $("#tree-menu").html('<div><img src="/sklepef/images/loading.gif" /></div>');
        },
        success: function(msg) {
        $("#tree-menu").html(msg);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
        $("#tree-menu").html('Brak wyniku, nie ma takiej strony.');
        }
    });
    
    
//        $("#container").fadeIn("slow");
//        $('#container').html('<div style="position:absolute;width:80%;height:80%;"><div style="width:100px;height:100px;margin:auto auto;"><img src="/shopping/media/images/loading.gif" /></div></div>');
//        $('#container').load(url);
//        //fadeIn('#container', "slow");
        
   
}
function ajax_form_request(action_form, formId) {
    var action_form = action_form;
    var formId = formId;
    $("form#" + formId).submit(function() {
        var dataStr = $("#" + formId).serialize();
        $('#container').html('<div style="position:absolute;width:80%;height:80%;"><div style="width:100px;height:100px;margin:0 auto;"><img src="/shopping/media/images/loading.gif" /></div></div>');
        $.ajax({
            type: "POST",
            url: action_form,
            data: dataStr,
            success: function(msg) {
                $('#container').load(action_form);
            }
        });
        return false;
    });
}
function ajax_validate(action_form, formId) {
    var action_form = action_form;
    var formId = formId;
    $(document).ready(function() {
        $("#" + formId).validationEngine({
            onValidationComplete: function(form, status) {
                if (status == true) {
                    setTimeout(ajax_form_request(action_form, formId), 4000);
                }
            }
        })
    });
}

$(document).ready(function() {

    $("ul#topnav li").hover(function() { //Hover over event on list item
        $(this).css({ 'background': '#a70a0a url(topnav_active.gif) repeat-x' }); //Add background color + image on hovered list item
        $(this).find("span").show(); //Show the subnav
    }, function() { //on hover out...
        $(this).css({ 'background': 'none' }); //Ditch the background
        $(this).find("span").hide(); //Hide the subnav
    });

});

$(document).ready(function() {
 
	$(".tab_content").hide();
	$("ul.tabs li:first").addClass("active").show();
	$(".tab_content:first").show();
	
	$("ul.tabs li").click(function() {
		$("ul.tabs li").removeClass("active");
		$(this).addClass("active");
		$(".tab_content").hide();
		var activeTab = $(this).find("a").attr("href");
		$(activeTab).fadeIn();
		return false;
	});
});
 $(function() {
      $('#tooltips a').lightBox();
  });
