$(document).ready(function(){

    // Kosár betöltése
    getCartContent();

    // Termék kosárba helyezése
    $('.add-to-cart').live('click', function(){
        var productId = parseInt($(this).attr('rel'));

        var qtyStr = $(this).siblings('.cart-qty').val();
        var qty = parseInt(qtyStr.replace(/[^0-9]*/g, ''));

        addToCart(productId, qty);

        return false;
    });

    $("#m47-shopping-cart-order-button").live('click', function(){
        $('#cart-checkout-tabs').slideToggle(300);
        return false;
    });

    // Oldalválasztó linkként funkcionálásának megszüntetése
    $('.pager-item').live('click', function(){
        if ( ! $(this).hasClass('active'))
        {
            var page = $(this).attr('rel');
            window.location.hash = '#page-'+page
            regenList('page', page, page);
        }
        return false;
    });

    // Másolási sebesség szűrése
    $('#list-page-filter-copy-speed a').live('click', function(){
        var $filter = $(this).attr('rel');
        var $value = $(this).text();
        
        regenList('masolasi_sebesseg_szuro', slugify($filter), $value);

        return false;
    });
    
    // Minden termék
    /*$('.all-products').live('click', function(){
        regenList('all', 1, 1);

        return false;
    });*/
    // Minden termék (bemutatóterem)
    /*$('.all-showroom').live('click', function(){
        regenList('all', 2, 2);

        return false;
    });*/

    // Szín funkció szűrése
    $('#list-page-filter-color a').live('click', function(){
        var $filter = $(this).attr('rel');
        regenList('masolasi_szin', $filter, $filter);

        return false;
    });

    // Másolási méret szűrése
    $('#list-page-filter-copy-size a').live('click', function(){
        var $filter = $(this).attr('rel');
        regenList('masolasi_meret', $filter, $filter);

        return false;
    });

    // Funkciók szűrése
    $('#list-page-filter-function a').live('click', function(){
        var $filter = $(this).attr('rel');
        regenList('funkciok', $filter, $filter);

        return false;
    });

    // Szélességek szűrése
    $('#list-page-filter-width a').live('click', function(){
        var $filter = $(this).attr('rel');
        regenList('szelesseg', $filter.replace('.', '_'), $filter);

        return false;
    });

    // Hosszúságok szűrése
    $('#list-page-filter-height a').live('click', function(){
        var $filter = $(this).attr('rel');
        regenList('hosszusag', $filter.replace('.', '_'), $filter);

        return false;
    });

    // Típusok szűrése
    $('#list-page-filter-brand a').live('click', function(){
        var $filter = $(this).attr('rel');
        regenList('marka', removeAccents($filter).toLowerCase(), $filter);

        return false;
    });

    // Szélességek szűrése
    $('#list-page-filter-gramweight a').live('click', function(){
        var $filter = $(this).attr('rel');
        regenList('grammsuly', $filter, $filter);

        return false;
    });

    // Keresés terméknevekben
    $('#list-page-search form').live('submit', function(){
        var $searchfield = $("#search-list-page");

        if ($searchfield.val().length > 2)
        {
            regenList('search', $searchfield.val(), $searchfield.val())
        }

        return false;
    });

    // Bemutatóterem részletes terméknézet betöltése ajaxszal
    $('.bemutatoterem .list-page-product-details').live('click', function(){
        loadDetailedPage(this);
        return false;
    });
    $('.list-page-prev-next a').live('click', function(){
        loadDetailedPage(this);
        return false;
    });

    // Részletes terméknézet vissza gomb
    $('.list-page-product-details-nav > a').live('click', function(){
        HashTag.set({page: 0, productId: 0});
        
        $('#bemutatoterem-detailed-pane').slideUp(500, function(){
            $('#bemutatoterem-products-pane').slideDown(500);
        });

        return false;
    });

    // Általános leírás és műszaki adatok váltása
    $('.product-tech-spec a').live('click', function(){
        // IE7 hack
        $('.product-details-detail-box').each(function(){
            $(this).css('height', $(this).height());
        });
        $('.product-tech-spec a').removeClass('active');
        var target = $(this).attr('rel');
        $(this).addClass('active');
        $('.product-details-detail-box').slideUp(200, function(){
            $('#'+target).slideDown(200);
        });
        
        return false;
    });

    // Terméknézet megnyitása
    $('.termekek .list-page-product-details').live('click', function(){

        $productdiv = $(this).parents('.list-page-product');
        if ( ! $productdiv.find('.list-page-product-header').hasClass('active'))
        {
            $productdiv.find('.list-page-product-header').addClass('active');
            $productdiv.addClass('active');
            $(this).text('becsuk');
            var newHeight = $productdiv.find('.list-page-product-description-content').height();
            $productdiv.find('.list-page-product-description').animate({height: newHeight+10});
            var newPropHeight = $productdiv.find('.list-page-product-properties-content').height();
            $productdiv.find('.list-page-product-properties').animate({height: newPropHeight+10});
        }
        else
        {
            $productdiv.find('.list-page-product-header').removeClass('active');
            $productdiv.removeClass('active');
            $(this).text('részletek');
            var newHeight = 11;
            $productdiv.find('.list-page-product-description').animate({height: newHeight});
            $productdiv.find('.list-page-product-properties').animate({height: 11});
        }

        return false;
    });
    $('.list-page-product-header').live('click', function(){
        $(this).find('.list-page-product-details').click();
        return false;
    });

    // Darabszám beállításánál a db felirat eltávolítása, majd visszarakása
    $('.cart-qty').live('focus mouseup', function(e){
        if (e.type == 'focusin')
        {
            var value = $(this).val();
            value = value.replace(/[^0-9]+/g, '');
            $(this).val(value);
            $(this).select();
        }
        else if (e.type == 'mouseup')
        {
            return false;
        }
    });
    $('.cart-qty').live('blur', function(){
        var value = $(this).val();
            value = value.replace(/[^0-9]+/g, '');
        $(this).val(value+' db');
    });

    // A kosárban lévő +/- gombokkal változtassuk közvetlenül a darabszámot,
    // és frissítsük a kosár tartalmát és a végösszeget
    $('#m47-shopping-cart-product-list .minus').live('click', function(){
        var product_id = $(this).attr('rel');
        var cart = new ShoppingCart();
        cart.setItem(product_id, -1);
    });
    $('#m47-shopping-cart-product-list .plus').live('click', function(){
        var product_id = $(this).attr('rel');
        var cart = new ShoppingCart();
        cart.setItem(product_id, 1);
    });

    // Plusz-mínusz gombok működése
    $('.minus').live('click', function(){
        // Kijelölés megakadályozása gyors klikkelgetéskor
        $(this).disableSelection();
        $(this).effect('highlight', {color: '#e70033'}, 200);
        var $input = $(this).siblings('.cart-qty');
        var intVal = parseInt($input.val().replace(/[^0-9]+/g, '')) || 0;

        intVal--;
        intVal = Math.max(intVal, 0);
        $input.val(intVal+' db');
        return false;
    });
    $('.plus').live('click', function(){
        // Kijelölés megakadályozása gyors klikkelgetéskor
        $(this).disableSelection();
        $(this).effect('highlight', {color: '#e70033'}, 200);
        var $input = $(this).siblings('.cart-qty');
        var intVal = parseInt($input.val().replace(/[^0-9]+/g, '')) || 0;

        intVal++;
        $input.val(intVal+' db');
        return false;
    });
    
    $('body').data('lastPage', 0);

    // Oldalak eltárolása a historyban, hogy a böngésző funkcióit használhassuk
    $(window).hashchange(function(){
        
        var hashData = HashTag.parse();
        
        var lastPage = parseInt($('body').data('lastPage') || -1);

        if (hashData.page > 0 && hashData.page != lastPage)
        {
            $('body').data('lastPage', hashData.page);
            regenList('page', hashData.page, hashData.page, hashData.productId);
        }
        else if (hashData.page != lastPage)
        {
            $('body').data('lastPage', 1);
            regenList('page', 1, 1, hashData.productId);
        }
        else if (hashData.page == lastPage && hashData.productId > 0)
        {
            var $element = $('.list-page-product-details[rel="product-'+hashData.productId+'"]');
            loadDetailedPage($element, hashData.productId);
        }
        else if (hashData.page == lastPage && hashData.productId <= 0)
        {
            $('.list-page-product-details-nav > a').click();
        }
    });


    
    // Ha van url-ben megadott keresés, search típusú lekérdezést csinálunk
    if ($('#search-list-page').val() !== 'Keresés terméknevekben')
    {
        $('#list-page-search form').submit();
    }
    else
    {
        // Ha esetleg mentett linket nyitnak meg, kezeljük rögtön oldalbetöltéskor
        $(window).hashchange();
    }
});

