function loader(elementId,size){
	$('#'+elementId).html('<img src="/img/' + size + '-loader.gif" alt="loading" />');
}

// for setting map locations
var initLocation = {};
var initPoint = {};
// Get address from user input
function getAddress(locId) {
	// Vars
	var address = $('#address'+locId).text();
	address = address.replace(/<br>/g, ' ');
	// Return address
	return address;
}

function findAddress(address,locId) {
	var map = new google.maps.Map2(document.getElementById('map')) ;
	map.addControl(new GSmallMapControl);
	var geocoder = new GClientGeocoder();
	if (geocoder) {
		geocoder.getLatLng(address, function(point) {
			if (!point) {
				map.clearOverlays();
				map.setCenter(new GLatLng(50.731247, 7.10434), 3);
				//alert(address + " not found. Try dragging the marker to your location");
			} else {
				map.clearOverlays();
				map.setCenter(point, 3);
				marker = new GMarker(point);
				map.addOverlay(marker);
			}
		});
	}
}
	
// initiate map
function mapInit(locId){
		
	if ($('#LocationGlat' + locId).val() != '' && $('#LocationGlong' + locId).val() != '') {
		var map = new google.maps.Map2(document.getElementById('map')) ;
		map.addControl(new GSmallMapControl);
		var center = new GLatLng($('#LocationGlat' + locId).val(),$('#LocationGlong' + locId).val());
		map.setCenter(center, 3);
		var marker = new GMarker(center);
		map.addOverlay(marker);
	}else{
		var address = getAddress(locId);
		findAddress(address,locId);
	}
}

function getCheckedList(trigger){
	var list = '';
	$(trigger).parents('.checkbox-list').find('input.option-checkbox:checked').each(function(e){
		if(e == 0){ 
			list += $(this).attr('rel');
		}else{
			list += ', ' + $(this).attr('rel');
		}
	});
	return list;
}

