function getLangStr(key, target_lang) {
    if (!key) {return '';}
    if (!target_lang) {target_lang = lang;}
    key = encodeURIComponent(key);
    if (lang_dict[key] && lang_dict[key][target_lang]){
        return decodeURIComponent(lang_dict[key][target_lang]);
    }
    return key;
}

function textLimiter(field, countfield, limit) {
    if (field.val().length > limit) {
        field.val(field.val().substring(0, limit));
    } else {
        countfield.html(limit - field.val().length);
    }
}

$(function() {
    $('input, select, textarea, button').mouseenter(function() {
        $(this).addClass('hover');
    });
    $('input, select, textarea, button').mouseleave(function() {
        $(this).removeClass('hover');
    });
    $('input, select, textarea, button').focus(function() {
        $(this).addClass('focus');
    });
    $('input, select, textarea, button').blur(function() {
        $(this).removeClass('focus');
    });
});

$(function() {
    $('button').click(function() {
        $(this).val($(this).attr('name'));
    });
});

$(function() {
    $('legend input.activator_0').each(function() {
        var fieldset_fields = $(this).parent().parent().parent().find('.fields');
        if ($(this).is(':checked')) {
            $(fieldset_fields).hide();
        }
        $(this).click(function() {
            if ($(this).is(':checked')) {
                $(fieldset_fields).show();
            } else {
                $(fieldset_fields).hide();
            }
        });
        $(this).change(function() {
            if ($(this).is(':checked')) {
                $(fieldset_fields).hide();
            } else {
                if ($(fieldset_fields).css('display') == 'none') {
                    $(fieldset_fields).show();
                }
            }
        });
    });
    $('legend input.activator_1').each(function() {
        var fieldset_fields = $(this).parent().parent().parent().find('.fields');
        if (!$(this).is(':checked')) {
            $(fieldset_fields).hide();
        }
        $(this).click(function() {
            if ($(this).is(':checked')) {
                $(fieldset_fields).show();
            } else {
                $(fieldset_fields).hide();
            }
        });
        $(this).change(function() {
            if ($(this).is(':checked')) {
                if ($(fieldset_fields).css('display') == 'none') {
                    $(fieldset_fields).show();
                }
            } else {
                $(fieldset_fields).hide();
            }
        });
    });
});

/* quicksearch */
$(function() {
    $('.form_quicksearch .input_field').each(function() {
        $(this).attr('standardvalue', getLangStr('search_input', lang));
        $(this).focus(function() {
            if ($(this).val() == $(this).attr('standardvalue')) {
                this.value = '';
            }
        });
        $(this).blur(function() {
            if (jQuery.trim($(this).val()) == '') {
                this.value = $(this).attr('standardvalue');
            }
        });
    });
});

$(function() {
    if ($('ul.fontsizes').length) {
        $('ul.fontsizes .size').click(function() {
            var clicked = this;
            var metadata = $(this).metadata();
            $.cookies.set('font-size', metadata.fontSize);
            $('ul.fontsizes .size').each(function() {
                if (this == clicked) {
                    if (!$(this).hasClass('active')) {
                        $(this).addClass('active');
                    }
                    if ($(this).hasClass('default')) {
                        $('body').removeClass('fontsize_adjusted');
                    } else {
                        if (!$('body').hasClass('fontsize_adjusted')) {
                            $('body').addClass('fontsize_adjusted');
                        }
                    }
                } else {
                    $(this).removeClass('active');
                }
            });
            $('body').css('font-size', metadata.fontSize);
        });
        
        $('ul.fontsizes .decrease').click(function() {
            var prev = $('ul.fontsizes .size.active').prev();
            if (prev.hasClass('size')) {
                prev.click();
            }
        });
        
        $('ul.fontsizes .encrease').click(function() {
            var next = $('ul.fontsizes .size.active').next();
            if (next.hasClass('size')) {
                next.click();
            }
        });
        
        var fontSize = $.cookies.get('font-size');
        if (fontSize) {
            var sizeItem = $.grep($('ul.fontsizes .size'), function(item) {
                return $(item).metadata().fontSize == fontSize;
            });
            if (sizeItem.length > 0) {
                $(sizeItem[0]).click();
            } else {
                $('ul.fontsizes .size.default').click();
            }
        } else {
            $('ul.fontsizes .size.default').click();
        }
        
        $('ul.fontsizes').addClass('loaded');
    }
});

$(function($, undefined) {
    $.nmFilters({
        text: {
            is: function(nm) {
                var metadata = nm.opener.metadata();
                if (metadata.text && metadata.text.length > 0) {
                    return true;
                } else {
                    return false;
                }
            },
            beforeShowCont: function(nm) {
                var offset = nm.elts.cont.offset();
                var metadata = nm.opener.metadata();
                nm.store.text = $('<div />', {
                    html: decodeURIComponent(metadata.text)
                }).addClass('nyroModalText');
                nm.elts.cont.append(nm.store.text);
            },
            close: function(nm) {
                if (nm.store.text) {
                    nm.store.text.remove();
                    nm.store.text = undefined;
                    delete(nm.store.text);
                }
            }
        }
    });
});

