var MIGTYRES;
if (!MIGTYRES) MIGTYRES = {};

MIGTYRES.catalogP = function() {
    var w = $(document).width();
    var p = 4;

    if (w >= 1000 && w <= 1200) {
        p = 5;
    } else if (w >= 1201 && w <= 1350) {
        p = 1.7;
    } else if (w >= 1351 && w <= 1600) {
        p = 6;

        if ($.browser.msie && $.browser.version == 6) {
            p = 3.6;
        }
    } else {
        p = 3;
    }

    $('head').append('<style type="text/css">#c-c .cat-item {padding-right:' + p + '%;}</style>');
};

MIGTYRES.placeHolder = function(input, value) {
    var val = value || '';

    var defVal = $(input).attr('value');
    defVal = jQuery.trim(defVal);

    if (defVal == '' || defVal == val) {
        $(input).addClass('placeholder').attr('value', val);
    }

    $(input).focus(function(){
        if ($(this).val() == val) {
            $(this).val('').removeClass('placeholder');
        }
    });

    $(input).blur(function(){
        if (jQuery.trim($(this).val()) == '') {
            $(this).addClass('placeholder').attr('value', val);
        }
    });
};

MIGTYRES.authLink = function() {
    $('#auth').replaceWith('<a href="#TB_inline?height=249&width=310&inlineId=auth-form&cls=TB_closeWindowButton" class="thickbox js">Âîéòè</a>');
};

MIGTYRES.splashBlockHtml = function(data) {
    var box = '<div class="splash-block"><div class="block">';
    box += '<div class="close"><img src="/images/body/ico-close-box.gif" alt="" width="16" height="15" /></div>';
    box += '<div class="t png"></div><div class="r png"><div class="l png">' + data + '</div></div><div class="b png"></div><div class="lt png"></div><div class="rt png"></div><div class="lb png"></div><div class="rb png"></div></div></div>';

    return box;
};

MIGTYRES.splashBlock = function() {
    $('.cat-item div.price, #detail div.price').each(function() {
        var t = $(this);

        var priceC = t.find('div').html();
        if(!priceC)
            return;

        var block = MIGTYRES.splashBlockHtml(priceC);

        $(this).find('div:last').remove();

        $(this).find('strong:first').addClass('js').click(function(e) {
            e.stopPropagation();
            e.preventDefault();
            $(t).find('.block').show();
        });

        $(this).append(block)
        $(this).find('.close').click(function(e) {
            e.stopPropagation();
            e.preventDefault();
            $(this).parent().hide();
        });
    });
};

MIGTYRES.showHint = function() {
    $('.cat-item .splash-block, #detail .splash-block').hover(
        function(e) {
            e.stopPropagation();
            e.preventDefault();
            $(this).find('.block').show();
            $(this).parent().css('z-index', '1000')
        },
        function(e) {
            e.stopPropagation();
            e.preventDefault();
            $(this).find('.block').hide();
            $(this).parent().css('z-index', '200');
        }
    ).find('img.png').attr('title', '');
};

MIGTYRES.adjustAutoModel = function(selectedItem) {
    var markaValue = $('#prodauto').val();
    var modelValue = $('#modelauto');
    var modelYear = $('#year');
    var modelMod = $('#modification');

	modelYear.attr("disabled",true);
	modelYear.emptySelect();
	modelMod.attr("disabled",true);
	modelMod.emptySelect();

    if (markaValue.length == 0) {
        modelValue.attr("disabled",true);
        modelValue.emptySelect();
    }else{
		modelValue.attr("disabled",false);
		$.getJSON('/ajax/auto_.php', {vendor:markaValue, selected:selectedItem}, function(data){modelValue.loadSelect(data);});
	}
};

MIGTYRES.adjustAutoYear = function(selectedItem, selectedItemModel) {
    var markaValue = $('#prodauto').val();
    var modelValue = $('#modelauto').val(); if(modelValue.length == 0&&selectedItemModel.length != 0) modelValue = selectedItemModel;
    var modelYear = $('#year');
    var modelMod = $('#modification');

	modelMod.attr("disabled",true);
	modelMod.emptySelect();

    if (modelValue.length == 0) {
        modelYear.attr("disabled",true);
        modelYear.emptySelect();
    }else{
		modelYear.attr("disabled",false);
		$.getJSON('/ajax/auto_.php', {vendor:markaValue, model:modelValue, selected:selectedItem}, function(data){modelYear.loadSelect(data);});
	}
};

