
jQuery(function ($) {

  // Log all jQuery AJAX requests to Google Analytics
  $(document).ajaxSend(function(event, xhr, settings){ 
	  if(settings.url.indexOf('update_toolbar.php') < 0 
			  && settings.url.indexOf('get_districts.php') < 0
			  && settings.url.indexOf('get_hotornot.php') < 0) {
		  _gaq.push(['_trackPageview', settings.url]);
	  }
  });

}); 


$(document).ready(function()
{
	//stick the footer at the bottom of the page if we're on an iPad/iPhone due to viewport/page bugs in mobile webkit
	if(navigator.platform == 'iPad' || navigator.platform == 'iPhone' || navigator.platform == 'iPod')
	{
		$('body').css('height', '100%');
		//$('#globalcontainer').css('min-height', '100%');
		var iWidth = $('#globalcontainer').width();
		var iTop = $(window).height() < $('#globalcontainer').height() ? $('#globalcontainer').height() : $(window).height();
		$('#toolbar').css('position', 'absolute').css('width', iWidth + 'px').css('top', iTop + 'px');
		$('#infoIcons').css('marginRight', $('#sky').width() + 'px');
		$('#messageBox,#newMessageBox').css('right', ($('#sky').width() + 24) + 'px');
		$('#friendsBox,#newFriendsBox').css('right', ($('#sky').width() - 35) + 'px');
	};
	
	if ($("a.image_link").length > 0)
		$("a.image_link").fancybox({transitionIn: 'none', transitionOut: 'none', hideOnContentClick: true, showCloseButton: false, centerOnScroll: false, titlePosition: 'inside', titleFormat: formatFancyTitle});
	
	// nur laden wenn auf such-resultat seite und user angemeldet...
	if (($('#page-search_results').length) && (!is_guest))
		get_hotornot('');
	
	$('#hotfilterF').click(function(e)
	{
		$('#hotfilterF').addClass('bold').css('cursor', null);
		$('#hotfilterMF').removeClass('bold').css('cursor', 'pointer');
		$('#.hotfilterM').removeClass('bold').css('cursor', 'pointer');
		
		$('#hotornot').find('div.hotloader').show();
		$('#hotornot').find('div.hotuser').hide();
		
		get_hotornot('F');
	});
	
	$('#hotfilterMF').click(function(e)
	{
		$('#hotfilterF').removeClass('bold').css('cursor', 'pointer');
		$('#hotfilterMF').addClass('bold').css('cursor', null);
		$('#hotfilterM').removeClass('bold').css('cursor', 'pointer');
		
		$('#hotornot').find('div.hotloader').show();
		$('#hotornot').find('div.hotuser').hide();
		
		get_hotornot('MF');
	});
	
	$('#hotfilterM').click(function(e)
	{
		$('#hotfilterF').removeClass('bold').css('cursor', 'pointer');
		$('#hotfilterMF').removeClass('bold').css('cursor', 'pointer');
		$('#hotfilterM').addClass('bold').css('cursor', null);
		
		$('#hotornot').find('div.hotloader').show();
		$('#hotornot').find('div.hotuser').hide();
		
		get_hotornot('M');
	});
	
	$('input.hotstar').rating(
	{
		callback: function(value, link)
		{
			var k = $('#hotornot').find('input.hotindex').val();
			
			$('#hotornot').find('div.hotloader').show();
			$('#hotornot').find('div.hotuser').hide();

			$.post
			(
				base_url + "/api/vote_hotornot.php", 
				{ index: k, rating: value, csrf_token: 'nothing' },
				function (data)
				{					
					if (!data.success)
						alert('Fehler beim Voten!');
					else
					{
						$('input.hotstar').rating('select', '', false);
						get_hotornot('');					
					}
				},
				"json"
			);
		}
	});
	
		$(".boxUL").find("li").live('mouseover', function()
		{
			$(this).addClass('boxLiOver'); 
		}).live('mouseout', function()
		{ 
			$(this).removeClass('boxLiOver'); 
		}).live('click', function(e)
		{
			if (e.target.tagName != 'A' && $(this).find('a.target_link').attr('href'))
				window.location.href = $(this).find('a.target_link').attr('href');
		});
		
		$(".boxOL").find("li").bind('click', function(e) { 
			if (e.target.tagName != 'A' && $(this).find('a.target_link').attr('href'))
				window.location.href = $(this).find('a.target_link').attr('href');
		});

		$('div.languageSwitcher a').click(function()
		{
			var lang_code = $(this).html().toLowerCase();
			
			$.get
			(
				base_url + "/api/set_language.php", 
				{ lang_code: lang_code },
	  			function(r)
	  			{
					if (r.success)
						location.reload(true);
					else
						alert(r.error);
				}, 
				"json"
			);
			
			return false;
		});
		
		listAction_handler();

});