// Termék adatlap betöltése
function loadDetailedPage(element, elementId)
{
    if (element.length == 0 && typeof elementId !== 'undefined' && elementId > 0)
    {
        var product_id = elementId;
    }
    else
    {
        var product_id = $(element).attr('rel').split('-').pop();
    }

    $.ajax({
        url: '/projectSetup/modules/5/public/ajax/product.inc.php',
        data: {
            type: 'bemutatoterem',
            product_id: product_id
        },
        type: 'POST',
        success: function(html) {
            HashTag.set({page: 0, productId: product_id});
            
            $('#bemutatoterem-detailed-pane').slideUp(500, function(){
                $('#bemutatoterem-detailed-pane').html(html);

                $('#bemutatoterem-products-pane').slideUp(500, function(){
                    $('#bemutatoterem-detailed-pane').slideDown(500);
                    $.scrollTo($('.list-page-product.product-details'), 500);
                });
            });
        },
        dataType: 'html'
    });
}

//listát ujrageneráló funkctió
function regenList(strType, numID, strSessionName, productId)
{
    // Ha nem lapozunk, állítsuk alaphelyzetbe a hashtaget
    if (strType != 'page')
    {
        window.location.hash = '';
    }

    if (typeof(strSessionName) == 'undefined')
    {
        var $item = $('#'+strType+'-'+numID);
        strSessionName = $item.find('span').html();
    }

    var state = setActiveListItem(strType, numID);
    getListContent(strType, strSessionName, state, productId);
}

