﻿
jQuery.fn.onImagesLoaded = function (_cb) {
    return this.each(function () {

        var $imgs = (this.tagName.toLowerCase() === 'img') ? $(this) : $('img', this),
        _cont = this,
            i = 0,
    _done = function () {
        if (typeof _cb === 'function') _cb(_cont);
    };

        if ($imgs.length) {
            $imgs.each(function () {
                var _img = this,
        _checki = function (e) {
            if ((_img.complete) || (_img.readyState == 'complete' && e.type == 'readystatechange')) {
                if (++i === $imgs.length) _done();
            }
            else if (_img.readyState === undefined) {
                $(_img).attr('src', $(_img).attr('src')); // re-fire load event
            }
        }; // _checki \\

                $(_img).bind('load readystatechange', function (e) { _checki(e); });
                _checki({ type: 'readystatechange' }); // bind to 'load' event...
            });
        } else _done();
    });
};    



/*
*Постороение ссылки на поиск
*/
function WebSearch(query) {
    alert(query);
    return "http://rynok.yottos.com.ua/ViewHistory.aspx?id=" + query;
}

function ShowHideSuggest() {
    if (!$ShowHideSuggest) return;
    if ($leftSuggest < 4) return;
    var $_h = $leftSuggest * 28 + "px";    
    var h = $("#divWebSuggest").css("height");
    if (h == $_h) {
        $("#divWebSuggest").css("height", "105px");
        $("#imgDown").show();
        $("#imgUp").hide();
    } else {
        $("#divWebSuggest").css("height", $_h);
        $("#imgDown").hide();
        $("#imgUp").show();
    }    
}
function ShowHideYourRequest() {
    //alert($ShowHideYourRequest);
    if (!$ShowHideYourRequest) return;
    var $_h = $yourSuggest * 28 + "px";
    var h = $("#divYourSuggest").css("height");
    if (h == $_h) {
        $("#divYourSuggest").css("height", "105px");
        $("#img1").show();
        $("#img2").hide();
    } else {
        $("#divYourSuggest").css("height", $_h);
        $("#img1").hide();
        $("#img2").show();
    }
}
function getClientWidth() {
    return document.compatMode == 'CSS1Compat' && !window.opera ? document.documentElement.clientWidth : document.body.clientWidth;
}
function buildSearchUrl(sender) {
    function getSearchUrl(href) {
        var r = /yottos\.(com\.ua|ru|com)/;
        var domain = window.location.host.match(r);
        var host = domain ? ('http://' + domain[0] + '/') : 'http://yottos.ru/';
        var lang = domain ? domain[1] : 'ru';
        var link = '';
        var txtControl = document.getElementById("searchString") ||
                         document.getElementById("QueryTextBox") ||
                         document.getElementById("ctl00_SearchString1_QueryTextBox") ||
                         document.getElementById("ctl00_QueryTextBox") || 
                         document.getElementById("SearchString_QueryTextBox");
        if (!txtControl || !txtControl.value) return href;
        var query = txtControl.value.replace(/[:;]/g, '');
        
        switch (lang) {
            case 'com.ua':
                link = (href.match('news\.yottos\.') && ("http://news.yottos.com.ua/Поиск?" + query)) ||
                       (href.match('rynok\.yottos\.') && ("http://rynok.yottos.com.ua/поиск/" + query)) ||
                       (href.match('catalog\.yottos\.') && ("http://catalog.yottos.com.ua/yottos-каталог/Поиск/" + query)) ||
                       (href.match('zero\.yottos\.') && ("http://zero.yottos.com.ua/Zero.aspx?q=" + query)) ||
                       ("http://yottos.com.ua/Результат?" + query);
                break;
            case 'com':
                link = (href.match('news\.yottos\.') && ("http://news.yottos.com/Поиск?" + query)) ||
                       (href.match('rynok\.yottos\.') && ("http://rynok.yottos.com/поиск/" + query)) ||
                       (href.match('catalog\.yottos\.') && ("http://catalog.yottos.com/yottos-каталог/Поиск/" + query)) ||
                       (href.match('zero\.yottos\.') && ("http://zero.yottos.com/Zero.aspx?q=" + query)) ||
                       ("http://yottos.com/Результат?" + query);
                break;
            case 'ru':
            default:
                link = (href.match('news\.yottos\.') && ("http://news.yottos.ru/Поиск?" + query)) ||
                       (href.match('rynok\.yottos\.') && ("http://rynok.yottos.ru/поиск/" + query)) ||
                       (href.match('catalog\.yottos\.') && ("http://catalog.yottos.ru/yottos-каталог/Поиск/" + query)) ||
                       (href.match('zero\.yottos\.') && ("http://zero.yottos.ru/Zero.aspx?q=" + query)) ||
                       ("http://yottos.ru/Результат?" + query);
                break;
        }
        return query ? link : (link.match(/^http:\/\/[^\/]*\//)[0] || link);           // Если запрос пустой, возвращает ссылку на домен
    }

    sender.href = getSearchUrl(sender.href);
    return true;
}





/*




var webSearchPrefix = "/Результат?";
var zeroSearchPrefix = "Zero.aspx?q=";
var newsSearchPrefix = "/AllNews.aspx?q=";
var catalogSearchPrefix = "/CatalogSearch.aspx?q=";
var rynokSearchPrefix = "/AllResult.aspx?q=";

function buildSearchQuery_Transformed(hyperLink, searchPerfix) {
    var newUrl = 'http://' + hyperLink.href.match(/:\/\/(.[^\/]+)/)[1];
    var searchText = "";
    var searchTextBox = document.form1.QueryTextBox;    
    if (searchTextBox == null) searchTextBox = document.getElementById("ctl00_SearchString1_QueryTextBox");
    if (searchTextBox == null) searchTextBox = document.aspnetForm.ctl00_QueryTextBox;
    if (searchTextBox != null) searchText = escape(searchTextBox.value).replace(";", "");
    if (searchText != "") newUrl += (searchPerfix + searchText);
    else if (searchPerfix == zeroSearchPrefix) newUrl += "Zero/О_Zero";
    hyperLink.href = newUrl;
    return 1;
}

*/
