// JavaScript Document
var partnerURL = '/partner/'
var userURL = '/users/'
var uurl = '/users/ajax_auth.asp'
var purl = '/partner/ajax_auth.asp'

$(function(){
	if(typeof(reserves) != 'undefined') {
		for(i in reserves) {
			$('#notify_currency').append($('<option value="' + i + '">' + i + '</option>'));
		}
	}

	$(".cntnr table.colored tr:even, #faq .cntnr div:odd").css("background-color", "#f0f0e7");
	$('#aterms').click(function(){
		var path = ''
		if($('#reg_client:checked').length) path='/eula.asp'
		else path='/partner_agreement.asp'
		$('#headerform').attr('action', path).submit()
	})

	$(".reg a").click(function() {
		if($(this).hasClass('uin')) {
			document.location.href='/users/';
		} else if($(this).hasClass('pin')) {
			document.location.href='/partner/partner_statistics.asp';
		} else {
			$("div.trigger").show();
			if($(".auth").hasClass("act"))
			{
				$(".auth").removeClass("act");
				$("#auth").toggle();
			}
			$(".reg").toggleClass("act");
			$("#reg").toggle();
		}
		
		if(!$("#reg").hasClass("act")) {
			$("#reg p").hide();
			//$('div.trigger').hide();
			$('#puser_password2').show()
			$('#user_password2').val('').hide()
			$('#puser_password').show()
			$('#user_password').val('').hide()
			$("#reg input").each(function(){
				if($(this).attr('defaultValue')) $(this).val($(this).attr('defaultValue'))
				else if (this.defaultValue) $(this).val(this.defaultValue)
			});
		} 
		if($('#reg').css('display') == 'none') $('div.trigger').hide()
		if(typeof(PIE)!='undefined') PIE.attach( document.getElementById('reg') ); 
		return false;
	});

	$(".auth a").click(function() {
			if($(this).hasClass('uin') || $(this).hasClass('pin')) {
				aobj = $(this)
				$.get($(this).hasClass('uin')?uurl:purl, {act:'logout'}, function(data){
					var obj;eval('obj='+data);
					if(obj.success) { 
						$(".reg a").removeClass('pin').removeClass('uin').html($(".reg a").attr('login'));
						aobj.removeClass('pin').removeClass('uin').html(aobj.attr('login'));
						document.location.href='/'
					}
				})
			} else {
				$("div.trigger").show();
				if($(".reg").hasClass("act")){
					$(".reg").removeClass("act")
					$("#reg").toggle();
					}
				$(".auth").toggleClass("act");
				$("#auth").toggle();
			}
			
			if(!$("#auth").hasClass("act")) {
				$("#auth p").hide();
				$("#auth input").each(function(){
					if($(this).attr('defaultValue')) $(this).val($(this).attr('defaultValue'))
					else if (this.defaultValue) $(this).val(this.defaultValue)
				})
			} 
			if($('#auth').css('display') == 'none') $('div.trigger').hide()
			if(typeof(PIE)!='undefined') PIE.attach( document.getElementById('auth') ); 
			return false;
		});

	$(".trigger").click(function() {
		
		$(".reg").removeClass("act");
		$("#reg").hide();
		$(".auth").removeClass("act");
		$("#auth").hide();
		$(this).hide();
	});

	
	function inputOnFocusEventHandler(){
		if(this.value == $(this).attr("#fillvalue"))
		{
			this.value = "";
			if(this.id == "puser_password" || this.id == "puser_password2" || this.id == "ppwd_auth") {
				this.value = $(this).attr("#fillvalue");
				$("#"+this.id).hide();
				$("#"+this.id.substr(1)).show();
				$("#"+this.id.substr(1)).focus();					
			};
		}
	};
	
	function inputOnBlurEventHandler(){
		if(this.value == "")
		{
			this.value = $(this).attr("#fillvalue");
			if(this.id == "user_password" || this.id == "user_password2" || this.id == "pwd_auth") {
				$("#"+this.id).val("");
				$("#"+this.id).hide();
				$("#p"+this.id).show();
			};
		}
	}
	
	$("#headerform input:text,input:password").focus(inputOnFocusEventHandler);
	$("#authform input:text,#authform input:password").focus(inputOnFocusEventHandler);
	$("#pass_email input:text").focus(inputOnFocusEventHandler);
	//
	$("#headerform input:text,input:password").blur(inputOnBlurEventHandler);
	$("#authform input:text,#authform input:password").blur(inputOnBlurEventHandler);
	$("#pass_email input:text").blur(inputOnBlurEventHandler);

/**/
	$("#help .slider").click(function () {
		if(!$('#h1tp1').hasClass('arrd')) {
			$("#help .sld").toggle()
			$('.sldsm,.sldem').toggle();
			//$("#help .opn").toggle();
		}
	});
	$("#help .sld").click(function () {
		$(this).toggle();
		$('.sldsm,.sldem').toggle();
	});

	$(".tabs .news").click(function() {
		 if(isIE7()) {
			$(".tabs .news").css('margin-top', '4px')
			$(".tabs .replies").css('margin-top', '0') 
		}
			if(!$(this).hasClass("act")) {
				$(this).toggleClass("act");
				$(".tabs .replies").toggleClass("act");
				$("#right .cntnr .replies").parents(".cntnr").hide();
				$("#right .cntnr .news").parents(".cntnr").show();
			}
			if(isIE7()) {
				$(".tabs .replies").css('margin-top', '4px')
				$(".tabs .news").css('margin-top', '0')
			}
		});

	$(".tabs .replies").click(function() {
			if(isIE7()) {
				$(".tabs .news").css('margin-top', '0')
				$(".tabs .replies").css('margin-top', '4px')
			}
			if(!$(this).hasClass("act")) {
				$(this).toggleClass("act");
				$(".tabs .news").toggleClass("act");
				$("#right .cntnr .news").parents(".cntnr").hide();
				$("#right .cntnr .replies").parents(".cntnr").show();
			}
			//if(!$(".tabs .news").hasClass("act")) {
			if(isIE7()) {
				$(".tabs .news").css('margin-top', '4px')
				$(".tabs .replies").css('margin-top', '0')
			}
		});

	$(".wide>b").hover(function() {
		$(this).next("em").animate({opacity: "show", top: "-65"}, "fast");
		}, function() {
			$(this).next("em").animate({opacity: "hide", top: "-85"}, "fast");
		});

	//
	$("#notify_a, .remember_c").fancybox({
		'titleShow'			: false,
		'padding'			: 0,
		'scrolling'	: 'no',
		'transitionIn'		: 'none',
		'transitionOut'		: 'none'
		});
	
	$(".rnotify").click(function(){
		var obj = $(this);
		$("#notify_currency option").removeAttr('selected');		
		
		$('#notify_currency option').each(function(){
			if($(this).val() == obj.attr('cur')) {
				$(this).attr('selected', 'selected');
			//} else {
				//$(this).attr('selected', '');
				//$(this).removeAttr('selected');
			}
		});
		
		$('#notify p.err').hide();
		if(obj.attr('fbinit') != '1') 
		{
			obj.attr('fbinit', '1');
			obj.fancybox({
				'titleShow'			: false,
				'padding'			: 0,
				'scrolling'	: 'no',
				'transitionIn'		: 'none',
				'transitionOut'		: 'none'
			});
			obj.click();
		}
		return false;
	});
	
	$(".showmap").each(function(){
		var obj = $(this)
		var id = 'showmapimg' + ($('.showmapimg').length+1)
		var s = '<img src="'+obj.attr('src')+'" class="showmapimg" id="'+id +'" style="display:none" />'
		$(s).appendTo($('#showmapimg'))
		obj.attr('rel', id)
		loadImgSize(obj)
	}).click(function(){
		var obj = $(this)
		$('.showmapimg').hide()
		$('#'+obj.attr('rel')).show()
		if(obj.attr('w')) $('#showmap').css('width', obj.attr('w')+"px")
		if(obj.attr('fbinit') != '1') {	
			obj.attr('fbinit', '1')
			obj.fancybox({
				'titleShow'			: false,
				'padding'			: 0,
				'scrolling'	: 'no',
				'transitionIn'		: 'none',
				'transitionOut'		: 'none'
			});
			obj.click()
		} 
		return false;
	});
	
});