$(function(){
	Shadowbox.init();
	if(location.hash.substring(1) == "contact"){
		$('.active').removeClass('active');
		$('#contact-link').addClass('active');
		$('.profile-content:visible').hide();
		$('#contact').show();

		setTimeout('mapInit(0)', 500);
	}
	
	$('.view-website').click(function(){
		$.get($('#urlLang').val() + '/search/ajax_generatewebclick/' + $(this).attr('rel'));
	});
	
	$('.view-details-btn').click(function(){
		$.get($('#urlLang').val() + '/search/ajax_generatecontactclick/' + $(this).attr('rel'));
		$(this).next('span.contact-details').show();
		$(this).hide();
		return false;
	});
	
	$(document).click(function(e){
		if($(e.target).parents('.popList').length < 1){
			$('.popList:visible').hide();
			$('.select').removeClass('active');
		}
	});
	
	$('#search-form .select').live('click', function(){
		if($(this).find('.option-list').is(':visible')){
			$('.option-list').hide();
			$(this).removeClass('active');
		}else if($('.popList').is(':visible')){
			$('.popList').hide();
			if($(this).find('.option-list').find('li').length > 0){
				$(this).find('.option-list').show();
				if($(this).find('.option-list').find('li').length > 25){
					$(this).find('.option-list').css('height', 500).show();
				}
				$(this).addClass('active');
			}
		}else{
			if($(this).find('.option-list').find('li').length > 0){
				$(this).find('.option-list').show();
				if($(this).find('.option-list').find('li').length > 25){
					$(this).find('.option-list').css('height', 500).show();
				}
				$(this).addClass('active');
			}
		}
		return false;
	});
	
	$('.option-list li').live('click', function(){
		if(!$(this).is('.primary-seperator')){
			var selectList = $(this).parents('.select-list');
			$(selectList).children('li.selected').removeClass('selected');
			$(this).addClass('selected');
			$(selectList).find('.selected-option').html($(this).html());
			$(this).parents('.select').removeClass('active');
			
			if($(this).attr('rel') == 0){
				$(selectList).children('input').val('');
			}else{
				$(selectList).children('input').val($(this).attr('alias'));
			}
			
			if($(this).is('.country-option')){
				$(this).parents('form').find('button').attr('disabled', false);
			}
			//$('.option-list').hide();
		}
	});
	
	$('li.primary-option').click(function(){
		loader('selected-country','small');
		$('#SearchSecondary0').val('');
		$('#countryId').val($(this).attr('rel'));
		$.get($('#urlLang').val() + '/search/ajax/getSecondaryZeroOptions/' + $(this).attr('rel'), function(data){
			if(data !=''){
				$('#selected-country').html($('#selected-country').attr('rel'));
				$('#country-list').html(data);
			}else{
				$('#selected-country').html($('#selected-country').attr('nonetext'));
				$('#country-list').html('');
			}
		});
		if($('#basicCitySelect').length > 0){
			loader('basicCitySelect','small');
			var countryId = $(this).attr('rel');
			$.get($('#urlLang').val() + '/search/ajax/getSecondaryOneOptions/' + $(this).attr('rel'), function(data){
				$('#basicCitySelect').html(data);
			});
		}
	});
	
	$('#searchLanguage').click(function(){
		if($('#defaultSearchKeyword').val() != '' || $('#SearchPrimary').val() != ''){
			return true;
		}else{
			return false;
		}
	});
	
	$('li.primary-option').click(function(){
		$('#searchLanguage').attr('disabled', false);
	});
	
	$('#defaultSearchKeyword').keyup(function(){
		if($(this).val() != ''){
			$('#searchLanguage').attr('disabled', false);
		}else{
			//$('#searchLanguage').attr('disabled', true);
		}
	});
	
	$('.selected-options').click(function(){
		if($(this).next('.checkbox-list').is(':visible')){
			$('.checkbox-list').hide();
			$(this).parents('.advanced-option').removeClass('active');
		}else if($('.popList').is(':visible')){
			$('.popList').hide();
			$(this).next('.checkbox-list').show();
			$(this).parents('.advanced-option').removeClass('active');
		}else{
			$(this).next('.checkbox-list').show();
			$(this).parents('.advanced-option').addClass('active');
		}
		return false;
	});
	
	$('.checkbox-list input').click(function(){
		var list = getCheckedList($(this));
		if(list == ''){
			$(this).parents('.advanced-option').find('.selected-options').html('<em>' + $('#noneSelected').html() + '</em>');
		}else{
			$(this).parents('.advanced-option').find('.selected-options').html('<a href="#" class="tooltip" title="' + list + '">' + list + '</a>');
		}
	});
	
	$('a.select-all').click(function(){
		$(this).parents('.checkbox-list').find('input.option-checkbox').each(function(e){
			$(this).attr('checked', true);
		});
		var list = getCheckedList($(this));

		$(this).parents('.advanced-option').find('.selected-options').html('<a href="#" class="tooltip" title="' + list + '">' + list + '</a>');
		return false;
	});
	
	$('a.group-select-all').click(function(){
		var list = getCheckedList($(this));
		$(this).parents('.advanced-group').find('input.option-checkbox').each(function(e){
			$(this).attr('checked', true);
		});
		$(this).parents('.advanced-option').find('.selected-options').html('<a href="#" class="tooltip" title="' + list + '">' + list + '</a>');
		return false;
	});
	
	$('a.unselect-all').click(function(){
		$(this).parents('.checkbox-list').find('input').each(function(){
			$(this).attr('checked', false);
		});
		$(this).parents('.advanced-option').find('.selected-options').html('<em>' + $('#noneSelected').html() + '</em>');
		return false;
	});
	
	$('a.close').click(function(){
		$(this).parents('.popList').hide();
		return false;
	});
	
	$('#family-sites').hover(function(){
		$(this).stop().animate({
			marginTop: "0px"
		}, 200);
	}, function(){
		$(this).stop().animate({
			marginTop: "-90px"
		}, 200);
	});
	
	$('.home-more').click(function(){
		var linkText = $(this).attr('rel');
		$(this).attr('rel', $(this).text());
		$(this).text(linkText);

		$(this).next('.more-text').slideToggle(200);
		return false;
	});
	
	$('#profile-table #side-menu a').click(function(){
		if(!$(this).is('.view-website')){
			if(!$(this).is('.active')){
				$('.active').removeClass('active');
				$(this).addClass('active');
				var container = $(this).attr('rel');
				$('.profile-content:visible').hide();
				$('#'+container).fadeIn(500);
				
				if($(this).attr('rel') == "contact"){
					setTimeout('mapInit(0)', 500);
				}
			}
			return false;
		}
	});
	
	// find the div.fade elements and hook the hover event
	$('div.share').hover(function() {
		// on hovering over find the element we want to fade *up*
		var fade = $(this).find('.share-options');
 
		// if the element is currently being animated (to fadeOut)...
		if (fade.is(':animated')) {
			// ...stop the current animation, and fade it to 1 from current position
			fade.stop().fadeTo(500, 1);
		} else {
			fade.fadeIn(500);
		}
	}, function () {
		var fade = $(this).find('.share-options');
		if (fade.is(':animated')) {
			fade.stop().fadeTo(500, 0);
		} else {
			fade.fadeOut(500);
		}
	});

	//initiate tooltips
	$('#search-form .tooltip, form .tooltip, #share-links .tooltip').livequery( function(){
		$(this).tooltip({track: true, delay: 0,showURL: false, showBody: " ~ ", extraClass: "bodyTip"});
	});
	
	$('#search-tabs .tooltip, .translation-indicators .tooltip').tooltip({track: true,delay: 0,showURL: false, showBody: " ~ ", extraClass: "flagTip" });
	
	//create share links id and open lightbox
	$('#share-links a').live('click', function(){
		var shareUrl = $(this).attr('href');
		$.post($('#share-form').attr('action'), $('#share-form').serialize(), function(data){
			shareUrl = shareUrl.replace('%_shareURL_%', data);
			var title = $('a.friend').attr('title');
			Shadowbox.open({
				content:    shareUrl,
				player:     "iframe",
				title:      title,
				height:     550,
				width:      990
			});

		});
		return false;
	});
});