function addLocationSelector(html_state_id, html_district_id, base_url, all)
{
	$('#' + html_state_id).change(function()
	{
		var state_id = $(this).val();
		
		if (state_id.substr(0, 1) == 'c')
		{
			$('#' + html_district_id).html('');
			$('#' + html_district_id).attr('disabled', 'disabled');
		}
		else
		{
			$('#' + html_district_id).removeAttr('disabled');
			
			$.get
			(
				base_url + "/api/get_districts.php", 
				{ state_id: state_id },
	  			function(districts)
	  			{
					var options = '';
					for (var i = 0; i < districts.length; i++) 
					{
						options += '<option value="' + districts[i].id + '"';
			
						if (i == 0 && !all) 
							options += ' selected="selected"';
			
						options += '>' + districts[i].dsc + '</option>';
					}
										
					if (districts.length == 0)
						$('#' + html_district_id).attr('disabled', 'disabled');
					else if (all)
						options = '<option value="" selected="selected">Alle</option>' + options;
						
					$('#' + html_district_id).html(options);
				}, 
				"json"
			);
		}
	});
}
	
function listAction_handler() {
	
	$("DIV.listDiv").each( function(e) {
		$(this).bind('mouseover', function() {
			$(this).find(".listActions").show();
			$(this).addClass('listDivOver');
		});

		$(this).bind('mouseout', function() {
			$(this).find(".listActions").hide();
			$(this).removeClass('listDivOver');
		});
		
		$(this).bind('click', function(e) {
			if (e.target.tagName != 'A' && e.target.tagName != 'INPUT')
			{
				if ($(this).find('a.target_link').attr('href') && $(this).find('a.target_link').attr('href') != '#')
					window.location.href = $(this).find('a.target_link').attr('href');
				else
					$(this).find('a.target_link').trigger('click');
			}
		});

	});		
	
/*
	$("DIV.shortList").each( function(e) {
		$(this).bind('mouseover', function() {
			$(this).find(".listActions").show();
			$(this).addClass('listDivOver'); 
		});

		$(this).bind('mouseout', function() {
			$(this).find(".listActions").hide();
			$(this).removeClass('listDivOver'); 
		});

		$(this).bind('click', function(e) { 
			if (e.target.tagName != 'A')
				window.location.href = $(this).find('a.target_link').attr('href');
		});
	});
	*/
}
	
//
// Clone Friend Row
//
jQuery.fn.bussiCloneFriendRow = function(friend, base_url, profile_url) 
{
	var $this = $(this).clone(true);

	// replace all html ids
    $this.removeAttr('id').removeClass('hidden').find('*').each(function ()
    {
    	var id = $(this).attr('id');
    	var type = $(this).get(0).nodeName.toLowerCase();
    	
    	if (id != "")
    		$(this).attr('id', id + '-' + friend.id);
    		
    	if (type == "input" || type == "select")
    	{
    	    $(this).attr
		    (
		        'name',
		        function() 
		        { 
		        	return $(this).attr('name').replace('%id%', friend.id); 
		        }
		    );
		}
		else if (type == "label")
		{
			var attrFor = $(this).attr('for');
			
			if (attrFor != "")
				$(this).attr('for', attrFor + '-' + friend.id);
		}
				
    });
    
    // populate data
    $this.find('span.friendId').html(friend.id);
    $this.find('a.friendName').attr('href', profile_url + friend.id).html(friend.username);
    $this.find('span.friendAge').html(friend.age);
    $this.find('span.friendLocation').html(friend.location);
    $this.find('img.friendImage').attr('src', base_url + friend.picture);
    $this.find('img.userOnlineImage').attr('src', base_url + '/api/onlinestatus.php?type=i&id=' + friend.id);
    $this.find('img.friendGenderIcon').attr('src', base_url + friend.gender_icon);

    return $this;
};

