$(document).ready( function() {
	if( $( 'body#page_ar_object_details' ).length ) { // Detailansicht
		// Google Maps
		if( object_latitude != 0 && object_longitude != 0 ) {
			maps_get_api();
		}
		
		// Fancybox initialisieren
		$('.videoChangerContainer a').attr('rel', 'gallery').fancybox({
			'titlePosition': 'inside'
		});
		$('#images .small_images a').attr('rel', 'gallery').fancybox({
			'titlePosition': 'inside'
		});
		
		// Listener für Bild-Vergrößerung
		$("a[id^='large_tab_href']").click(function() {
			var clickedPictureHref = $(this).attr('href');
			
			var i = 0;
			while( i != -1 )
			{
				var tmpElement = getLargeImageName($('#tab_image' + i).attr('src'));
				if( tmpElement != undefined)
				{
					if( tmpElement == clickedPictureHref)
					{
						$('#tab_image' + i).click();
						return false;
					}
				} else {
					i = -1;
				}
				
				i++;
				if(i > 29)
				{
					i = -1;
				}
			}
			return false;
		});
		
		// Facebook "Gefällt mir"-Button: Vorschaltbutton
		$( '#detail_facebook_like_button_container img' ).click( function() {
			$( '#detail_facebook_like_button_container' ).html( '' );
			$( '<script src="http://connect.facebook.net/de_DE/all.js"><\/script>' ).appendTo( '#detail_facebook_like_button_container' );
			$( '<img id="detail_facebook_like_button_container_loader" width="16" height="16" alt="" src="/fileadmin/ang/template/img/icon_loader.gif" style="margin-left: 36px;" />' ).appendTo( $('#detail_facebook_like_button_container') );
		});
		
		// Facebook "Gefällt mir"-Button: Eigentlicher Facebook-Button
		$(window).bind('call_fbAsyncInit', function(){
			$( '#detail_facebook_like_button_container' ).html( '' );
			FB.init( {xfbml: true} );
			$( '<fb:like href="http://www.angelreisen.de/angelreise-' + $('#object_key').val() + '.html" layout="button_count" show_faces="false" width="130" font=""></fb:like>' ).appendTo( '#detail_facebook_like_button_container' );
		});
		window.fbAsyncInit = function(){
			$(window).trigger('call_fbAsyncInit');
		};
	}
});

function maps_get_api()
{
	var script = document.createElement("script");
	script.src = "http://www.google.com/jsapi?key=AIzaSyDJ93GAeGaAaBJvFlNg1cTV3si3hvFQla8&callback=maps_initialize_api";
	script.type = "text/javascript";
	document.getElementsByTagName("head")[0].appendChild(script);
}

function maps_initialize_api() 
{
	google.load("maps", "2", {"callback" : maps_build });
}

function maps_build()
{
	var map = new GMap2(document.getElementById('googles_map'));
	var big_map = new GMap2(document.getElementById('googles_big_map'), {size: new GSize(360,320)});
	
	map.setCenter(new GLatLng(object_latitude, object_longitude), 12);
	var point = new GLatLng(object_latitude, object_longitude);
	map.addOverlay(new GMarker(point));
	map.addControl(new GSmallMapControl());
	
	big_map.setCenter(new GLatLng(object_latitude, object_longitude), 12);
	var point2 = new GLatLng(object_latitude, object_longitude);
	big_map.addOverlay(new GMarker(point2));
	
	big_map.addControl(new GSmallMapControl());
	big_map.addControl(new GMapTypeControl());
}

function showGoogleRouteplanner()
{
	var country = $('#country').val() + ',';
	var city = $('#city').val() + ',';
	var street = $('#street').val();
	
	request_params = '?mrsp=0&daddr=' + lat + ',' + lon + '&saddr=';
	if( country != ',' ) {
		request_params += country;
	}
	if( city != ',' ) {
		request_params += city;
	}
	request_params += street;
	open('http://maps.google.com/' + request_params, 'google');
}


function switchTab( tab_id )
{
	$('.tabs li').each(function() {
		$(this).removeClass('active');
	});
	$('.tabContentText').hide();
	$('#' + tab_id + '_tab').addClass('active');
	$('#' + tab_id).show();
}

// images
var current_image = 0;
var current_tab_image = 1;

