

/**
 * Style switcher plugin
 * @author: Tomek Pęszor
 * @example
 * Usage: $('#selector').styleSwitcher();
 */
(function($){
    $.fn.extend({
        styleSwitcher: function(userOptions) {
            var defaults = {
                enableAllLabel: 'enable all',
                disableAllLabel: 'disable all',
                wrappingElement: '<div id="styleSwitcher"><h3 id="styleSwitcher_h">Alternatywne wersje</h3></div>'
            };

            var options =  $.extend(defaults, userOptions);

            return this.each(function() {
                var o = options;


                function selectStyle(chosen_style) {
                    styles.each(
                        function(){
                            var style = $(this).attr('title');
                            $(this).attr("disabled", true);
                            if (style == chosen_style) {
                                $(this).attr("disabled", false);
                            }
                        });

                    setCookie('pageStyleTmp', chosen_style, 365);
                    $('select[name="pageStyleSwitcher"]').val(chosen_style);
                }

                // cookie functions

                function setCookie(cookieName, cookieValue, nDays) {
                    var today = new Date();
                    var expire = new Date();
                    if (nDays === null || nDays === 0) {
                        nDays = 1;
                    }
                    expire.setTime(today.getTime() + 3600000 * 24 * nDays);
                    document.cookie = cookieName + "=" + escape(cookieValue) + ";expires=" + expire.toGMTString();
                }

                function getCookie(cookieName) {
                    var theCookie = "" + document.cookie;
                    var ind = theCookie.indexOf(cookieName);
                    if (ind == -1 || cookieName === "") {
                        return "";
                    }
                    var ind1 = theCookie.indexOf(';', ind);
                    if (ind1==-1) {
                        ind1 =theCookie.length;
                    }
                    return unescape(theCookie.substring(ind + cookieName.length + 1, ind1));
                }

                function disableAll()
                {
                    var styles = $('head link[rel*="stylesheet"]');
                        styles.each(function() {
                                $(this).attr('disabled', true);

                        });
                        setCookie('pageStyleTmp','disable_all_styles_unique_identifier', 365);
                }
                function enableAll()
                {
                    var styles = $('head link[rel*="stylesheet"]');
                        styles.each(function() {
                                $(this).attr('disabled', false);

                        });
                        setCookie('pageStyleTmp','enable_all_styles_unique_identifier', 365);
                }

                // the code

                var select = $('<select name="pageStyleSwitcher"></select>');
                var styles = $('head link[title][rel="alternate stylesheet"]');
                var option;
                styles.each(function(){
                    name = $(this).attr('title');
                    option = $('<option value="'+name+'">'+name+'</option>');
                    if ($(this).attr('rel').indexOf('alternate') == -1) {
                        option.attr('selected', 'selected');
                    }
                    option.click(function(){
                        var name = $('select[name="pageStyleSwitcher"]').val();
                        selectStyle(name);
                    });
                    select.append(option);
                });


                // option to disable all styles
                option = $('<option value="disable_all_styles_unique_identifier">'+o.disableAllLabel+'</option>');
                option.click(function(){
                    disableAll();
                });
                select.append(option);

                // option to enable all styles
                option = $('<option value="enable_all_styles_unique_identifier">'+o.enableAllLabel+'</option>');
                option.click(function(){
                    enableAll();
                });
                select.append(option);

                parentHtml = $(o.wrappingElement);

                parentHtml.append(select);
                $(this).after(parentHtml);

                var styleName = getCookie('pageStyleTmp');
                if (styleName) {
                    selectStyle(styleName);
                }

            });
        }
    });

})(jQuery);

