
function toggle_hint() {
    
    $("#hint").toggle();
    
    if ($("#show_hint").text()=='скрыть') {
        $("#show_hint").text('показать');
        $.cookie("fw_show_hints", "none", { expires: 365 });
    }
    else {
        $("#show_hint").text('скрыть');
        $.cookie("fw_show_hints", "block", { expires: 365 });
    }
    
    return false;
}

function toggle_dash(id) {
    
    $("#"+id).toggle();
    if ($("#"+id+"_title").text()=='свернуть') {
        $("#"+id+"_title").text('развернуть');
    }
    else {
       $("#"+id+"_title").text('свернуть'); 
    }
    
    $.post("/ajax/set_dash.php",{id: id},function(xml){});
    
    return false;
}

function vote(module, id, vote) {
    
    if (module == 'recipes') {
        var r = Math.round($("#rrating_2_"+id).html()); 
        $("#rrating_2_"+id).html("<img src=templates/img/loading1.gif>");
    }
    if (module == 'blog') {
        var r = Math.round($("#brating_2_"+id).html()); 
        $("#brating_2_"+id).html("<img src=templates/img/loading1.gif>");
    }
    if (module == 'advices') {
        var r = Math.round($("#arating_2_"+id).html()); 
        $("#arating_2_"+id).html("<img src=templates/img/loading1.gif>");
    }
    if (module == 'comments') {
        var r = Math.round($("#crating_"+id).html()); 
        $("#crating_"+id).html("<img src=templates/img/loading1.gif>");
    }
 
    $.post("/ajax/votes.php",{module: module, id: id, vote: vote},
    function(xml)
    {
        
        if (xml=='low_bullets') {
            
            $.jGrowl("Вы исчерпали лимит голосов за день. Подробнее о рейтингах читайте в разделе \"Помощь\".", { 
                theme: 'smoke',
                header: 'Для информации',
                life: 4000
            });
            
            if (module == 'recipes') { $("#rrating_2_"+id).html("-");}
            if (module == 'blog') { $("#brating_2_"+id).html("-");}
            if (module == 'advices') { $("#arating_2_"+id).html("-");}
            if (module == 'comments') {$("#crating_"+id).html("-");}
        }
        else {    
        
            if (xml=='ok') {
            
                if (vote == 'good') rating = r+1;
                if (vote == 'bad') rating = r-1;
                
                
                if (module == 'recipes' || module == 'blog' || module == 'advices') {
                    
                    $('#rvotes_count').html($('#rvotes_count').html()-1);
                }
                
                if (module == 'recipes') {
                
                    $("#rrating_2_"+id).html(rating);
                    $("#rrating_1_"+id).html("<img src=/templates/img/1.gif>");
                
                    if (rating<0) $("#rrating_3_"+id).attr('class','rating_cell_red');
                    if (rating>0) $("#rrating_3_"+id).attr('class','rating_cell_green');
                    if (rating==0) $("#rrating_3_"+id).attr('class','rating_cell_std');
                }
            
                if (module == 'blog') {
                
                    $("#brating_2_"+id).html(rating);
                    $("#brating_1_"+id).html("<img src=/templates/img/1.gif>");
                
                    if (rating<0) $("#brating_3_"+id).attr('class','rating_cell_red');
                    if (rating>0) $("#brating_3_"+id).attr('class','rating_cell_green');
                    if (rating==0) $("#brating_3_"+id).attr('class','rating_cell_std');
                }
                
                if (module == 'advices') {
                
                    $("#arating_2_"+id).html(rating);
                    $("#arating_1_"+id).html("<img src=/templates/img/1.gif>");
                
                    if (rating<0) {
                        $("#arating_3_"+id).attr('class','adv_rating_cell_red');
                        $("#adv_rt_"+id).css('top','14');
                        $("#arating_2_"+id).css('padding-top','4px');
                    }
                    if (rating>0) {
                        $("#arating_3_"+id).attr('class','adv_rating_cell_green');
                        $("#adv_rt_"+id).css('top','3');
                        $("#arating_2_"+id).css('padding-top','15px');
                    }
                    if (rating==0) {
                        $("#arating_3_"+id).attr('class','adv_rating_cell_std');
                        $("#adv_rt_"+id).css('top','3');
                        $("#arating_2_"+id).css('padding-top','15px');
                    }
                }
            
                if (module == 'comments') {
                
                    $("#crating_"+id).html(rating);
                
                    if (rating<0) $("#cr_back_"+id).attr('class','cvote_red_middle');
                    if (rating>0) $("#cr_back_"+id).attr('class','cvote_green_middle');
                    if (rating==0) $("#cr_back_"+id).attr('class','cvote_std_middle');
                
                    if (vote == 'good') {
                        $("#cimr_"+id).html('<img src=/templates/img/cvote_green3.gif border=0>');
                        $("#ciml_"+id).html('<img src=/templates/img/cvote_std1.gif border=0>');
                    }
                    if (vote == 'bad') {
                        $("#ciml_"+id).html('<img src=/templates/img/cvote_red1.gif border=0>');
                        $("#cimr_"+id).html('<img src=/templates/img/cvote_std3.gif border=0>');
                    }
                    
                    $('#cvotes_count').html($('#cvotes_count').html()-1);
                }
            }
            else alert("Произошла ошибка");
        }
    });
    
    return false;
}