function loadImgSize(obj)
{
	var newImg = new Image();
	newImg.id = obj.attr('rel')
	newImg.onload = function() { obj.attr('w', this.width); obj.attr('h', this.height); }
	newImg.src = obj.attr('src');
}

var __notify = false;
function checkNotify() {
	
	if(__notify) {
		return;
	}
	$('#notify p.err').hide();
	var ch = true 
	if(parseFloat($('#notify_quantity').val()) <= reserves[$("#notify_currency option:selected").val()]) {
		$('#err_notify_quantity').html($('#msg_notify_quantity').html()).show();
		ch = false;
	}
	if(ch)	
	{
		__notify = true;
		$.post('/notifications.asp'
			, {currency:currIds[$("#notify_currency option:selected").val()],quantity:$('#notify_quantity').val(),email:$('#notify_email').val(),should_block:$('#notify_save:checked').length, duration:$("#notify_time option:selected").val(),'captcha-string':$('#notify_captcha').val(),'validation_code':$('#validation_code').val()}
			, function(data) {
				var obj;
				eval('obj='+data);
				var err = false;
				for(var i in obj.errors) {
					$('#err_'+i).html(obj.errors[i]).show()
					err = true
				}
				if(!err) 
				{
					$("#notify_a").trigger("click")
					$("#notify_quantity").val('')
					$("#notify_email").val('')
					$("#notify_captcha").val('')
					$("#notify_save").removeAttr('checked')				
					$('#notify img.captcha').attr('src', '/partner/captcha.asp?type=captcha-string&reload=1&t='+(new Date()).getTime())
					//$.fancybox.close()
				}
				__notify = false;
		});
	}
}


function CreateBookmarkLink() {

 var title = "Intexchange"; 
 var url = "http://intexchange.ru";

	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url,"");
	} else if( window.external ) { // IE Favorite
		window.external.AddFavorite( url, title); }
	else if(window.opera && window.print) { // Opera Hotlist
		return true; }
}
function isIE7() {
	return parseInt($.browser.version, 10) == 7 && $.browser.msie
}