function getListContent(strType, strSessionNam, state, productId)
{
    var state = state || 'inactive';
    var pageType = $('#page-type').val();

    $("#list-page-products").html("<div class=\"m47_preload\"><div><img src=\"/projectSetup/images/ajax-loader.gif\" alt=\"Betöltés...\" /></div><div><p>Kis türelmet, töltöm az adatokat...</p></div></div>");

    $.ajax({
        type: "POST",
        url: "/projectSetup/modules/5/public/ajax/a_inc.php",
        data: {
            type:       strType,
            value:      strSessionNam,
            state:      state,
            pageType:   pageType
        },
        success: function(msg){
            $("#list-page-products").html(msg);
            
            if (typeof productId !== 'undefined' && productId > 0)
            {
                loadDetailedPage($(), productId);
            }
        }
    });
}

//aktív elem beállítása az adott listában
function setActiveListItem (strType, numID)
{
    var $item = $('#'+strType+'-'+numID);
    
    // Minden termék megjelenítése, töröljük a szűrőket
    if (strType == 'all')
    {
        $("#search-list-page").val('Keresés terméknevekben');
        $('.filter-item').removeClass('active');
        return 'inactive';
    }

    // Szabadszavas keresésnél a többi filter megszűnik
    if (strType == 'search')
    {
        $('.filter-item').removeClass('active');
    }
    // Filter használatánál a szabadszavas keresés befejeződik
    else
    {
        $("#search-list-page").val('Keresés terméknevekben');
    }

    // Ha a funkciókat állítjuk, működjön toggleként
    if (strType === 'funkciok')
    {
        if ( ! $item.hasClass("active"))
        {
            $item.addClass("active");
            return "inactive";
        }
        else
        {
            $item.removeClass("active");
            return "active";
        }
    }

    if ( ! $item.hasClass("active"))
    {
        // Az egyéni elemek inaktívvá tétele
        $("[id^=" + strType + "]").each(function() {
            $(this).removeClass("active");
        });

        $item.addClass("active");
        
        return "inactive";
    }
    else
    {
        $item.removeClass("active");

        return "active";
    }
}

//kosár tartalmának ajaxos visszatöltését végző funkció
function getChartContent() {
	$.ajax({
		type: "POST",
		url: "/projectSetup/modules/5/public/shoppingCart/inc.php",
		data: "",
		success: function(msg){
			$(".m47_basket_container").html(msg);
		}
	});
}


