$jQ = jQuery.noConflict();

var core_url = "/index.php";
var is_stop = false;
var clip = null;

$jQ(document).ready(function(){
	jQuery("ul#list li")
	.mouseover(function() {
		jQuery(jQuery(this).attr("id"))
			.css("left", jQuery(this).offset().left-20)
			.css("top", jQuery(this).offset().top-8)
			.show()
			.mouseout(function() {
				jQuery(this).hide();
			});
	})
	
	$jQ('a#favorites').bind('click', function() {
		if (window.sidebar) {
			window.sidebar.addPanel($jQ(this).attr('title'), $jQ(this).attr('href'),'');
		} else if (document.all || (window.opera && window.print)) {
			window.external.AddFavorite($jQ(this).attr('href'), $jQ(this).attr('title'));
		}
		return false;
	});
	if (document.all) {
		$jQ('span.front').css('display', 'inline');
		$jQ('a#front').bind('click', function() {
			$jQ(this).css('behavior', 'url(#default#homepage)');
			document.getElementById('front').setHomePage('http://news76week.ru/');
		});
	}
	
	$jQ('.bubbleInfo').each(function () {
        var distance = 10;
        var time = 250;
        var hideDelay = 500;
        var hideDelayTimer = null;
        var beingShown = false;
        var shown = false;
        var trigger = $jQ('#field_input', this);
        var info = $jQ('.popup_message', this).css('opacity', 0);

        $jQ(trigger).focus(function () {
        	if (hideDelayTimer) clearTimeout(hideDelayTimer);
            if (beingShown || shown) {
                return;
            } else {
                beingShown = true;
                info.css({
					top: trigger.offset().top - $jQ('table#form').offset().top - info.height() + trigger.height(),
					left: trigger.offset().left + 100,
                    display: 'block'
                }).animate({
                    top: '-=' + distance + 'px',
                    opacity: 1
                }, time, 'swing', function() {
                    beingShown = false;
                    shown = true;
                });
            }
            return false;
        }).blur(function () {
            if (hideDelayTimer) clearTimeout(hideDelayTimer);
            hideDelayTimer = setTimeout(function () {
                hideDelayTimer = null;
                info.animate({
                    top: '-=' + distance + 'px',
                    opacity: 0
                }, time, 'swing', function () {
                    shown = false;
                    info.css('display', 'none');
                });
            }, hideDelay);
            return false;
        });
    });
    jQuery('a[rel*=skyweb]').skyweb();
});

function LoadData(op, module_name, value) {
	$jQ.ajax({
		url: core_url,
		type: "POST",
		data: "op=" + op + "&module_name=" + module_name + "&value=" + value,
		dataType : "json",
		beforeSend: function() {
			return php.beforeSend();
		},
		success: function(data, textStatus) {
			return php.success(data, textStatus);
		},
		error: function (xmlEr, typeEr, except) {
			return php.error(xmlEr, typeEr, except);                  
		},
		complete: function (XMLHttpRequest, textStatus) {
			return php.complete(XMLHttpRequest, textStatus);
		}
	});
	return false;
}

function LoadDataDetail(op, module_name, value, field, fvalue) {
	$jQ.ajax({
		url: core_url,
		type: "POST",
		data: "op=" + op + "&module_name=" + module_name + "&value=" + value + "&field=" + field + "&fvalue=" + fvalue,
		dataType : "json",
		beforeSend: function() {
			return php.beforeSend();
		},
		success: function(data, textStatus) {
			return php.success(data, textStatus);
		},
		error: function (xmlEr, typeEr, except) {
			return php.error(xmlEr, typeEr, except);                  
		},
		complete: function (XMLHttpRequest, textStatus) {
			return php.complete(XMLHttpRequest, textStatus);
		}
	});
	return false;
}

function LoadForm(form) {
	$jQ.ajax({
		url: core_url,
		type: "POST",
		data: $jQ('#'+form).formToArray(true),
		enctype: "multipart/form-data",
		dataType : "json",
		beforeSend: function() {
			return php.beforeSend();
		},
		success: function(data, textStatus) {
			return php.success(data, textStatus);
		},
		error: function (xmlEr, typeEr, except) {
			return php.error(xmlEr, typeEr, except);                  
		},
		complete: function (XMLHttpRequest, textStatus) {
			return php.complete(XMLHttpRequest, textStatus);
		}
	});
	return false;
}

