//function resetSearch(id) {
//	if (!id) id = 'search_form';
//	$('#'+id).prepend('<input type="hidden" name="search[reset]" value="1"/>');
//	search(id);
//}
//
//function search(id){
//	if (!id) id = 'search_form';
//	$('#'+id).submit();
//}

function ajax(url, method, params, before, success, error) {
	options = {
		url:url,
		method:method
	}
	if (params != undefined && params != '') options['data'] = params
	if (before != undefined && before != '') options['beforeSend'] = before;
	if (success != undefined && success != '') options['success'] = success;
	if (error != undefined && error != '') options['error'] = error;
	$.ajax(options)
}

function setupCalendar(input_id, btn_id) {
	Zapatec.Calendar.setup({
		inputField     :    input_id,
		ifFormat       :    "%m/%d/%Y",
		showsTime      :     false,
		button         :    btn_id,
		align          :    "B2"
	});
}
function startWait(msg){
	if (msg == undefined) msg = 'Loading...';
	notifier.display(msg)
}

function endWait(){
	notifier.hide()
}

function win_popup(url){
	mywindow = window.open (url,"mywindow","menubar=1,resizable=1,location=1,status=1,scrollbars=1");
	mywindow.moveTo(0,0);
}

var popupOpened = false;
var response;

function ajaxpopup(text, width, height){
	if ($("div.popup").length > 0) {
		popupclean();
	}
	else {
		hideUnderPopup();
	}
	
	if (width == undefined){
		width = 600;
	}
	var templatesRoot = 'templates/prototype/';
	
	$("body").append("<div style='width:"+width+"' id='popup'  class=\"popup\">"+
	"<table cellpadding='0' cellspacing='0' border='0' width='100%'>"+
	"<tr>"+
		"<td colspan='3'>"+
			"<table width='100%' border='0' cellpadding='0' cellspacing='0'><tr>"+
				"<td class='top-left-corner'><img src='"+templatesRoot+"img/spacer.gif' width='34' height='10' alt='' /></td>"+
				"<td class='top-bg' width='100%'><img src='"+templatesRoot+"img/spacer.gif' width='100%' height='10'  alt='' /></td>"+
				"<td class='top-right-corner'><img src='"+templatesRoot+"img/spacer.gif' width='34' height='10' alt='' /></td>"+
		"</tr></table>"+	
		"</td>"+
	"</tr>"+
	"<tr valign='top'>"+
		"<td class='left-cols'>"+
			"<table cellpadding='0' cellspacing='0'>"+
				"<tr><td class='col-top left1'><img src='"+templatesRoot+"img/spacer.gif' width='14' height='25' alt='' /></td></tr>"+
				"<tr><td class='left-shadow'><img src='"+templatesRoot+"img/spacer.gif' alt='' /></td></tr>"+
				"<tr><td class='col-bot left2'><img src='"+templatesRoot+"img/spacer.gif' width='14' height='21' alt='' /></td></tr>"+
			"</table>"+
		"</td>"+
		"<td bgcolor='white'>"+
			"<table cellpadding='0' cellspacing='0' width='100%'>"+				
				"<tr><td><div class='popup_close' align='right'><a href='javascript://' onclick='popupclose(); return false;' title='Close Popup Window'>"+
					"<img src='"+templatesRoot+"img/icons/close.gif' alt='Close Popup Window' class='vm mr4 mt4'  /></a></div>"+
				"</td></tr>"+
				"<tr><td id='popupcontent'>"+text+"</td></tr>"+
			"</table>"+
		"</td>"+
		"<td class='right-cols'>"+
			"<table cellpadding='0' cellspacing='0'>"+
				"<tr><td class='col-top right1'><img src='"+templatesRoot+"img/spacer.gif' width='24' height='25' alt='' /></td></tr>"+
				"<tr><td class='right-shadow'><img src='"+templatesRoot+"img/spacer.gif'  alt='' /></td></tr>"+
				"<tr><td class='col-bot right2'><img src='"+templatesRoot+"img/spacer.gif' width='24' height='21' alt='' /></td></tr>"+
			"</table>"+
		"</td>"+
	"</tr>"+
	"<tr><td colspan='3'>"+
		"<table width='100%' border='0' cellpadding='0' cellspacing='0'><tr>"+
				"<td class='bottom-left-corner'><img src='"+templatesRoot+"img/spacer.gif' width='50' height='24' alt='' /></td>"+
				"<td class='bottom-bg' width='100%'><img src='"+templatesRoot+"img/spacer.gif' width='100%' height='24'  alt='' /></td>"+
				"<td class='bottom-right-corner'><img src='"+templatesRoot+"img/spacer.gif' width='50' height='24' alt='' /></td>"+
		"</tr></table>"+	
	"</td>"+
	"</table></div>"+
	"<script type='text/javascript' src='"+templatesRoot+"js/pngfix.js'/>"+
	"<script type='text/javascript'>$(function(){$('#popup').pngFix();})</script>");
	
	center("div.popup");
	applyCurtain();
	popupOpened = true;
	endWait();
}