function makeSiteLink(name) {
	if (!window.js_base_url) {
		str = document.location.toString();
		js_base_url = str.replace("/javascript/editor/scripts/hyperlink.htm","");
		js_base_url = js_base_url.replace("/javascript/editor/scripts/moz/hyperlink.htm","");
	}
	popup(js_base_url+'/admin/index.php?mod=tree&action=mini_browser&returnname='+name,'600','400');
}

function retBack(obj, str) {
	window.close();
	obj.value = str;
}

function insertIntoWYSIWYG(str) {
	doc = "window.opener";
	for ( property in eval(doc) ) {
		fullPropertyName = "document."+property;

		if (fullPropertyName.indexOf("document.myStyle")!=-1) {
			objName = fullPropertyName.replace("document.myStyle", "");
			break;
		}
	}
	objName = eval(doc+".window."+objName);
	objName.insertCustomTag(str);
}



function switch_loading_box (box_id) {
	
	if(document.getElementById(box_id).style.display!="none") {
		document.getElementById(box_id).style.display="none";
	}
	else {
		document.getElementById(box_id).style.display="";
		
		var ie=document.all;
		iebody=document.body;
		var scroll_top=(ie)? iebody.scrollTop : window.pageYOffset;
		var docwidth=(ie)? iebody.clientWidth : window.innerWidth;
		docheight=(ie)? iebody.clientHeight: window.innerHeight;
		var objwidth=document.getElementById(box_id).offsetWidth;
		objheight=document.getElementById(box_id).offsetHeight;
		var test=docwidth/2-objwidth/2+"px";

		document.getElementById(box_id).style.left=docwidth/2-objwidth/2+"px";
		document.getElementById(box_id).style.top=scroll_top+docheight/2-objheight/2+"px";
	}
	
	return false;
	
}

function add_ingredient() {
    
    if ($("#ingredients_count").attr("value")<1000) {
        var count = $("#ingredients_count").attr("value")-0+1;
		var count2 = $("#ingredients_count").attr("value")-0+2;
        $("#ingredients_count").attr("value",count);
		
        var new_field = "<div id=ing_div_"+count+"><span class=ci>"+count2+"</span>. <input type=text name=ingredients["+count+"] style=\"width:110px;\" id=ing_"+count+"> кол-во: <input type=text name=quantity["+count+"] style=\"width:50px;\"> <select name=units["+count+"]><option></option><option>г.</option><option>шт.</option><option>кг.</option><option>ст.</option><option>л.</option><option>мл.</option><option>ст.л</option><option>ч.л</option></select> [<a href=# onclick=\"return delete_ingredient('"+count+"');\">x</a>]</div>";
        
        $("#ingredients_box").append(new_field);
        $("#ing_"+count).autocomplete('/ajax/kitchen.ing_autocomplete.php', {
            width: 200,
            matchContains: true
        });
        
    }

    return false;
}


