﻿$(document).ready(function () {
    $('.searchfield').each(function () {
        var inp = $(this);
        inp.focus(function () {

            if (inp.attr('value') == 'искать') inp.attr('value', '');
        });
        inp.blur(function () {

            if (inp.attr('value') == '') inp.attr('value', 'искать');
        });
    });

    initTabs();
});

function fixPNGElements(element, sizingMethod) {
    //alert('in pngFix');
    //Если браузер IE версии 5.5-6
    if (/MSIE (5\.5|6).+Win/.test(navigator.userAgent)) {
        var src;

        if (element.tagName == 'IMG') //Если текущий элемент картинка (тэг IMG)
        {
            if (/\.png$/.test(element.src)) //Если файл картинки имеет расширение PNG
            {
                src = element.src;
                element.src = "images/1.gif"; //заменяем изображение прозрачным gif-ом
            }
        }
        else //иначе, если это не картинка а другой элемент
        {
            //если у элемента задана фоновая картинка, то присваеваем значение свойства background-шmage переменной src
            src = element.currentStyle.backgroundImage.match(/url\("(.+\.png)"\)/i);
            if (src) {
                src = src[1]; //берем из значения свойства background-шmage только адрес картинки
                element.runtimeStyle.backgroundImage = "none"; //убираем фоновое изображение
            }
        }
        if (!sizingMethod)
            sizingMethod = 'image';
        //если, src не пуст, то нужно загрузить изображение с помощью фильтра AlphaImageLoader
        if (src) {
         //   var filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='" + sizingMethod + "')";
         //   alert(filter);
            element.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='" + sizingMethod + "')";
        }
    }
}

function initTabs() {
    var sets = document.getElementsByTagName("ul");
    for (var i = 0; i < sets.length; i++) {
        if (sets[i].className.indexOf("tabset") != -1) {
            var tabs = [];
            var links = sets[i].getElementsByTagName("a");
            for (var j = 0; j < links.length; j++) {
                if (links[j].className.indexOf("tab") != -1) {
                    tabs.push(links[j]);
                    links[j].tabs = tabs;
                    var c = document.getElementById(links[j].name.substr(links[j].name.indexOf("_") + 1));

                    if (c) if (links[j].className.indexOf("active") != -1) c.style.display = "block";
                    else c.style.display = "none";

                    links[j].onclick = function() {
                        var c = document.getElementById(this.name.substr(this.name.indexOf("_") + 1));
                        if (c) {
                            for (var i = 0; i < this.tabs.length; i++) {
                                var tab = document.getElementById(this.tabs[i].name.substr(this.tabs[i].name.indexOf("_") + 1));
                                if (tab) {
                                    tab.style.display = "none";
                                }
                                this.tabs[i].className = this.tabs[i].className.replace("active", "");
                            }
                            this.className += " active";
                            c.style.display = "block";
                            return false;
                        }
                    }
                }
            }
        }
    }
}

function Set_Cookie(name, value, expires, path, domain, secure) {
    // set time, it's in milliseconds
    var today = new Date();
    today.setTime(today.getTime());

    /*
    if the expires variable is set, make the correct
    expires time, the current script below will set
    it for x number of days, to make it for hours,
    delete * 24, for minutes, delete * 60 * 24
    */
    if (expires) {
        expires = expires * 1000 * 60 * 60 * 24;
    }
    var expires_date = new Date(today.getTime() + (expires));

    document.cookie = name + "=" + escape(value) +
((expires) ? ";expires=" + expires_date.toGMTString() : "") +
((path) ? ";path=" + path : "") +
((domain) ? ";domain=" + domain : "") +
((secure) ? ";secure" : "");
}

function Get_Cookie(check_name) {
    // first we'll split this cookie up into name/value pairs
    // note: document.cookie only returns name=value, not the other components
    var a_all_cookies = document.cookie.split(';');
    var a_temp_cookie = '';
    var cookie_name = '';
    var cookie_value = '';
    var b_cookie_found = false; // set boolean t/f default f

    for (i = 0; i < a_all_cookies.length; i++) {
        // now we'll split apart each name=value pair
        a_temp_cookie = a_all_cookies[i].split('=');


        // and trim left/right whitespace while we're at it
        cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

        // if the extracted name matches passed check_name
        if (cookie_name == check_name) {
            b_cookie_found = true;
            // we need to handle case where cookie has no value but exists (no = sign, that is):
            if (a_temp_cookie.length > 1) {
                cookie_value = unescape(a_temp_cookie[1].replace(/^\s+|\s+$/g, ''));
            }
            // note that in cases where cookie is initialized but no value, null is returned
            return cookie_value;
            break;
        }
        a_temp_cookie = null;
        cookie_name = '';
    }
    if (!b_cookie_found) {
        return null;
    }
}

function GetSavedVolume() {
    var savedVolume = Get_Cookie("selected_volume");
    savedVolume = savedVolume || 50;
    return parseInt(savedVolume, 10);
}
