function addBasket(id, o, col) {
    if( !col ) col='green';
    var cont = jQuery('#basket');
    cont.load('/cat/add_basket_ajax/'+id, function() {
	jQuery(o).css('background-color', col);
    });
}

function showHelp(ob) {
    jQuery('#popupWindow #popupWindow_header').html('');
    jQuery('#popupWindow #popupWindow_content').html(
	'<div id="popupHelp"><center><img src="/img/loading.gif" alt="" /></center></div>' + 
	'<center><input type="button" value=" Закрыть " onclick="disablePopup();" /></center>'
    );
    centerPopup();
    loadPopup();
    jQuery('#popupHelp').load('/ajax/help/'+jQuery(ob).attr('helpTopic'));
}
jQuery(function() {
    jQuery('.kan4a input').val('tRu3');
    jQuery('[helpTopic]').click(function(){ showHelp(this); });
});

function createCookie(name,value,days) {
    if (days) {
	var date = new Date();
	date.setTime(date.getTime()+(days*24*60*60*1000));
	var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
	var c = ca[i];
	while (c.charAt(0)==' ') c = c.substring(1,c.length);
	if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

// Give function named n in object o
function resolveFunc(n, o) {
    var i, l;
    o = o || window;
    n = n.split('.');
    for (i=0, l = n.length; i<l; i++) {
	o = o[n[i]];
	if (!o)
    	    break;
    }
    return o;
}

function restrict_file_input(ob, regex, fmts) {
    if( ! ob.value.match(regex) ) {
	ob.value='';
	alert('Предлагаемый файл не подходит по формату, выберите пожалуйста другой.'+
	    (fmts ? "\nПринимаются файлы в форматах "+fmts : ''));
    }
}

/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;

//loading popup with jQuery magic!
function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		jQuery("#popupBackground").css({
			"opacity": "0.7"
		});
		jQuery("#popupBackground").fadeIn("fast");
		jQuery("#popupWindow").fadeIn("fast");
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		jQuery("#popupBackground").fadeOut("fast");
		jQuery("#popupWindow").fadeOut("fast");
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = jQuery("#popupWindow").height();
	var popupWidth = jQuery("#popupWindow").width();
	//centering
	jQuery("#popupWindow").css({
		"position": "absolute",
		"top": jQuery(window).scrollTop() + windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	jQuery("#popupBackground").css({
		"height": windowHeight
	});
	
}

function showPopup(h, ar) {
    jQuery('#popupWindow #popupWindow_header').html(h);
    var removearr = new Array('ids', 'url', 'names', 'only_leaf', 'only_one', 'only_1st');
    for(var i=0; i<removearr.length; i++) {
        jQuery('#popupWindow #popupWindow_content').removeAttr(removearr[i]);
    }
    if( ar['ids'] && ar['url'] ) {
	jQuery('#popupWindow #popupWindow_content').html(
	    '<div id="pwc_d"></div><center><input type="button" value=" Готово " onclick="done_tree();" /></center>'
	).attr(ar);
	load_tree(jQuery('#pwc_d'), '');
    }
    centerPopup();
    loadPopup();
}
function load_tree(jOb, tree_id) {
    jOb.html('<center><img src="/img/loading.gif" alt="" /></center>');
    jOb.load(jQuery('#popupWindow #popupWindow_content').attr('url') + tree_id, function() {
	if( jQuery('#popupWindow #popupWindow_content').attr('only_leaf') ) {
	    jQuery('#popupWindow #popupWindow_content .pwc_line[childs!=0] input').hide();
	}
	if( jQuery('#popupWindow #popupWindow_content').attr('only_one') ) {
	    jQuery('#popupWindow #popupWindow_content .pwc_line input').hide();
	    if( jQuery('#popupWindow #popupWindow_content').attr('only_leaf') ) {
		jQuery('#popupWindow #popupWindow_content .pwc_line[childs=0] span.pwc_text').wrap('<a href="#" onclick="one_select_tree(this); return false;"></a>');
	    }else{
		jQuery('#popupWindow #popupWindow_content .pwc_line span.pwc_text').wrap('<a href="#" onclick="one_select_tree(this); return false;"></a>');
	    }
	}
	if( jQuery('#popupWindow #popupWindow_content').attr('only_1st') ) {
	    jQuery('#popupWindow #popupWindow_content .pwc_line .pwc_pm').hide();
	}
	try { //hello ie6
	    var c = jQuery(this).prev().find('input').attr('checked');
	    jQuery(this).find('input[type=checkbox]').attr('checked', c).attr('disabled', c);
	}catch(e) {
	}
    });
}
function toggle_tree(ob) {
    var jOb1 = jQuery(ob);
    var treeid = jOb1.parent().attr('cid');
    var jOb2 = jQuery('#pwc_d'+treeid);
    jOb1.toggleClass('tree_plus').toggleClass('tree_minus');
    jOb2.toggle();
    if( jOb2.html().length < 1 ) {
	load_tree(jOb2, treeid);
    }
}
function change_tree(ob) {
    var jOb = jQuery(ob);
    var c = jOb.attr('checked');
    jQuery('#pwc_d' + jOb.parent().attr('cid')).find('input[type=checkbox]').attr('checked', c).attr('disabled', c);
}
function one_select_tree(ob) {
    jQuery(ob).parent().find('input[type=checkbox]').attr('checked', 'checked');
    done_tree();
}
function done_tree() {
    var ids = jQuery('#popupWindow #popupWindow_content').attr('ids');
    var fn = jQuery('#popupWindow #popupWindow_content').attr('select_handler');
    if( ids ) {
	jQuery('#'+ids).val(
	    jQuery.map(jQuery('#popupWindow #popupWindow_content .pwc_line input:checked:enabled').get(), function(a) {
		return a.value;
	    }).join(',')
	);
    }
    var ns = jQuery('#popupWindow #popupWindow_content').attr('names');
    if( ns ) {
	jQuery('#'+ns).html(
	    jQuery.map(jQuery('#popupWindow #popupWindow_content .pwc_line input:checked:enabled').parent().find('.pwc_text').get(), function(a) {
		return a.innerHTML;
	    }).join('; ')
	);
    }
    disablePopup();
    if( fn ) {
	resolveFunc(fn).call(null, []);
    }
}


////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//CONTROLLING EVENTS IN jQuery
jQuery(document).ready(function(){
	//CLOSING POPUP
	//Click the x event!
	jQuery("#popupWindow #popupWindow_closebutton").click(function(){
		disablePopup();
	});
	//Click out event!
	jQuery("#popupBackground").click(function(){
		disablePopup();
	});
	//Press Escape event!
	jQuery(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});

});