function popupclose() {
	popupclean();
	removeCurtain();
	popupOpened = false;
	
	showUnderPopup();
}

function hideUnderPopup(body) {
	if (!body) body = $("body");
	$("object:visible", body).css("visibility", "hidden");
	$("select:visible", body).css("visibility", "hidden");
}

function showUnderPopup(body) {
	if (!body) body = $("body");
	$("object:hidden", body).css("visibility", "visible");
	$("select:hidden", body).css("visibility", "visible");
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

function getWindowSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return [ myWidth, myHeight ];
}

function getCenteredArea(el){
	w = $(el).width();
	h = $(el).height();
	wsize = getWindowSize();
	wscroll = getScrollXY();
	x = (wsize[0]-w)/2 + wscroll[0];
	y = (wsize[1]-h)/2 + wscroll[1];
	return [x, y, x+w, y+h];
}

function center(el){
	area = getCenteredArea(el);
	$(el).css("left", area[0]);
	$(el).css("top", area[1]);
}

function removeCurtain(){
	$("#popupcurtain").remove();
	$("#popupcurtain1").remove();
}

function ShowBlock(data, width, height){
	//statusControl(0);
	popup(data, width, height);
}

function applyCurtain(){
	wsize = getWindowSize();
	$('body').append('<iframe src="templates/prototype/black.html" style="position:absolute;top:0px;left:0px;width:'+wsize[0]+'px;height:'+wsize[1]+'px;  filter:alpha(opacity=0);-moz-opacity:.0;opacity:.0;" id="popupcurtain1" frameborder="none" scrolling="No" marginheight="0" marginwidth="0"></iframe>');
	$('#popupcurtain').fadeTo(0, 0.5, function(){$("div.popup").show()});
}
	
	
	

function popupclean(){
	$("div.popup").remove();
}

function rolloverEnhancedCopy(container_class, clipped_class, enhanced_class, clip_width, clip_height) {
	
	$("."+clipped_class).css({
		position: 'absolute',
		width: clip_width,
		height:(clip_height + 2),
		clip: 'rect(0px '+clip_width+'px '+clip_height+'px 0px)'
	});
	$("."+container_class).hover(
		function () {
			$(this).children("."+enhanced_class).show();  
			$(this).children("."+clipped_class).hide(); 
		}, 
		function () {
			$(this).children("."+enhanced_class).hide();
			$(this).children("."+clipped_class).show(); 
		}
	);
}


function Rating(div_id){
	this.div = document.getElementById(div_id);
	this.rating = this.div.getAttribute("rating");
	this.articleId = this.div.getAttribute("articleId");
	this.enabled = this.div.getAttribute("enabled") == "1" ? true : false ;
	this.imgPath = this.div.getAttribute("imgPath");
	if (notifier) this.notifier = notifier;

	this.stars = new Array();
	this.starTypes = new Array();

	this.createStars();
};


Rating.prototype.recalculateStars = function (rating){
	if (rating > 5) {
		rating = 5;
	}
	var roundRating = Math.round(rating * 2) / 2;
	var floorRating = Math.floor(roundRating);

	for (var i=1; i <= roundRating; i++){
		this.starTypes[i] = this.imgPath + 'rating_on.gif';
	};

	if (floorRating != roundRating) {
		this.starTypes[floorRating + 1] = this.imgPath + 'rating_half.gif';
		floorRating++;
	};	
	
	for (var i = floorRating + 1; i <= 5; i++){
		this.starTypes[i] = this.imgPath + 'rating_off.gif';
	};
}

Rating.prototype.createStars = function (){
	this.div.innerHTML = "";
	this.recalculateStars(this.rating);
	
	var thisCopy = this;	
	for (var i = 1; i <= 5; i++){
		this.stars[i] = document.createElement("IMG");
		this.stars[i].ratingNum = i;
		this.stars[i].src = this.starTypes[i];
		this.stars[i].width = 12;
		this.stars[i].height = 12;
		this.stars[i].border = 0;
		this.stars[i].alt = i;
		this.stars[i].onmouseover = function() { if (thisCopy.enabled) thisCopy.mouseOver(this.ratingNum) };
		this.stars[i].onmouseout = function() { if (thisCopy.enabled) thisCopy.mouseOut(this.ratingNum) };
		this.stars[i].onclick = function() { if (thisCopy.enabled) thisCopy.mouseClick(this.ratingNum) };
		this.div.appendChild(this.stars[i]);
	};	
};