$(function() {
    $('.gallery a, .galleryitem').nyroModal({
        closeButton: '<a href="#" class="nyroModalClose nyroModalCloseButton nmReposition" title="' + getLangStr('Close', lang) + '">' + getLangStr('Close', lang) + '</a>'
    });
});

$(function() {
    $('body').delegate('a.imagepreview', 'mouseenter', function() {
        var my_img = $('img', $(this));
        var x_offset = ((my_img.attr('width') - $(this).width())/2)+5;
        var y_offset = ((my_img.attr('height') - $(this).height())/2)+5;
        $(this).qtip({
            overwrite: true,
            content: {
                text: function() {
                    returnvalue = $(this).clone();
                    returnvalue.removeAttr('rel');
                    returnvalue.removeClass('imagepreview');
                    returnvalue.removeClass('vertical-middle');
                    returnvalue.removeAttr('style')
                    returnvalue.data('origin', $(this));
                    return returnvalue;
                }
            },
            hide: {
                fixed: true
            },
            show: {
                ready: true
            },
            position: {
                my: 'top left',
                at: 'left top',
                adjust: {
                    x: -x_offset,
                    y: -y_offset
                }
            },
            style: {
                classes: 'ui-tooltip-imagepreview',
                tip: false
            }
        });
    });
});

$(function() {
    $('body').delegate('.ui-tooltip-imagepreview a', 'click', function(event) {
        event.preventDefault();
        $(this).data('origin').click();
        $('.qtip').qtip('hide');
    });
});

$(function() {
    $('body').delegate('.overview a.imagepreview, .shop_overview a.imagepreview', 'click', function(event) {
        window.location = $(this).attr('href');
    });
});

$(function() {
    try {
        $('.playvideo').each(function() {
            flowplayer($(this).attr('id'), '/globalfiles/swf/flowplayer.swf');
        });
    } catch (e) {}
});

$(function() {
    if ($('.locationmap, .locationsmap').length > 0) {
        $.getScript('http://maps.google.com/maps/api/js?sensor=false&language=' + language_codes[lang]['iso-639-1'] + '&async=2&callback=jQuery.noop');
    }
});

$(function() {
    $('.locationmap, .locationsmap').not($('.tabcontainer_item .locationmap')).each(function() {
        eval($(this).metadata().init_function)();
    });
});

$(function() {
   $('.tabcontainer').each(function() {
       
        var container = $(this);
        $('.tabcontainer_navigation a', container).each(function() {
            $(this).click(function(event) {
                event.preventDefault();
                var clicked = this;
                $('.tabcontainer_navigation a', container).each(function(index) {
                    var target = $($(this).attr('href'))
                    if (this == clicked) {
                        $.cookies.set(container.attr('id'), index.toString());
                        if (!$(this).hasClass('active')) {
                            $(this).addClass('active');
                        }
                        if (!target.hasClass('active')) {
                            target.addClass('active');
                        }
                        $('.locationmap', target).each(function() {
                            eval($(this).metadata().init_function)();
                        });
                    } else {
                        $(this).removeClass('active');
                        target.removeClass('active');
                    }
                });
            });
        });
        
        if (document.location.toString().match('#')) {
            var myAnchor = '#' + document.location.toString().split('#')[1];
            if (myAnchor.length > 1 && $(myAnchor).length > 0 && $('.tabcontainer_navigation a[href="' + myAnchor + '"]', container).length > 0) {
                $('.tabcontainer_navigation a[href="' + myAnchor + '"]', container).click();
            }
        }
        if ($.cookies.get(container.attr('id'))) {
            $($('.tabcontainer_navigation li a', container)[$.cookies.get(container.attr('id'))]).click();
        }
        if (!$('.tabcontainer_navigation a.active', container).length > 0) {
            $('.tabcontainer_navigation li:first a', container).click();
        }
       
        $(this).addClass('loaded');
   });
});

$(function() {
    if (!($.browser.msie && $.browser.version <= 6)) {
        $('#header_promotion > div').wrapAll('<div class="wrapper" style="width:100%; height:100%;" />');
        $('#header_promotion > .wrapper').cycle({fx: 'cover', timeout: 8000});
    }
});

$(function() {
    if (!($.browser.msie && $.browser.version <= 6)) {
        $('#header_images > div').wrapAll('<div class="wrapper" style="width:100%; height:100%;" />');
        $('#header_images > .wrapper').cycle({fx: 'cover', timeout: 8000});
    }
});

$(function() {
    if (!($.browser.msie && $.browser.version <= 6)) {
        $('.slideshow > div').wrapAll('<div class="wrapper" style="width:100%; height:100%;" />');
        $('.slideshow > .wrapper').cycle({fx: 'cover', timeout: 8000});
    }
});