//
//Clone User Row
//
jQuery.fn.bussiCloneUserRow = function(user, base_url, profile_url) 
{
	var $this = $(this).clone(true);

	// replace all html ids
	$this.removeAttr('id').removeClass('hidden').find('*').each(function ()
	{
		var id = $(this).attr('id');
		var type = $(this).get(0).nodeName.toLowerCase();
	 	
	 	if (id != "")
	 		$(this).attr('id', id + '-' + user.id);
	 		
	 	if (type == "input" || type == "select")
	 	{
	 	    $(this).attr
		    (
		        'name',
		        function() 
		        { 
		        	return $(this).attr('name').replace('%id%', user.id); 
		        }
		    );
			}
		else if (type == "label")
		{
			var attrFor = $(this).attr('for');
			
			if (attrFor != "")
				$(this).attr('for', attrFor + '-' + user.id);
		}				
	});
 
	// populate data
	$this.find('span.userId').html(user.id);
	$this.find('a.userLink').attr('href', profile_url + user.id).html(user.username);
	$this.find('span.userAge').html(user.age);
	$this.find('span.userLocation').html(user.location);
	$this.find('img.userImage').attr('src', base_url + user.picture);
	$this.find('img.userOnlineImage').attr('src', base_url + '/api/onlinestatus.php?type=i&id=' + user.id);
	$this.find('img.userGenderIcon').attr('src', base_url + user.gender_icon);
	
	return $this;
};

//
//Clone Guestbook Row
//
jQuery.fn.bussiCloneGBRow = function(data, base_url, profile_url) 
{
	var $this = $(this).clone(true);

	// replace all html ids
	$this.removeAttr('id').removeClass('hidden').find('*').each(function ()
	{
		var id = $(this).attr('id');
		var type = $(this).get(0).nodeName.toLowerCase();
	 	
	 	if (id != "")
	 		$(this).attr('id', id + '-' + data.GID);
	 		
	 	if (type == "input" || type == "select")
	 	{
	 	    $(this).attr
		    (
		        'name',
		        function() 
		        { 
		        	return $(this).attr('name').replace('%id%', data.GID); 
		        }
		    );
			}
		else if (type == "label")
		{
			var attrFor = $(this).attr('for');
			
			if (attrFor != "")
				$(this).attr('for', attrFor + '-' + data.GID);
		}				
	});

	// populate data
	$this.find('span.gbId').html(data.GID);
	$this.find('a.userLink').attr('href', profile_url + data.UID).attr('title', data.utitle).html(data.username);
	$this.find('img.gbImage').attr('src', base_url + data.picture);
	$this.find('span.gbText').html(data.GTEXT);
	$this.find('div.gbComment').html(data.comments);
	$this.find('span.datetime').html(data.CREDATE);
	
	return $this;
};

//get hotornot
function get_hotornot(gender)
{
	$.get
	(
		base_url + "/api/get_hotornot.php", 
		{ gender: gender },
		function (rows)
		{
			if (rows.length > 0)
			{
				$('#hotornot').find('a.image_link').attr('href', base_url + rows[0].picture + ',l.jpg').html('<img style="border:1px solid #535B89" src="' + base_url + rows[0].picture + ',m.jpg" width="70" height="78" />');
				$('#hotornot').find('a.hotlink').attr('title', rows[0].utitle).attr('href', base_url + '/profil/' + rows[0].id);
				$('#hotornot').find('span.hotname').html(rows[0].username);
				$('#hotornot').find('span.hotalter').html(rows[0].age);
				$('#hotornot').find('span.hotvotes').html(rows[0].votes);
				$('#hotornot').find('span.hotrank').html(rows[0].voting);
				$('#hotornot').find('input.hotindex').val(rows[0].index);
				
				$('#hotornot').find('div.hotloader').hide();
				$('#hotornot').find('div.hotuser').show();
			}
		}, 
		"json"
	);
}

function formatFancyTitle(title, currentArray, currentIndex, currentOpts) 
{
    return '<div id="tip7-title"><span><a style="font-weight:bold" href="javascript:;" onclick="$.fancybox.close();">X</a></span>' + (title && title.length ? '<b>' + title + '</b>' : '' )+'</div>';
}