Rating.prototype.mouseOver = function (num){
	for (var i=1; i <= num; i++){
		this.stars[i].src = this.imgPath + 'rating_over.gif';
	};
};

Rating.prototype.mouseOut = function (num){
	for (var i=1; i <= 5; i++){
		this.stars[i].src = this.starTypes[i];
	};
};

Rating.prototype.mouseClick = function (num){
	if (this.enabled) {
		if (this.notifier) this.notifier.display("Please wait...");
		$(this.div).fadeOut(300);
		var newRating = $.ajax({
			type: "POST",
			dataType: "text",
			url: "index.php?controller=articlestat&action=vote",
			data: {
				id: this.articleId,
				point : num
			},
			async: false
		}).responseText;

		newRating = newRating.replace(/[^0-9\.\,]/g,'');
		if (newRating + "" != "") {
			this.recalculateStars(parseInt(newRating));
		}
		for (var i=1; i <= 5; i++){
			this.stars[i].src = this.starTypes[i];
		};
		this.enabled = false;
		$(this.div).fadeIn(300);
		if (this.notifier) this.notifier.hide();
	};
};


function fc_submit(form_title, url, divId, fc_data, clearFunction){
	var _callback = null;
	if (fc_submit.arguments.length > 4) {
		_callback = fc_submit.arguments[4];
	}
	
	if (notifier) notifier.display("Please wait...");

	$.ajax({
		type: "POST",
		dataType: "xml",
		url: url,
		data: fc_data,
		async: "true",
		success: function(xml) {
			if (notifier) notifier.hide();
			var blankFieldsPresent = emailInvalid = error = false;
			var responseTxt = "";
			$(xml).find("returnedData > errors > error").each(function(){
				if ($(this).attr("type") == "blank") {
					error = true;
					if ($(this).text() == 'Referrer e-mail is invalid' || $(this).text() == 'Recipient # 1 e-mail is invalid') {
						emailInvalid = true;
					} else {
						blankFieldsPresent = true;
					}
				}
				
				if ($(this).attr("type") == "validate") {
					emailInvalid = true;
					error = true;
				}

				if ($(this).attr("type") == "already_registered") {
					responseTxt += "You have already registered with us.\n\n";
					error = true;
				}

			});
			if (blankFieldsPresent) {
				responseTxt += "Please fill all required fields.\n\n";
			};
			if (emailInvalid) {
				responseTxt += "E-mail Address is invalid.\n\n";
			};
			
			if (!error) {
				var cleared = false;
				$(xml).find("returnedData > message").each(function(){
					if ($(this).attr("type") == "thank") {
						if (document.getElementById(divId) && clearFunction){
							var originalId = divId;
							clearFunction();
							var originalContent = $('#' + originalId).html();
							backToForm = function(){
								$('#' + originalId).html(originalContent);
							};
						}
						responseTxt += "<br/><div align='Center'>Thank you for your interest.<br/>You will be hearing from us soon.";
						if (clearFunction){
							responseTxt += "<br/><br/><a href='#' onclick='backToForm(); return false;'>Back to " + form_title + " form</a>";
						};
						responseTxt += "</div>";						
//						document.getElementById(divId).innerHTML = responseTxt;
						$('#' + divId).html(responseTxt);
						if (_callback) _callback();
					} else {
						error = true;
					}
				});
				
				if (error) {
					responseTxt += "Unknown error, please try again later.\n\n";
				};
			};

			if (error) {
				alert(form_title + ":\n\n" + responseTxt);
			};
		},
		error: function (XMLHttpRequest, textStatus, errorThrown) {
			if (notifier) notifier.hide();
			alert(form_title + ": an error occured while connecting to the server, please try again later.\n\n");
		}
	});
}


