function supermag_init () {
	all_external_links_target_blank();
	
	// minimalizm
	var min = getCookie('minimalizm')
	if(min && min != '0') {
		document.getElementById('minimalizm_box').checked = true
		minimalizm(true);	
	}
}

function set_search_mode(what) {
	// turm previous button off
	if(!this.mode) this.mode = document.getElementById('search_mode').value;
	document.getElementById(this.mode).className = document.getElementById(this.mode).className.replace('on','');

	this.mode = what.id;

	// button switch
	what.className += ' on';
	
	// set mode hidden input
	document.getElementById('search_mode').value = this.mode;

	// detail mode
	if(this.mode == 'search_mode_detail') {
		document.getElementById('search_details').className = '';
		document.getElementById('search_no_details').className = 'hidden';
		document.getElementById('search_text_lt').name = '';
		document.getElementById('search_text_xt').name = 'search_text';
	} else {
		document.getElementById('search_details').className = 'hidden';
		document.getElementById('search_no_details').className = '';
		document.getElementById('search_text_lt').name = 'search_text';
		document.getElementById('search_text_xt').name = '';
	}
	
	if(this.mode == 'search_mode_catalogue') {
		document.getElementById('search_in_descriptions').className = '';
	} else {
		document.getElementById('search_in_descriptions').className = 'hidden';
	}
}

function sh(id) {
	var el = document.getElementById(id);

	if(el.className.search('hidden') == -1) {
		el.className += ' hidden';
	} else {
		el.className = el.className.replace('hidden','');
	}
}

function set_currency(currency) {
	document.cookie = 'currency=' + currency;
	document.location.reload();
}

function popup(src) {
	document.getElementById('popup').className = 'popup';
	document.getElementById('popup_image').src = src;
}

function closepopup() {
	document.getElementById('popup').className = 'hidden';
}

var open_submenu;
function cat_over(id) {
	id = 'cat_submenu_' + id;
	if(document.getElementById(id).className == 'hidden') {
		open_submenu = setTimeout('sh("'+id+'")', 500);
	}
}

function cat_out() {
	clearTimeout(open_submenu);
}

function minimalizm(sw) {
	if(sw) {
		document.body.className = 'ug_ne_proidet';
		setCookie('minimalizm',1);
	} else {
		document.body.className = '';
		setCookie('minimalizm',0);
	}
}

// external links
function all_external_links_target_blank() {
	locc1 = 'http://' + document.domain + '/';
	locc2 = '/out/';
	for (i in document.links) {
		if(document.links[i].href) if(
			document.links[i].href.search(locc2) >= 0 
			|| document.links[i].href.search(locc1) != 0
			&& document.links[i].href.search('http://') == 0 
		) {
			document.links[i].target = "_blank";
		}
	}
}


// cookie
function setCookie(name, value, expires, path, domain, secure) {
        var curCookie = name + "=" + escape(value) +
                ((expires) ? "; expires=" + expires.toGMTString() : "") +
                ((path) ? "; path=" + path : "") +
                ((domain) ? "; domain=" + domain : "") +
                ((secure) ? "; secure" : "")
                document.cookie = curCookie
}

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))
}

// form
function submit_do(form, value) {
	form.action += '&do=' + value;
	form.submit();
}

// AJAX
Function.prototype.bind = function(obj, args_pre)  {
	var fn = this;
	if(args_pre)return function() {return fn.apply(obj, args_pre)}
	else		return function() {return fn.apply(obj, arguments)}
}

function createRequestObject()
{
    if (window.XMLHttpRequest) {
        try {
            return new XMLHttpRequest();
        } catch (e){}
    } else if (window.ActiveXObject) {
        try {
            return new ActiveXObject('Msxml2.XMLHTTP');
        } catch (e){
          try {
              return new ActiveXObject('Microsoft.XMLHTTP');
          } catch (e){}
        }
    }
    return null;
}

var search_assist_timeout = null;
var search_assist_old_text = null;
function search_assist() {
	clearTimeout(search_assist_timeout);

	search_text = document.getElementById('search_text_lt').value;
	search_text = search_text.replace(/^\s+|\s+$/, '');
	if(search_text && search_text.length < 2) {
		search_assist_close();
		return;
	}

	if(search_text == search_assist_old_text) {
		return;
	}
	
	search_assist_timeout = setTimeout('search_assist_go()', 400);
	search_assist_old_text = search_text;
}

function search_assist_go() {
	search_text = document.getElementById('search_text_lt').value;
	if(search_text.length < 3) return;

   	document.getElementById('search_assist_popup').className = '';
	document.getElementById('search_assist_popup').innerHTML = '<p><b>Загружаю подсказку...</b></p>';
	XMLHttp = createRequestObject();
	
	XMLHttp.onreadystatechange = search_assist_draw.bind(XMLHttp);
	url = "/search_assist/?search_text=" + encodeURIComponent(search_text);
	XMLHttp.open("GET", url, true);
	XMLHttp.send("search_text=" . search_text);
}

function search_assist_draw() {
//	for(i in this) document.write(i + '::' + this[i] + '<br>');
	try {
	    if (this.readyState == 4 && this.status == 200) {
	    	document.getElementById('search_assist_popup').className = '';
			document.getElementById('search_assist_popup').innerHTML = this.responseText;
	    }
	} catch(e) {}
}

function search_assist_close() {
	document.getElementById('search_assist_popup').className = 'hidden';
	document.getElementById('search_assist_popup').innerHTML = '';
}
