//need to load image from here, and when loaded do effects
var bIE = ($.browser.msie)?true:false;
var bO = ($.browser.opera)?true:false;


if (typeof console == "undefined" || typeof console.log == "undefined") var console = { log: function() {} }; 

/*------AJAX------ 
function(){
*/


ajax = function(str, foo, _el){
	//alert('http://www.'+domain+'/ajax/ajax.' + type + '.php?' + str + '&hash=' + hash)
	
	console.log('http://www.' + domain + '/api?' + str)
	str = str.replace(/#/g, '[num]');
	$.ajax({
		url: 'http://www.' + domain + '/api?' + str + '&hash=' + hash,
		cache: false,
		dataType: 'json',
		success: function(_data){
			console.log('requested url: %o', this.url);
			foo(_el, _data);
		},
		error: function(){
		}
	})
	return true;
}

/*
}
------/AJAX------*/


//function for next image (yeah, need to make it better)
var img_r, image_loaded, image_loaded_fun, next_obj;
var join_err = 0;


load_Img = function(src){
	var img = new Image();
	image_loaded = false;
	$(img)
		.load(function(){
			img_r = Math.round($('#bg').width()*1000/$('#bg').height())/1000;
			win_Resize(false);
			$('#bg').fadeIn(300, function(){$('#bg_old').remove()})
			$('.author').show();
			image_loaded = true;
			image_loaded_fun();
		})
		.attr('id', 'bg')
		.attr('src', src)
		
		//.attr('src', 'http://i.' + domain + '/l/' + src);
	$('#bg').remove();
	$('#main').prepend(img);
	
	//setting map marker position, only for first time
	if(!next_obj)map_marker_pos(spot_x, spot_y);
	
}

//changing size of backgroung image
win_Resize = function(boo){
	var d = $(window);
	var m = $('#main');
	m.css('width', Math.max(950, d.width())).css('height', Math.max(500, d.height()));
	var w = m.width();
	var h = m.height();
	if(img_r){
		if(w/img_r<h)w = Math.ceil(h*img_r);	
		$('#bg').css('width', w).css('height', Math.ceil(w/img_r));
	}
	$('#circle').css('top', h/2);
	
	
	$('#map').css('top', h/2);
	$('#nav').css('top', h/2);
	$('#numbers').css('top', h - h/4 + 95).fadeIn(300);

	//$('h3').css('bottom', h/2 - 30);
	if(boo){
		$('#circle').show();
		$('#map').show();
		$('h3').show();//.find('small:first').addClass('clear')
	}
}

$(window).resize(function(){
	win_Resize();
})
win_Resize(true);

//blocking join for from submitting if there are mistakes in inputs
$('.join form').submit(function(){
	var t = $(this);
	if($('.loading', t).length==0){
		join_err = 0;
		$('input', t).each(function(i){
			if(i<4){
				var t = $(this);
				if(t.val().length==0 || t.data('E') || (t.is('input[type=checkbox]') && !t.is(':checked'))){
					//fix for terms
					join_err++;
					t.addClass('e').parent().addClass('error');
				}
			}
		})
		console.log('join_err: %o', join_err);
		
		if(join_err)return false;
	}else{
		return false;
	}
});

$('#join').click(function(){
	//urlC(j_h);
	var j = $('.join').data('v');
	if(!j){
		
		j = $('.join');
		j.data('v', true);
		
		$('input[type="text"]', j).blur(function(){
			var t = $(this);
			if(t.val()!=''){
				
				if(t.val()!=t.data('pv')){
					t.removeClass('e');
					t.parent().addClass('loading');
					//http://www.xxx/ajax/ajax.check_name.php?register=1&name=dfds&_=1260270862297
					//http://www.xxx/ajax/ajax.check_email.php?register=1&email=dip@elempi.com&_=1260271042678
					
					var n = t.attr('name');
					//TODO: not sure if need .replace(/&/g, '[amp]')
					ajax('action=validate&type=user&key=' + n + '&value=' + t.val().replace(/&/g, '[amp]'), function(_el, _data){
						
						var ep = _el.parent();
						
						//adding/removing error class
						(_data.ok)?ep.removeClass('error'):ep.addClass('error');
						
						//saving something
						//TODO: need to check this
						_el.data('E', (_data.ok)?false:true)
						
						//saving original label
						if(!ep.data('original_label'))ep.data('original_label', $('label', ep).text())
						
						
						//changin label
						$('label', ep).text((_data.ok)?ep.data('original_label'):_data.re)
						
						
						ep.removeClass('loading');
						//saving previous value, so next time we won't send the same
						_el.data('pv', t.val());
						
						
					}, t);
				}
				
			}else{
				t
					.data('E', true)
					.parent().addClass('error');
			}
		});
		
		/*$('input[name="pass1"]', j).blur(function(){
			var t  = $(this);
			if(t.val() == $('input', t.parent().prev()).val()){
				t.removeClass('e').data('E', false);
			}else{
				t.addClass('e').data('E', true);
			}
		});*/
		
		$('input[name="password"]', j).blur(function(){
		//	var t  = $('input', $(this).parent().next());
			if($(this).val().length>0)$(this).removeClass('e').data('E', false).parent().removeClass('error');
			
			/*console.log('>>>>>>><<<<<<<<<<<<: %o', t.val());
			
			if(t.val().length>0){
				console.log('37246327864789236478: %o', 1);
				//if(t.val() == $(this).val()){
					t.removeClass('e').data('E', false);
				//}else{
					//t.addClass('e').data('E', true);
				//}
			}else{
				t.addClass('e').data('E', true);
			}*/
		}).data('E', false);
		
		/*$('input[name="captcha_code"]', j).unbind('blur').blur(function(){
			var t = $(this);
			if(t.val().length==4){
				t.removeClass('e').data('E', false);
			}else{
				t.addClass('e').data('E', true);
			}
		});*///.data('E', true);
		
		$('.submit', j).click(function(){
			if($('.terms input').is(':checked')){
				$('.terms').removeClass('error');
				var tpp = $(this).parent().parent();
				if(join_err==0)tpp.submit()	
			}else{
				//setting checkbox to error
				$('.terms').addClass('error');
			}
		});
		
		$('.back', j).click(function(){
			//urlC('#', $(this));
			$('.join').fadeOut(200);
		});
	}
	
	

	$('.join').hide().css('top', 0).fadeIn(300).find('input:first').focus();
	//if(bIE)$('.main').hide();
	
	//checking errors and emty fields, so I'll remove red bg for empty ones
	$('.join input[type="text"]').each(function(){
		if($(this).val().length==0)$(this).parent().removeClass('error')
	})


});

$('#log_in').click(function(){
	//urlC(l_h);
	var j = $('.log_in').data('v');
	if(!j){
		j = $('.log_in');
		j.data('v', true);
		$('.submit', j).click(function(){
			var tpp = $(this).parent().parent();
			tpp.submit()
		});
		$('.back', j).click(function(){
			//urlC('#', $(this));
			$('.log_in').fadeOut(300)
		});
		$('#forgot').click(function(){
			//on reply
			
			var inp = $('.log_in input[name=email]');
			
			if(inp.val().length>0){
			
				/// 
				$('.msg_bg, .msg_text')
					.hide()
					.html('')
					.css('top', 0)
					.append()
					.fadeIn(300);
				
				$('.msg_text').addClass('msg_loading')
				
				ajax('action=amnesia&type=user&id=' + $('.log_in input[name=email]').val().replace(/&/g, '[amp]') + '&forgot=1', function(_el, _data){
					//removing loader 	
					$('.msg_text').removeClass('msg_loading')
					//showing message
					$('<h5' + ((_data.ok)?'':' class="error"') + '><a class="back"></a>' + _data.re + '</h5>').hide().fadeIn(300).appendTo($('.msg_text'));
				});
			}else{
				inp.parent().addClass('error');
				inp.unbind('blur').bind('blur', function(){
					var p = $(this).parent();
					if($(this).val().length>0)p.removeClass('error')
				})
			}
		})
	}
	
	$('.log_in').hide().css('top', 0).fadeIn(300);//.find('input[name="email"]').focus()
	//if(bIE)$('.main').hide();
	
	
});

map_marker_pos = function(_x, _y, _ani){
	//console.log('here: %o', $('#map div').css('top'));
	$('#map div').animate({'top': 195-Math.floor(_x*1.05), 'left': 170+Math.floor(_y*0.86)}, (_ani)?800:0).fadeIn(300);
}

image_loaded_fun = function(){
	if(next_obj){
		
		var o = next_obj;
		
		console.log('here: %o', next_obj);
		//title animation
		$('#map h3').slideUp(300, function(){
			$(this).slideDown(300).html('<a href="' + o.url + '">' + o.name + ' <b>*</b></a>');
		});
		$('#map h4').slideUp(300, function(){
			if(o.locations){
				var i = 0;
				var str = ''
				$.each(o.locations, function(_i, _v){
					//TODO: pretty dirty with www and with s/
					if(i<2)str += ((i>0)?' / ':'') + '<a href="http://www.' + domain + '/search/' + o.type + 's/' + _i + ':' + _v[0] + '">' + _v[1] + '</a>';
					i++;
				});
				$(this).slideDown(300).html(str);
			}
		});
	
		map_marker_pos(o.x, o.y, true);
	
	
		//changing author
		$('.author').attr('href', o.author_url).find('span').text(o.author)
		
	
	
	
	
		//removing loadbar
		$('.next_loading').removeClass('next_loading');
		
		//clearing object (after animation is over)
		setTimeout(function(){next_obj = 0;}, 500)
		
	}
}

start = function(){
	switch(aujo){
		case 1:
			$('#join').click();
			break;
		case 2:
			$('#log_in').click();
			break;
		case 3:
			$('.msg_bg, .msg_text').css('top', 0);
			break;
		case 4:
			$('.msg_bg, .msg_text').css('top', 0);
			break;
		
	}
	
	
	//adding languages list
	
	$('#lang select').change(function(){
		var str = window.location;
		str = str.toString();
		var l = str.length;
		var str1 = str.substr(l-1, l);
		if(str1!='/')str += '/';
		window.location = str + '?lang=' + $('option:selected', $(this)).val();
	});
	
	
	$('.next').click(function(e){
		
		image_loaded = true
		//loading obj and resetting background object
		e.preventDefault();
		
		//need to show loader on the button
		if(image_loaded){
			var t = $(this);
			
			t.addClass('next_loading')
			
			
			
			//sending request
			ajax('action=wallpaper&type=user', function(_el, _data){
				//saving obj as global to use it in image_loaded_fun
				next_obj = _data.data;

				/*next_obj = {
								'img': 'http://files.droplr.com/files/11976572/yAOZ.00625_genevagraffiti_1600x1200.jpg',
								'x': 20.4274952407,
								'y': -60.7014792400,
								'name': 'New Name',
								'link': 'New Name',
								'country': 'Country',
								'state': 'State',
								'country_link': 'country-ID',
								'state_link': 'state-ID',
								'author': '???',
								'author_link': 'author_link'
							}*/
				
				

				$('#bg').attr('id', 'bg_old');
				load_Img(next_obj.pic);
			});
			
			
		}
	});
	
	
	$('.msg_text').click(function(){
		$('.msg_text, .msg_bg').fadeOut(300);
		if(aujo==3){
			$('#log_in').click();
		}else if(aujo==4){
			$('#join').click();
			//$('#log_in').click();
		}
	})
	
	
	//IE7 and 8 are different, fixing in js, css is only one for both
/*	if(bIE && $.browser.version.substr(0,1)<8){
		$('input[type="text"], input[type="password"]').css('marginLeft', -48);
		$('.code input').css('marginLeft', 0);
	}*/
}