function makeUrl(value)
{
    if (typeof value == 'undefined' || value == null)
    {
        return '';
    }

    var stripped_value = value.replace(/[^\w]/ig, '');
    stripped_value = stripped_value.replace(/\*/ig, '_');
    return stripped_value;
}

// kosár tartalmának ajaxos betöltését végző funkció
function getCartContent()
{
    $.ajax({
        type: "POST",
        url: "/projectSetup/modules/5/public/shoppingCart/inc.php",
        success: function(msg){
            $("#cart").html(msg);
        }
    });
}
// termék kosárba tétele
function addToCart(itemId, qty)
{
    qty = qty || 1;
    qty = parseInt(qty);

    var cart = new ShoppingCart();
    cart.setItem(itemId, qty);

    $('#tab-line').tabs('option', 'selected', 5);
    $.scrollTo('#tab-line');
}

/**
 * jQuery.ScrollTo - Easy element scrolling using jQuery.
 * Copyright (c) 2007-2009 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
 * Dual licensed under MIT and GPL.
 * Date: 5/25/2009
 * @author Ariel Flesler
 * @version 1.4.2
 *
 * http://flesler.blogspot.com/2007/10/jqueryscrollto.html
 */
;(function(d){var k=d.scrollTo=function(a,i,e){d(window).scrollTo(a,i,e)};k.defaults={axis:'xy',duration:parseFloat(d.fn.jquery)>=1.3?0:1};k.window=function(a){return d(window)._scrollable()};d.fn._scrollable=function(){return this.map(function(){var a=this,i=!a.nodeName||d.inArray(a.nodeName.toLowerCase(),['iframe','#document','html','body'])!=-1;if(!i)return a;var e=(a.contentWindow||a).document||a.ownerDocument||a;return d.browser.safari||e.compatMode=='BackCompat'?e.body:e.documentElement})};d.fn.scrollTo=function(n,j,b){if(typeof j=='object'){b=j;j=0}if(typeof b=='function')b={onAfter:b};if(n=='max')n=9e9;b=d.extend({},k.defaults,b);j=j||b.speed||b.duration;b.queue=b.queue&&b.axis.length>1;if(b.queue)j/=2;b.offset=p(b.offset);b.over=p(b.over);return this._scrollable().each(function(){var q=this,r=d(q),f=n,s,g={},u=r.is('html,body');switch(typeof f){case'number':case'string':if(/^([+-]=)?\d+(\.\d+)?(px|%)?$/.test(f)){f=p(f);break}f=d(f,this);case'object':if(f.is||f.style)s=(f=d(f)).offset()}d.each(b.axis.split(''),function(a,i){var e=i=='x'?'Left':'Top',h=e.toLowerCase(),c='scroll'+e,l=q[c],m=k.max(q,i);if(s){g[c]=s[h]+(u?0:l-r.offset()[h]);if(b.margin){g[c]-=parseInt(f.css('margin'+e))||0;g[c]-=parseInt(f.css('border'+e+'Width'))||0}g[c]+=b.offset[h]||0;if(b.over[h])g[c]+=f[i=='x'?'width':'height']()*b.over[h]}else{var o=f[h];g[c]=o.slice&&o.slice(-1)=='%'?parseFloat(o)/100*m:o}if(/^\d+$/.test(g[c]))g[c]=g[c]<=0?0:Math.min(g[c],m);if(!a&&b.queue){if(l!=g[c])t(b.onAfterFirst);delete g[c]}});t(b.onAfter);function t(a){r.animate(g,j,b.easing,a&&function(){a.call(this,n,b)})}}).end()};k.max=function(a,i){var e=i=='x'?'Width':'Height',h='scroll'+e;if(!d(a).is('html,body'))return a[h]-d(a)[e.toLowerCase()]();var c='client'+e,l=a.ownerDocument.documentElement,m=a.ownerDocument.body;return Math.max(l[h],m[h])-Math.min(l[c],m[c])};function p(a){return typeof a=='object'?a:{top:a,left:a}}})(jQuery);

/*
 * jQuery hashchange event - v1.3 - 7/21/2010
 * http://benalman.com/projects/jquery-hashchange-plugin/
 * 
 * Copyright (c) 2010 "Cowboy" Ben Alman
 * Dual licensed under the MIT and GPL licenses.
 * http://benalman.com/about/license/
 */