function scrollImage( mode, area, id, image_cnt ){
	var img_no = 0;
	if (mode == 'forward') {
		if (area == 'tab') {
			current_tab_image++;
			current_tab_image = current_tab_image % image_cnt;
			img_no = current_tab_image;
		}
		else {
			current_image++;
			current_image = current_image % image_cnt;
			img_no = current_image;
		}
	}
	else {
		if (area == 'tab') {
			current_tab_image--;
			if (current_tab_image == -1) {
				current_tab_image = image_cnt - 1;
			}
			img_no = current_tab_image;
		}
		else {
			current_image--;
			if (current_image == -1) {
				current_image = image_cnt - 1;
			}
			img_no = current_image;
		}
	}
	switchImage(area, id, img_no, image_cnt);
}

function switchImage(area, id, img_no, image_cnt){
	if (area == 'tab') {
		next_tab_image = (img_no + 1) % image_cnt;
		prev_tab_image = img_no - 1;
		if (prev_tab_image == -1) {
			prev_tab_image = image_cnt - 1;
		}
		$('#large_tab_image2').attr('src', getLargeImageName($('#' + id + next_tab_image).attr('src')));
		$('#large_tab_image1').attr('src', getLargeImageName($('#' + id + img_no).attr('src')));
		$('#large_tab_image0').attr('src', getLargeImageName($('#' + id + prev_tab_image).attr('src')));

		$('#large_tab_img_caption2').text($('#' + id + next_tab_image).attr('title'));
		$('#large_tab_img_caption1').text($('#' + id + img_no).attr('title'));
		$('#large_tab_img_caption0').text($('#' + id + prev_tab_image).attr('title'));

		$('#large_tab_href2').attr('href', getLargeImageName($('#' + id + next_tab_image).attr('src')));
		$('#large_tab_href1').attr('href', getLargeImageName($('#' + id + img_no).attr('src')));
		$('#large_tab_href0').attr('href', getLargeImageName($('#' + id + prev_tab_image).attr('src')));
	}
	else {
		if( $('#' + id + img_no) )
		{
			$('#large_img').attr('src', getLargeImageName($('#' + id + img_no).attr('src')));
			$('#large_img_caption').text($('#' + id + img_no).attr('title'));
		}
	}
}

function getLargeImageName(smallImageName)
{
	// Split URL
	var smallPicUrl = smallImageName.split('/');
	var smallPicFilename = smallPicUrl[smallPicUrl.length-1];
	smallPicUrl.pop();
	
	// Build the Path without filename
	var smallPicPath = '';
	for( var i = 0; i < smallPicUrl.length; i++ )
	{
		smallPicPath += smallPicUrl[i] + '/';
	}
	
	var tmpFilename = smallPicFilename.split('_');
	var newFilename = smallPicPath;
	for( var i = 0; i < tmpFilename.length; i++ )
	{
		if( i != 0 || tmpFilename[0].split('x').length == 1 )
		{
			newFilename += tmpFilename[i];
			if(i < tmpFilename.length-1)
			{
				newFilename += '_';
			}
		}
	}
	
	return newFilename;
}

function showGallery(){
	var images = new Array();
	$('#images .small_images a').each(function(){
		var title = '';
		// just one child
		$(this).children().each(function(){
			title = $(this).attr('title');
		});
		if (title != '') {
			var tmp_obj = new Object();
			tmp_obj.href = $(this).attr('href');
			tmp_obj.title = title;
			images.push(tmp_obj);
		}
		else {
			images.push($(this).attr('href'));
		}
	});
	
	$.fancybox(images, {
		'titlePosition': 'inside'
	});
}

function showAllComments(objectkey){
	$.ajax({
		url: "/scripts/ar/ar_object_details.php",
		type: "POST",
		dataType: "html",
		data: "object_key="+objectkey+"&show_comments=1&page=1",
		success: function(comments)
		{
			$('div.all_comment_review').html( $(comments).html() );
		}
	});
}

function showPage(objectkey, page){
	$.ajax({
		url: "/scripts/ar/ar_object_details.php",
		type: "POST",
		dataType: "html",
		data: "object_key="+objectkey+"&show_comments=1&page="+page,
		success: function(comments)
		{
			$('div.all_comment_review').html( $(comments).html() );
			$(".page a.page_" + page).css({'font-weight' : 'bold'});
		}
	});
}

function showPerOperationNumber(operation_number)
{
	$('.flickr_images a[rel="showimages_' + operation_number + '"]').fancybox({
		'titlePosition': 'inside'
	});
}