function add_ingredient_big() {
    
    if ($("#ingredients_count").attr("value")<1000) {
        var count = $("#ingredients_count").attr("value")-0+1;
		var count2 = $("#ingredients_count").attr("value")-0+2;
        $("#ingredients_count").attr("value",count);
		

        var new_field = "<table cellspacing=0 cellpadding=0 id=ing_div_"+count+" class=list_ing_table width=100%>\
			<tr>\
				<td width=30 nowrap>\
					<span class=ci style=\"color: #bfbeb7; font-size: 14pt;\">"+count2+"\.</span>\
				</td>\
				<td nowrap>\
					<div class=big_gray_input1><img src=templates/img/1.gif></div>\
					<div class=big_gray_input2 style=\"width: 90%;\"><input type=text name=ingredients["+count+"] id=ing_"+count+" class=big_gray_input style=\"width:100%;\"></div>\
					<div class=big_gray_input3><img src=templates/img/1.gif></div>\
				</td>\
				<td width=55>\
					<span style=\"color:#998c8c; font-size: 12pt;\">кол-во</span>\
				</td>\
				<td width=70 nowrap>\
					<div class=big_gray_input1><img src=templates/img/1.gif></div>\
					<div class=big_gray_input2><input type=text name=quantity["+count+"] class=big_gray_input style=\"width:60px;\"></div>\
					<div class=big_gray_input3><img src=templates/img/1.gif></div>\
				</td>\
\
				<td width=80>\
					<div id=num_select_block_"+count+" class=num_select_block>\
					<select name=units["+count+"] id=num_select_"+count+" class=num_select_s>\
                    <option></option>\
                    <option value=\"0\">г.</option>\
                    <option value=\"1\">шт.</option>\
                    <option value=\"2\">кг.</option>\
                    <option value=\"3\">ст.</option>\
                    <option value=\"4\">мл.</option>\
                    <option value=\"5\">л.</option>\
		            <option value=\"6\">ст.л.</option>\
				    <option value=\"7\">ч.л.</option>\
	            	</select></div>\
				</td>\
				<td align=right width=60>\
					<a href=# onclick=\"return delete_ingredient('"+count+"');\" class=service_link>удалить</a>\
				</td>\
			</tr>\
		</table>";
        
        
        $("#ingredients_box").append(new_field);
        $("#ing_"+count).autocomplete('/ajax/kitchen.ing_autocomplete.php', {
            width: 200,
            matchContains: true
        });
        $('#num_select_block_'+count).jelectbox({selectbox_id:'num_select_'+count, width:'70px', height:'auto'}); 
        
    }

    return false;
}


function delete_ingredient(id) {
    
	var c=1;

    if ($("#ingredients_count").attr("value")>1) {
	
        var count = $("#ingredients_count").attr("value")-1;
		
        $("#ingredients_count").attr("value",count);
		
        $("#ing_div_"+id).remove();

		for (var i = 0; i < count+1; i++) {

			$(".ci:eq("+i+")").html(""+c+".");
			$(".num_select_block:eq("+i+")").attr('id',"num_select_block_"+c-1);
			$(".num_select_s:eq("+i+")").attr('id',"num_select_"+c-1);
			c++;
		}
    }
    
    return false;
}


function show_div(name) {
	
	if(document.getElementById(name).style.display!="none") {
		document.getElementById(name).style.display="none";
	}
	else {
		document.getElementById(name).style.display="";
	}
	
	return false;
}


function show_reg(obj, redirect, event) { 
	
	document.getElementById('login_redirect').value = redirect;
	
	var div_width=500;
	var offsetfrommouse=[15,25];
	
    docsize = getPageSize();
    
    var docwidth = docsize[0];
    var docheight =docsize[1];

	var x = y = 0;
	
	if (document.attachEvent != null) { // Internet Explorer & Opera 
	   x = window.event.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft); 
	   y = window.event.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop); 
	} else if (!document.attachEvent && document.addEventListener) { // Gecko 
	   x = event.clientX + window.scrollX; 
	   y = event.clientY + window.scrollY; 
	} else { 
	   // Do nothing 
	}
	
	var check = div_width+x;
	
	object = document.getElementById(obj); 
	
	if (check>=docwidth) {
		x = x - div_width;
	}

	object.style.left = x+"px"; 
	object.style.top = y+"px"; 
	object.style.display = 'block';
	
	return false;
} 

function hide_reg(obj, time) { 
 object = document.getElementById(obj); 
 
 if (time) {
    myTimer = setTimeout('object.style.display = \'none\'',time);
 }
 else {
    object.style.display = 'none';
 } 
 
 return false;
}

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}



//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}
