jQuery.noConflict();

jQuery(document).ready(function($) {

    $('img.rounded').one('load',function () {
        var img = $(this);
        var img_width = img.width();
        var img_height = img.height();
 
        // build wrapper
        var wrapper = $('<div class="rounded_wrapper"></div>');
        wrapper.width(img_width);
        wrapper.height(img_height);
 
        // move CSS properties from img to wrapper
        wrapper.css('float', img.css('float'));
        img.css('float', 'none')
 
        wrapper.css('margin-right', img.css('margin-right'));
        img.css('margin-right', '0')
 
        wrapper.css('margin-left', img.css('margin-left'));
        img.css('margin-left', '0')
 
        wrapper.css('margin-bottom', img.css('margin-bottom'));
        img.css('margin-bottom', '0')
 
        wrapper.css('margin-top', img.css('margin-top'));
        img.css('margin-top', '0')
 
        wrapper.css('display', 'block');
        img.css('display', 'block')
 
        // IE6 fix (when image height or width is odd)
        if ($.browser.msie && $.browser.version == '6.0')
        {
            if(img_width % 2 != 0)
            {
                wrapper.addClass('ie6_width')
            }
            if(img_height % 2 != 0)
            {
                wrapper.addClass('ie6_height')			
            }
        }
 
        // wrap image
        img.wrap(wrapper);
 
        // add rounded corners
        img.after('<div class="tl"></div>');
        img.after('<div class="tr"></div>');
        img.after('<div class="bl"></div>');
        img.after('<div class="br"></div>');
    }).each(function(){
        if(this.complete) $(this).trigger("load");
    });

    $('#searchinp').focus(function() {
        var value = $('#searchinp').val();
        if (value == 'Поиск по сайту') {
            $('#searchinp').val('');
        }
    });
    $('#searchinp').blur(function() {
        var value = $('#searchinp').val();
        if (value == '') {
            $('#searchinp').val('Поиск по сайту');
        }
    });

    /*$("#slide-down").click(function() {
        $.ajax({
            type: 'GET',
            url: '/ajax.php',
            data: ({module: 'banners', action: 'moveup', lastID: lastID}),
            dataType: 'script',
            async: false,
            success: function (data) {
                jQuery('#slideshow').animate({ top: "-=172px" }, "slow");
            }
        });
        //jQuery('#slideshow').append('<li class="text"><a href="/news/kurzweil_soundlibrary.html">Kurzweil представляет бесплатный каталог сэмплов для серии PC3K</a></li><li class="image"><a href="/news/kurzweil_soundlibrary.html"><img src="/upload/iblock/1b2/310_862.jpg" /></a></li>');
    });
    $("#slide-up").click(function() {
        if (parseInt($('#slideshow').css('top')) < 0) {
            $('#slideshow').animate({ top: "+=172px" }, "slow");
        }
    });*/
});

function sort(field) {
	if (getCookie('sortBy') == field && getCookie('sortAt') == "1"){
		setCookie('sortAt', '2');
	} else {
		setCookie('sortAt', '1');
	}
	setCookie('sortBy', field);
	//document.location = document.location;
    window.location.reload();
	return false;
}

function itemsPerPage(field) {
    setCookie('itemsPerPage', field);
    //document.location = document.location;
    window.location.reload();
    return false;
}

// Change view layout
function cView(field) {
    setCookie('catalogView', field);
    //document.location = document.location;
    window.location.reload();
    return false;
}

function setCookie(name, value) { 
      var valueEscaped = escape(value); 
      var expiresDate = new Date(); 
      expiresDate.setTime(expiresDate.getTime() + 24 * 60 * 60 * 1000);
      var expires = expiresDate.toGMTString(); 
      var newCookie = name + "=" + valueEscaped + "; path=/; expires=" + expires; 
      if (valueEscaped.length <= 4000) document.cookie = newCookie + ";"; 
} 
function getCookie(name) { 
      var prefix = name + "="; 
      var cookieStartIndex = document.cookie.indexOf(prefix); 
      if (cookieStartIndex == -1) return null; 
      var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length); 
      if (cookieEndIndex == -1) cookieEndIndex = document.cookie.length; 
      return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex)); 
}

function ShowSection(index) {
    for (var i = 1; i <= 6; i++) {
        document.getElementById("view_" + i).style.display = "none";
        if (document.getElementById("nav_" + i) != null)
            document.getElementById("nav_" + i).style.fontWeight = "normal";
    }
    document.getElementById("view_" + index).style.display = "block";
    document.getElementById("nav_" + index).style.fontWeight = "bold";
}

function setPrice() {
	var priceF = document.getElementById("priceF").value;
	var priceT = document.getElementById("priceT").value;
	setCookie("priceF", priceF);
	setCookie("priceT", priceT);
	//document.location = document.location;
    window.location.reload();
	return false;
}
function clrPrice() {
	setCookie("priceF", 0);
	setCookie("priceT", 0);
	//document.location = document.location;
    window.location.reload();
	return false;
}
function setBrandFilter(catId) {
    var brandObj = document.getElementById('brands_filter');
    var brandId = brandObj.options[brandObj.selectedIndex].value;
    setCookie("brandsFilterCat", catId);
    setCookie("brandsFilterCode", brandId);
    //document.location = document.location;
    window.location.reload();
}

/**
 * Search functions
 */

function loadSecLevelSections(fLevelObj) {
    showContent("/netcat/modules/catalog/ajax.php?action=GS&fLevelSec=" + fLevelObj.value, "sLevelTd");
}

function showContent(link, reloadId) {
    var cont = document.getElementById(reloadId);
    var http = createRequestObject();
    if( http ) {
        http.open('get', link);
        http.onreadystatechange = function () {
            if(http.readyState == 4) {
                cont.innerHTML = http.responseText;
            }
        }
        http.send(null);
    }
}

function createRequestObject() {
    try {return new XMLHttpRequest()}
    catch(e) {
        try {return new ActiveXObject('Msxml2.XMLHTTP')}
        catch(e) {
            try {return new ActiveXObject('Microsoft.XMLHTTP')}
            catch(e) {return null;}
        }
    }
}

function shops_modal(id, title) {
    // Create div for modal window
    var dialogdiv = document.createElement('div');
    dialogdiv.id = 'shops-modal';
    dialogdiv.title = 'Купить <span class="modal-shop-good-name">' + title + '</span> можно здесь:';
    //var shops_content = loadContent("/netcat/modules/catalog/ajax.php?action=SHOPS&CNT="+id);
    shops_content = jQuery.ajax({
        url: "/netcat/modules/catalog/ajax.php",
        global: false,
        type: "GET",
        data: ({action: "SHOPS", CNT: id}),
        dataType: "html",
        async:false
    }).responseText;
    dialogdiv.innerHTML = shops_content;
    
    jQuery(dialogdiv).dialog({
        autoOpen: false,
        height: 500,
        width: 534,
        resizable: false,
        modal: true
    });

    jQuery(dialogdiv).dialog('open');
    return false;
}