MIGTYRES.adjustAutoMod = function(selectedItem, selectedItemYear, selectedItemModel) {
    var markaValue = $('#prodauto').val();
    var modelValue = $('#modelauto').val(); if(modelValue.length == 0&&selectedItemModel.length != 0) modelValue = selectedItemModel;
    var modelYear = $('#year').val(); if(modelYear.length == 0&&selectedItemYear.length != 0) modelYear = selectedItemYear;
    var modelMod = $('#modification');

    if (modelYear.length == 0) {
        modelMod.attr("disabled",true);
        modelMod.emptySelect();
    }else{
		modelMod.attr("disabled",false);
		$.getJSON('/ajax/auto_.php', {vendor:markaValue, model:modelValue, year:modelYear, selected:selectedItem}, function(data){modelMod.loadSelect(data);});
	}
};

MIGTYRES.adjustDealRest = function (selectedItem) {
    var districtValue = $('#deal_raiony').val();
    var restValue = $('#deal_rest');
    if (districtValue.length == 0) {
        restValue.attr("disabled",true);
        restValue.emptySelect();
    }
    else {
        restValue.removeClass('error').attr("disabled",false);
        $.getJSON(
            '/ajax/deal.php',
            {value:districtValue, selected:selectedItem},
            function(data){
              restValue.loadSelect(data);
            }
        );
    }
}

MIGTYRES.calcProd2Basket = function (selectedItem) {
    var allProdPrice = 0;
    var allPriceObj = $('#all_prod_selected');
    var checkedObj = $('.cat-item :checked');

    checkedObj.each(
        function () {
            var id = $(this).attr('value');
            var prodPriceCheck = parseInt($('#prc-' + id).text());
            var quantity = parseInt($('#qntt-' + id).attr('value'));
            if(!isNaN(prodPriceCheck)) {
                if(isNaN(quantity) || quantity<=0)
                    quantity = 1;

                allProdPrice = allProdPrice + prodPriceCheck*quantity;
            } else {
                $(this)[0].checked = false;
            }
        }
    );

    allPriceObj.text(allProdPrice + ' ð');
}

MIGTYRES.hyph = {
    tags: '#c-c p',
    tshy: '&shy;',
    shy: null,
    spec: '[éüú]',
    vovel: '[àå¸èîóûýþÿaeiouy]',
    consonant: '(?:sh|ch|qu|[áâãäæçêëìíïðñòôõö÷øùbcdfghjklmnpqrstvwxz])',
    letters: '[üúéàå¸èîóûýþÿáâãäæçéêëìíïðñòôõö÷øùúüaeiouybcdfghjklmnpqrstvwxz]',
    rules: null,
    elements: [],

    getApplyMethod: function() {
        return 'text-align:justify;';
    },

    hyphenizeText: function(text) {
        for (var i = this.rules.length - 1; i >= 0; i--) {
            var ru = this.rules[i];

            if (!ru) {
                continue;
            }

            var re = new RegExp('(' + ru[0] + ')(?=' + ru[1] + ')', 'gi');

            text = text.replace(re, '$1' + this.shy);
        }

        return text;
    },

    hyphenize: function(node) {
        var n = node;
        var children = n.childNodes;
        var parent = node.parentNode;

        if (node.nodeType == 3) {
            if (!node.nodeValue) {
                return;
            }

            node.nodeValue = this.hyphenizeText(node.nodeValue);
            $(parent).attr('style', this.getApplyMethod());
        }

        for (var i = 0; i < children.length; i++) {
            this.hyphenize(children[i]);
        }
    },

    exe: function() {
        this._init();

        this.elements = $(this.tags);

        if (this.elements.length <= 0) {
            return;
        }

        for (var e = 0; e < this.elements.length; e++) {
            this.hyphenize(this.elements[e]);
        }
    },

    _init: function() {
        if (!this.shy) {
            var node = document.createElement('span');
            node.innerHTML = this.tshy;

            this.shy = node.childNodes[0].nodeValue;

            with (this) {
                rules = [[consonant + vovel, vovel + letters],
                         [vovel + consonant, consonant + vovel],
                         [consonant + vovel, consonant + vovel],
                         [vovel + consonant, consonant + consonant + vovel],
                         [vovel + consonant + consonant, consonant + vovel],
                         [vovel + consonant + consonant, consonant + consonant + vovel],
                         [spec, letters + letters],
                         null
                        ];
            }
        }
    }
};