function crosslinkpopup(text, callerID){
	if ($("div.popup").length > 0) {
		popupclean();
	}
	else {
//		hideUnderPopup();
	}
	var templatesRoot = 'templates/prototype/';
	
	$("body").append("<style>"+
	".popup_blue{"+
		"width:210px;"+
		"font:11px Arial, sans-serif;"+
		"text-align:left;"+
	"}"+
	".popup_blue .blockquote{background:#E7F2F7;border-left:1px solid #9EBED0;border-right:1px solid #9EBED0;border-top:1px solid #9EBED0;}"+
	".popup_blue .blockquote td{padding:7px 0px;}"+
	".popup_blue .blockquote div{padding-left:14px;line-height:150%}"+
	".popup_blue_h{background:#C0DAE6 url("+templatesRoot+"img/popup/blue/header_1px.gif) repeat-x;color:#3A3E40;font-weight:bold;}"+
	".popup_blue_h .FL{margin-left:5px;}"+
	".popup_blue_h .FR{margin-top:2px;}"+
	".popup_blue .bottom_1px{background:url("+templatesRoot+"img/popup/blue/bottom_1px.gif) repeat-x;height:4px;}"+
	"</style>"+
	"<div style='width:"+150+"' id='popup'  class=\"popup popup_blue\">"+
	"<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" style=\"height:18px;\">"+
		"<tr>"+
			"<td width=\"6\"><img src=\""+templatesRoot+"img/popup/blue/header_left.gif\" alt=\"\" width=\"6\" height=\"18\" /></td>"+
			"<td class=\"popup_blue_h\">"+
				"<div class=\"FL\">Linked Information</div>"+
				"<div class=\"FR\"><img src=\""+templatesRoot+"img/popup/blue/close.gif\" class=\"pointer\" onclick=\"popupclose();\" alt=\"Close Window\" width=\"8\" height=\"8\" /></div>"+
			"</td>"+
			"<td align=\"right\" width=\"6\"><img src=\""+templatesRoot+"img/popup/blue/header_right.gif\" alt=\"\" width=\"6\" height=\"18\" /></td>"+
		"</tr>"+
	"</table>"+
	"<table  width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" class=\"blockquote\">"+
		"<tr>"+
			"<td id='popupcontent'>"+text+"</td>"+
		"</tr>"+
	"</table>"+
	"<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" style=\"height:4px;\">"+
		"<tr valign=\"top\">"+
			"<td width=\"6\"><img src=\""+templatesRoot+"img/popup/blue/bottom_left.gif\" alt=\"\" width=\"6\" height=\"4\" /></td>"+
			"<td class=\"bottom_1px\"><img src=\""+templatesRoot+"img//spacer.gif\" alt=\"\"  /></td>"+
			"<td align=\"right\" width=\"6\"><img src=\""+templatesRoot+"img/popup/blue/bottom_right.gif\" alt=\"\" width=\"6\" height=\"4\" /></td>"+
		"</tr>"+
	"</table></div>"+
	"<script type='text/javascript' src='"+templatesRoot+"js/pngfix.js'/>"+
	"<script type='text/javascript'>$(function(){$('#popup').pngFix();})</script>");
	


	if (callerID != null) 
	{
		var caller = document.getElementById(callerID);
	};
	

	if (caller != null) {
		var callerLeft = callerTop = 0;
		var node = caller;
		var sideOffsetValue = 0;
		var gapValue = 20;
		if (node.offsetParent) {
			callerLeft = node.offsetLeft
			callerTop = node.offsetTop
			while (node = node.offsetParent) {
				callerLeft += node.offsetLeft
				callerTop += node.offsetTop
			}
		}
		
		var divLeft = callerLeft + caller.offsetWidth + sideOffsetValue;
		var divTop = callerTop - 10;
		var divWidth = 150;
		var divHeight = 150;

		if (divLeft + divWidth + gapValue > screen.width) {
			divLeft = callerLeft - divWidth - sideOffsetValue;
		}

		if (divTop + divHeight + gapValue > screen.height - 150) {
			divTop = callerTop - divHeight + caller.offsetHeight;
		}
		
		if (divLeft < 0) {
			divLeft = 0;
		}
		
		if (divTop - document.body.scrollTop < 0) {
			divTop = document.body.scrollTop;
		}
		$("div.popup").css("left", divLeft);
		$("div.popup").css("top", divTop);
	}else{
		center("div.popup");
	}

	popupOpened = true;
	endWait();
}


/**
 * peredaem id elementa v kotorom sleduet iskat' ssylki dlya zameny
**/
function cross_link(id, href){
	if (id == '' || id == undefined){
		 $("a.shortcut").each(function(){
	    	$(this).attr('href', "javascript:ajax('"+href+"','GET','keyword="+$(this).attr('id')+"&type="+$(this).attr('name')+"',before,function(msg){crosslinkpopup(msg,'"+$(this).attr('id')+"')})");
	    })
	}else{
		 $("a.shortcut", "#"+id).each(function(){
	    	$(this).attr('href', "javascript:ajax('"+href+"','GET','keyword="+$(this).attr('id')+"&type="+$(this).attr('name')+"',before,function(msg){crosslinkpopup(msg,'"+$(this).attr('id')+"')})");
	    })
	}
	
}

function doBlink() {
   
}

function startBlink() {
  
}