(function($,e,b){var c="hashchange",h=document,f,g=$.event.special,i=h.documentMode,d="on"+c in e&&(i===b||i>7);function a(j){j=j||location.href;return"#"+j.replace(/^[^#]*#?(.*)$/,"$1")}$.fn[c]=function(j){return j?this.bind(c,j):this.trigger(c)};$.fn[c].delay=50;g[c]=$.extend(g[c],{setup:function(){if(d){return false}$(f.start)},teardown:function(){if(d){return false}$(f.stop)}});f=(function(){var j={},p,m=a(),k=function(q){return q},l=k,o=k;j.start=function(){p||n()};j.stop=function(){p&&clearTimeout(p);p=b};function n(){var r=a(),q=o(m);if(r!==m){l(m=r,q);$(e).trigger(c)}else{if(q!==m){location.href=location.href.replace(/#.*/,"")+q}}p=setTimeout(n,$.fn[c].delay)}$.browser.msie&&!d&&(function(){var q,r;j.start=function(){if(!q){r=$.fn[c].src;r=r&&r+a();q=$('<iframe tabindex="-1" title="empty"/>').hide().one("load",function(){r||l(a());n()}).attr("src",r||"javascript:0").insertAfter("body")[0].contentWindow;h.onpropertychange=function(){try{if(event.propertyName==="title"){q.document.title=h.title}}catch(s){}}}};j.stop=k;o=function(){return a(q.location.href)};l=function(v,s){var u=q.document,t=$.fn[c].domain;if(v!==s){u.title=h.title;u.open();t&&u.write('<script>document.domain="'+t+'"<\/script>');u.close();q.location.hash=v}}})();return j})()})(jQuery,this);

/**
 * A php in_array funkciójához hasonló funkcionalitás
 * hozzáadása a javascript tömbökhöz
 */
Array.prototype.contains = function(obj) {
    var i = this.length;
    while (i--) {
        if (this[i] === obj) {
            return true;
        }
    }
    return false;
}

function slugify(text) {
	text = text.replace(/[^-a-zA-Z0-9,&\s]+/ig, '');
	text = text.replace(/[\s-]+/gi, "-");
	text = text.replace(/\s/gi, "");
	return text;
}

function urlize(text) {
    text = removeAccents(text.toLowerCase());
	text = text.replace(/[^-a-zA-Z0-9,&\s]+/ig, '-');
	text = text.replace(/[\s-]+/gi, "-");
	text = text.replace(/\s/gi, "");
	return text;
}

function removeAccents(text)
{
    var accents = [new RegExp(encodeURI('À'), 'g'), new RegExp(encodeURI('Á'), 'g'), new RegExp(encodeURI('Â'), 'g'), new RegExp(encodeURI('Ã'), 'g'), new RegExp(encodeURI('Ä'), 'g'), new RegExp(encodeURI('Å'), 'g'), new RegExp(encodeURI('Æ'), 'g'), new RegExp(encodeURI('Ç'), 'g'), new RegExp(encodeURI('È'), 'g'), new RegExp(encodeURI('É'), 'g'), new RegExp(encodeURI('Ê'), 'g'), new RegExp(encodeURI('Ë'), 'g'), new RegExp(encodeURI('Ì'), 'g'), new RegExp(encodeURI('Í'), 'g'), new RegExp(encodeURI('Î'), 'g'), new RegExp(encodeURI('Ï'), 'g'), new RegExp(encodeURI('Ð'), 'g'), new RegExp(encodeURI('Ñ'), 'g'), new RegExp(encodeURI('Ò'), 'g'), new RegExp(encodeURI('Ó'), 'g'), new RegExp(encodeURI('Ô'), 'g'), new RegExp(encodeURI('Õ'), 'g'), new RegExp(encodeURI('Ö'), 'g'), new RegExp(encodeURI('Ø'), 'g'), new RegExp(encodeURI('Ù'), 'g'), new RegExp(encodeURI('Ú'), 'g'), new RegExp(encodeURI('Û'), 'g'), new RegExp(encodeURI('Ü'), 'g'), new RegExp(encodeURI('Ý'), 'g'), new RegExp(encodeURI('ß'), 'g'), new RegExp(encodeURI('à'), 'g'), new RegExp(encodeURI('á'), 'g'), new RegExp(encodeURI('â'), 'g'), new RegExp(encodeURI('ã'), 'g'), new RegExp(encodeURI('ä'), 'g'), new RegExp(encodeURI('å'), 'g'), new RegExp(encodeURI('æ'), 'g'), new RegExp(encodeURI('ç'), 'g'), new RegExp(encodeURI('è'), 'g'), new RegExp(encodeURI('é'), 'g'), new RegExp(encodeURI('ê'), 'g'), new RegExp(encodeURI('ë'), 'g'), new RegExp(encodeURI('ì'), 'g'), new RegExp(encodeURI('í'), 'g'), new RegExp(encodeURI('î'), 'g'), new RegExp(encodeURI('ï'), 'g'), new RegExp(encodeURI('ñ'), 'g'), new RegExp(encodeURI('ò'), 'g'), new RegExp(encodeURI('ó'), 'g'), new RegExp(encodeURI('ô'), 'g'), new RegExp(encodeURI('õ'), 'g'), new RegExp(encodeURI('ö'), 'g'), new RegExp(encodeURI('ø'), 'g'), new RegExp(encodeURI('ù'), 'g'), new RegExp(encodeURI('ú'), 'g'), new RegExp(encodeURI('û'), 'g'), new RegExp(encodeURI('ü'), 'g'), new RegExp(encodeURI('ý'), 'g'), new RegExp(encodeURI('ÿ'), 'g'), new RegExp(encodeURI('Ā'), 'g'), new RegExp(encodeURI('ā'), 'g'), new RegExp(encodeURI('Ă'), 'g'), new RegExp(encodeURI('ă'), 'g'), new RegExp(encodeURI('Ą'), 'g'), new RegExp(encodeURI('ą'), 'g'), new RegExp(encodeURI('Ć'), 'g'), new RegExp(encodeURI('ć'), 'g'), new RegExp(encodeURI('Ĉ'), 'g'), new RegExp(encodeURI('ĉ'), 'g'), new RegExp(encodeURI('Ċ'), 'g'), new RegExp(encodeURI('ċ'), 'g'), new RegExp(encodeURI('Č'), 'g'), new RegExp(encodeURI('č'), 'g'), new RegExp(encodeURI('Ď'), 'g'), new RegExp(encodeURI('ď'), 'g'), new RegExp(encodeURI('Đ'), 'g'), new RegExp(encodeURI('đ'), 'g'), new RegExp(encodeURI('Ē'), 'g'), new RegExp(encodeURI('ē'), 'g'), new RegExp(encodeURI('Ĕ'), 'g'), new RegExp(encodeURI('ĕ'), 'g'), new RegExp(encodeURI('Ė'), 'g'), new RegExp(encodeURI('ė'), 'g'), new RegExp(encodeURI('Ę'), 'g'), new RegExp(encodeURI('ę'), 'g'), new RegExp(encodeURI('Ě'), 'g'), new RegExp(encodeURI('ě'), 'g'), new RegExp(encodeURI('Ĝ'), 'g'), new RegExp(encodeURI('ĝ'), 'g'), new RegExp(encodeURI('Ğ'), 'g'), new RegExp(encodeURI('ğ'), 'g'), new RegExp(encodeURI('Ġ'), 'g'), new RegExp(encodeURI('ġ'), 'g'), new RegExp(encodeURI('Ģ'), 'g'), new RegExp(encodeURI('ģ'), 'g'), new RegExp(encodeURI('Ĥ'), 'g'), new RegExp(encodeURI('ĥ'), 'g'), new RegExp(encodeURI('Ħ'), 'g'), new RegExp(encodeURI('ħ'), 'g'), new RegExp(encodeURI('Ĩ'), 'g'), new RegExp(encodeURI('ĩ'), 'g'), new RegExp(encodeURI('Ī'), 'g'), new RegExp(encodeURI('ī'), 'g'), new RegExp(encodeURI('Ĭ'), 'g'), new RegExp(encodeURI('ĭ'), 'g'), new RegExp(encodeURI('Į'), 'g'), new RegExp(encodeURI('į'), 'g'), new RegExp(encodeURI('İ'), 'g'), new RegExp(encodeURI('ı'), 'g'), new RegExp(encodeURI('Ĳ'), 'g'), new RegExp(encodeURI('ĳ'), 'g'), new RegExp(encodeURI('Ĵ'), 'g'), new RegExp(encodeURI('ĵ'), 'g'), new RegExp(encodeURI('Ķ'), 'g'), new RegExp(encodeURI('ķ'), 'g'), new RegExp(encodeURI('Ĺ'), 'g'), new RegExp(encodeURI('ĺ'), 'g'), new RegExp(encodeURI('Ļ'), 'g'), new RegExp(encodeURI('ļ'), 'g'), new RegExp(encodeURI('Ľ'), 'g'), new RegExp(encodeURI('ľ'), 'g'), new RegExp(encodeURI('Ŀ'), 'g'), new RegExp(encodeURI('ŀ'), 'g'), new RegExp(encodeURI('Ł'), 'g'), new RegExp(encodeURI('ł'), 'g'), new RegExp(encodeURI('Ń'), 'g'), new RegExp(encodeURI('ń'), 'g'), new RegExp(encodeURI('Ņ'), 'g'), new RegExp(encodeURI('ņ'), 'g'), new RegExp(encodeURI('Ň'), 'g'), new RegExp(encodeURI('ň'), 'g'), new RegExp(encodeURI('ŉ'), 'g'), new RegExp(encodeURI('Ō'), 'g'), new RegExp(encodeURI('ō'), 'g'), new RegExp(encodeURI('Ŏ'), 'g'), new RegExp(encodeURI('ŏ'), 'g'), new RegExp(encodeURI('Ő'), 'g'), new RegExp(encodeURI('ő'), 'g'), new RegExp(encodeURI('Œ'), 'g'), new RegExp(encodeURI('œ'), 'g'), new RegExp(encodeURI('Ŕ'), 'g'), new RegExp(encodeURI('ŕ'), 'g'), new RegExp(encodeURI('Ŗ'), 'g'), new RegExp(encodeURI('ŗ'), 'g'), new RegExp(encodeURI('Ř'), 'g'), new RegExp(encodeURI('ř'), 'g'), new RegExp(encodeURI('Ś'), 'g'), new RegExp(encodeURI('ś'), 'g'), new RegExp(encodeURI('Ŝ'), 'g'), new RegExp(encodeURI('ŝ'), 'g'), new RegExp(encodeURI('Ş'), 'g'), new RegExp(encodeURI('ş'), 'g'), new RegExp(encodeURI('Š'), 'g'), new RegExp(encodeURI('š'), 'g'), new RegExp(encodeURI('Ţ'), 'g'), new RegExp(encodeURI('ţ'), 'g'), new RegExp(encodeURI('Ť'), 'g'), new RegExp(encodeURI('ť'), 'g'), new RegExp(encodeURI('Ŧ'), 'g'), new RegExp(encodeURI('ŧ'), 'g'), new RegExp(encodeURI('Ũ'), 'g'), new RegExp(encodeURI('ũ'), 'g'), new RegExp(encodeURI('Ū'), 'g'), new RegExp(encodeURI('ū'), 'g'), new RegExp(encodeURI('Ŭ'), 'g'), new RegExp(encodeURI('ŭ'), 'g'), new RegExp(encodeURI('Ů'), 'g'), new RegExp(encodeURI('ů'), 'g'), new RegExp(encodeURI('Ű'), 'g'), new RegExp(encodeURI('ű'), 'g'), new RegExp(encodeURI('Ų'), 'g'), new RegExp(encodeURI('ų'), 'g'), new RegExp(encodeURI('Ŵ'), 'g'), new RegExp(encodeURI('ŵ'), 'g'), new RegExp(encodeURI('Ŷ'), 'g'), new RegExp(encodeURI('ŷ'), 'g'), new RegExp(encodeURI('Ÿ'), 'g'), new RegExp(encodeURI('Ź'), 'g'), new RegExp(encodeURI('ź'), 'g'), new RegExp(encodeURI('Ż'), 'g'), new RegExp(encodeURI('ż'), 'g'), new RegExp(encodeURI('Ž'), 'g'), new RegExp(encodeURI('ž'), 'g'), new RegExp(encodeURI('ſ'), 'g'), new RegExp(encodeURI('ƒ'), 'g'), new RegExp(encodeURI('Ơ'), 'g'), new RegExp(encodeURI('ơ'), 'g'), new RegExp(encodeURI('Ư'), 'g'), new RegExp(encodeURI('ư'), 'g'), new RegExp(encodeURI('Ǎ'), 'g'), new RegExp(encodeURI('ǎ'), 'g'), new RegExp(encodeURI('Ǐ'), 'g'), new RegExp(encodeURI('ǐ'), 'g'), new RegExp(encodeURI('Ǒ'), 'g'), new RegExp(encodeURI('ǒ'), 'g'), new RegExp(encodeURI('Ǔ'), 'g'), new RegExp(encodeURI('ǔ'), 'g'), new RegExp(encodeURI('Ǖ'), 'g'), new RegExp(encodeURI('ǖ'), 'g'), new RegExp(encodeURI('Ǘ'), 'g'), new RegExp(encodeURI('ǘ'), 'g'), new RegExp(encodeURI('Ǚ'), 'g'), new RegExp(encodeURI('ǚ'), 'g'), new RegExp(encodeURI('Ǜ'), 'g'), new RegExp(encodeURI('ǜ'), 'g'), new RegExp(encodeURI('Ǻ'), 'g'), new RegExp(encodeURI('ǻ'), 'g'), new RegExp(encodeURI('Ǽ'), 'g'), new RegExp(encodeURI('ǽ'), 'g'), new RegExp(encodeURI('Ǿ'), 'g'), new RegExp(encodeURI('ǿ'), 'g')];
    var clean   = ['A', 'A', 'A', 'A', 'A', 'A', 'AE', 'C', 'E', 'E', 'E', 'E', 'I', 'I', 'I', 'I', 'D', 'N', 'O', 'O', 'O', 'O', 'O', 'O', 'U', 'U', 'U', 'U', 'Y', 's', 'a', 'a', 'a', 'a', 'a', 'a', 'ae', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', 'n', 'o', 'o', 'o', 'o', 'o', 'o', 'u', 'u', 'u', 'u', 'y', 'y', 'A', 'a', 'A', 'a', 'A', 'a', 'C', 'c', 'C', 'c', 'C', 'c', 'C', 'c', 'D', 'd', 'D', 'd', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'G', 'g', 'G', 'g', 'G', 'g', 'G', 'g', 'H', 'h', 'H', 'h', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'IJ', 'ij', 'J', 'j', 'K', 'k', 'L', 'l', 'L', 'l', 'L', 'l', 'L', 'l', 'l', 'l', 'N', 'n', 'N', 'n', 'N', 'n', 'n', 'O', 'o', 'O', 'o', 'O', 'o', 'OE', 'oe', 'R', 'r', 'R', 'r', 'R', 'r', 'S', 's', 'S', 's', 'S', 's', 'S', 's', 'T', 't', 'T', 't', 'T', 't', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'W', 'w', 'Y', 'y', 'Y', 'Z', 'z', 'Z', 'z', 'Z', 'z', 's', 'f', 'O', 'o', 'U', 'u', 'A', 'a', 'I', 'i', 'O', 'o', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'A', 'a', 'AE', 'ae', 'O', 'o'];

    text = encodeURI(text);
    
    for (i in accents)
    {
        text = text.replace(accents[i], clean[i]);
    }

    return decodeURI(text);
}

var HashTag = {
    parse:  function(){
        var hash = window.location.hash;

        hash = hash.replace(/#/, '');
        var hashArray = hash.split('/');

        if (typeof hashArray.shift === 'function')
        {
            var page = parseInt(hashArray.shift().split('-').pop() || 1);
        }
        else
        {
            var page = 1;
        }
        
        if (typeof hashArray.shift === 'function' && hashArray.length !== 0)
        {
            var productId = parseInt(hashArray.shift().split('-').shift() || 0);
        }
        else
        {
            var productId = 0;
        }

        return {page: page, productId: productId};
    },
    
    set: function(data){
        
        var currentParams = HashTag.parse();
        var newParams = currentParams;
        
        if (data.page !== 0)
        {
            newParams.page = data.page;
        }
        
        if (data.productId !== 0)
        {
            newParams.productId = data.productId;
            
            var $element = $('.list-page-product-details[rel="product-'+data.productId+'"]');
            var $h3 = $element.siblings('h3');
            var productName = urlize($h3.text());
        }
        
        window.location.hash = '#page-'+newParams.page+((data.productId !== 0) ? '/'+newParams.productId+'-'+productName : '');
    }
}
            