// Marvel static functions
var Marvel = {
	// Html helper
	htmlHelper:{
		_vars:{
			loadingImage:'loading.gif',
			loadingImageBig:'loading.gif',
			loadingImageSmall:'loadingSmall.gif'
		},

		fadeReplace:function (source, target) {
			$(target).fadeOut(400);
			setTimeout(function () {
				$(target).html($(source).html());
				$(target).fadeIn(400);
			}, 450);
		},

		slideHide:function (target) {
			$(target).slideUp(800);
			setTimeout(function() { $(target).empty(); }, 900);
		},

		showLoading:function (target) {
			Marvel.htmlHelper.showCustomLoading(target, 'center', Marvel.htmlHelper._vars.loadingImageBig);
		},

		showCustomLoading:function (target, align, image) {
			if (!$(target).html().match(/loading/i)){
				$(target).html('<div id="loadingdiv" style="text-align:' + align + '"><img src="/img/' + image + '" alt="" /></div>');
			}
		}

	},

	// Page messaging via flash functionality
	message:{
		settings:{
			slideTime:900,
			displayTime:3000
		},

		flash:function (message) {
			// Make sure message is not empty
			message = $.trim(message);
			if (!message){
				return;
			}

			// Attach message
			if ($('#flash').html()) {
				$('#flash').append("<br>" + message);
			} else {
				$('#flashPoint').html('<div class="flash" id="flash">' + message + '</div>');
			}

			// Show
			if (!Marvel.message.isFlashing) {
				$('#flashPoint').hide();
				$('#flashPoint').slideDown(Marvel.message.settings.slideTime);
				Marvel.message.isFlashing = setTimeout(Marvel.message.hide, Marvel.message.settings.displayTime);
			} else {
				clearTimeout(Marvel.message.isFlashing);
				Marvel.message.isFlashing = setTimeout(Marvel.message.hide, Marvel.message.settings.displayTime);
			}
		},

		hide:function () {
			$('#flashPoint').slideUp(Marvel.message.settings.slideTime);
			setTimeout(Marvel.message.clear, Marvel.message.settings.slideTime + 5);
			Marvel.message.isFlashing = false;
		},

		clear:function() {
			$('#flashPoint').empty();
		}
	},

	// Php clones (http://kevin.vanzonneveld.net/code/php_equivalents/php.js)
	php:{
		empty:function(mixed_var) {
			var key;
			
			if (mixed_var === ""
				|| mixed_var === 0
				|| mixed_var === "0"
				|| mixed_var === null
				|| mixed_var === false
				|| mixed_var === undefined
			){
				return true;
			}
			if (typeof mixed_var == 'object') {
				for (key in mixed_var) {
					if (typeof mixed_var[key] !== 'function' ) {
						return false;
					}
				}
				return true;
			}
			return false;
		}
	}
};