function clearForm(form) {
	$jQ('form#'+form+' :input').each(function() {
		var type = this.type, tag = this.tagName.toLowerCase();
		if (type == 'text' || type == 'password' || tag == 'textarea')
			this.value = '';
		else if (type == 'checkbox' || type == 'radio')
			this.checked = false;
		else if (tag == 'select')
			this.selectedIndex = -1;
	});
};

function CommentsLoad(op, module_name, id) {
	$jQ.ajax({
		url: core_url,
		type: "POST",
		data: "module_name=comments&op="+op+"&module="+module_name+"&value="+id,
		dataType : "json",
		beforeSend: function() {
			return php.beforeSend();
		},
		success: function(data, textStatus) {
			return php.success(data, textStatus);
		},
		error: function (xmlEr, typeEr, except) {
			return php.error(xmlEr, typeEr, except);                  
		},
		complete: function (XMLHttpRequest, textStatus) {
			return php.complete(XMLHttpRequest, textStatus);
		}
	});
	return false;
}

function KarmaClick(value, module, id, uid, mid) {
	$jQ.ajax({
		url: core_url,
		type: "POST",
		data: "module_name=comments&op=karma_click&module="+module+"&id="+id+"&value="+value+"&uid="+uid+"&mid="+mid,
		dataType : "json",
		beforeSend: function() {
			return php.beforeSend();
		},
		success: function(data, textStatus) {
			return php.success(data, textStatus);
		},
		error: function (xmlEr, typeEr, except) {
			return php.error(xmlEr, typeEr, except);                  
		},
		complete: function (XMLHttpRequest, textStatus) {
			return php.complete(XMLHttpRequest, textStatus);
		}
	});
	return false;
}

function OCNews(block) {
	if ($jQ('#'+block+' #title').next().is(':hidden')) {
		$jQ('#'+block+' #data').slideDown('normal');
		$jQ('#'+block+' #stat').attr('src', '/i/minus.gif');
	} else {
		$jQ('#'+block+' #data').slideUp('normal');
		$jQ('#'+block+' #stat').attr('src', '/i/plus.gif');
	}
}

function usl(event) {
	if(event.KeyCode == 13) alert('ok');
}
function SelectAll(el) {
	$jQ("select#"+el+" option").each(function(){
		$jQ(this).attr("selected",true);
	});
}
function OnOff(element) {
	if (document.getElementById(element).style.display == 'none') {
		document.getElementById(element).style.display = 'inline';
	} else {
		document.getElementById(element).style.display = 'none';
	}
}

function LoadDataField(op, name, id, fid, fname) {
	$jQ.ajax({
		url: core_url,
		type: "POST",
		data: "op="+op+"&module_name="+name+"&value="+id+"&fid="+fid+"&fname="+fname,
		dataType : "json",
		beforeSend: function() {
			return php.beforeSend();
		},
		success: function(data, textStatus) {
			return php.success(data, textStatus);
		},
		error: function (xmlEr, typeEr, except) {
			return php.error(xmlEr, typeEr, except);                  
		},
		complete: function (XMLHttpRequest, textStatus) {
			return php.complete(XMLHttpRequest, textStatus);
		}
	});
	return false;
}

function OpenClose(id) {
	if (jQuery('#'+id).is(':hidden')) {
		jQuery('#'+id).show();
	} else {
		jQuery('#'+id).hide();
	}
}

function SaveForm() {
	jQuery('#send_comments').remove();
	jQuery('#submit_button_message').click();
	jQuery('#is_save').val(1);
	jQuery('#submit_button_message').click();
}

function initBigImage(el, image, module, id) {
	jQuery(el).parent().parent().parent().parent().parent().parent().find('#view_big_img').attr('href', '/files/'+module+'/'+id+'/'+image);
	jQuery(el).parent().parent().parent().parent().parent().parent().find('#big_img').attr('src', '/files/'+module+'/'+id+'/300_'+image).fadeIn(100);
}

function LoadDataURL(url) {
	$jQ.ajax({
		url: core_url,
		type: "POST",
		data: url,
		dataType : "json",
		beforeSend: function() {
			return php.beforeSend();
		},
		success: function(data, textStatus) {
			return php.success(data, textStatus);
		},
		error: function (xmlEr, typeEr, except) {
			return php.error(xmlEr, typeEr, except);                  
		},
		complete: function (XMLHttpRequest, textStatus) {
			return php.complete(XMLHttpRequest, textStatus);
		}
	});
	return false;
}
