/*
 * Image preview script 
 * powered by jQuery (http://www.jquery.com)
 * 
 * written by Alen Grakalic (http://cssglobe.com)
 * 
 * for more info visit http://cssglobe.com/post/1695/easiest-tooltip-and-image-preview-using-jquery
 *
 */
 
this.imagePreview = function(){	
	/* CONFIG */
		
		xOffset = 6;
		yOffset = 30;

		direction_v = "top";
		direction_h = "left";
		
		win_height = window.innerHeight;//Non IE
		win_width = window.innerWidth;//Non IE
		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result
		
	/* END CONFIG */
		$("a.preview").hover(function(e){
			this.t = this.title;
			this.title = "";	
			var c = (this.t != "") ? "<br/>" + this.t : "";
			var newpic = $("body").append("<a id='preview' class='preview'><span style='text-align:center;min-width:250;width:auto;width:250;min-height:150;height:auto;height:150;'><img src='"+ this.id +"' alt='Image preview' /><div class='previewtext'>"+ c +"</div></span></a>");								 
			var new_pos_v = e.pageY + yOffset;
			var new_pos_h = e.pageX + xOffset;
			win_height = window.innerHeight;//Non IE
			if (win_height < 1 || win_height == null){
				win_height = document.documentElement.clientHeight; //IE6+  
				if (win_height < 1 || win_height == null){
					win_height = document.body.clientHeight; //IE8+
					if (win_height < 1 || win_height == null){
						win_height = screen.height;//General Width of full screen
					}
				}
			}
			win_width = window.innerWidth;//Non IE
			if (win_width < 1 || win_width == null){
				win_width = document.documentElement.clientWidth; //IE6+  
				if (win_width < 1 || win_width == null){
					win_width = document.body.clientWidth;
					if (win_width < 1 || win_width == null){
						win_width = screen.width;//General Width of full screen
					}
				}
			}
			
			//Calculates the horizontal center of the screen/Element (including scrollbars)
			var cur_page_size_x = win_width;
			if (cur_page_size_x > screen.width) cur_page_size_x = screen.width;//General Width of full screen
			//Calculates the vertical center of the screen/Element  (including scrollbars)
			var cur_page_size_y = win_height;
			if (cur_page_size_y > screen.height) cur_page_size_y = screen.height;//General Width of full screen
			//if ((win_height - document.body.scrollTop) > screen.height) win_height=screen.height;//General Width of full screen
			
			//alert(document.body.scrollTop + "-" + document.body.scrollLeft + "-" + win_width + "-" + win_height + "-" + e.pageX + "-" + e.pageY);
			direction_h = "left";
			if (((cur_page_size_x * 0.5) + document.body.scrollLeft) <= new_pos_h){
				direction_h = "right";
				new_pos_h = (win_width - e.pageX) + (xOffset * 0.25);
			}
			direction_v = "top";
			if (((cur_page_size_y * 0.5) + document.body.scrollTop) <= new_pos_v){
				direction_v = "bottom";
				new_pos_v = (win_height - e.pageY) + (yOffset * 0.25);
			}
			$("#preview")
				.css(direction_v,(new_pos_v) + "px")
				.css(direction_h,(new_pos_h) + "px")
				.css("z-index",1002)
				.fadeIn("fast")						
				.hover(function(e){
				$("#preview").remove();
			});

		},
		function(){
			this.title = this.t;	
			$("#preview").remove();
		}),
		$("a.preview").click(function(e){
			$("#this_selection").value = this.t;
			$("#preview").remove;
		});	
		$("a.preview").mousemove(function(e){
			var new_pos_v = e.pageY + yOffset;
			var new_pos_h = e.pageX + xOffset;
			if (direction_h == "right"){
				new_pos_h = (win_width - e.pageX) + (xOffset * 0.25);
			}
			if (direction_v == "bottom"){
				new_pos_v = (win_height - e.pageY) + (yOffset * 0.25);
			}
			
			$("#preview")
				.css(direction_v,(new_pos_v) + "px")
				.css(direction_h,(new_pos_h) + "px")
		});	
};

// starting the script on page load
$(document).ready(function(){
	if ( $("#do_juery_stuff").length == 1 ) {
		
		// Init picture Handling
		imagePreview();
		
			// Request all avail. bikes
		var init_number_of_bikes = function(){ 
			var cat_id = $('#chosen_cat_id').val();
			var size = $('#size_id :selected').val();
			var arrive = $('#date_arrive').val();
			var depart = $('#date_depart').val();
			$.ajax({
				type: "get",
				url:  "./includes/ajax.php?action=getBikes&category_id="+cat_id+"&size_id="+size+"&date_arrive="+arrive+"&date_depart="+depart,
				success: function(msg){
					if(msg > 0){
						$('.msg').css({'display' : 'block'});
						$('.msgfail').css({'display' : 'none'});
						$('.nrBikes').html(msg);
						$('#chosen_cat_available').val(msg);
					}else{
						$('.msg').css({'display' : 'none'});
						$('.msgfail').css({'display' : 'block'});
						$('.nrBikes').html('0');
						$('#chosen_cat_available').val('0');
					}
				}
			});
		}
	
	// check if we have a cat_id via REQUEST
	if( $('#chosen_cat_id').val() != "" || $('#chosen_cat_name').val() != ""){
		init_number_of_bikes();
		var cat_name = $('#chosen_cat_name').val();
		var chosen_full_id = $('#chosen_cat_id').val();
		cat_name=cat_name.replace(/\\\'/g, "'");
		cat_name=cat_name.replace(/\\\`/g, "`");

		// Hide cat select img in parent window
		$('#drop_img_cat').css({'display' : 'none'});
		
		// Display cat name
		$('#selected_cat_name').css({'display' : 'block'});
		$('#selected_cat_name').html(cat_name);
		
		// Display edit link
		$('#edit_cat_link').css({'display' : 'block'});

		// change img border for selected bike
		$('#'+chosen_full_id).css({'border' : '3px solid Green'});
	}
	
	// check if we have a ped_id via REQUEST
	if( $('#chosen_ped_id').val() != "" || $('#chosen_ped_name').val() != ""){
		
		var ped_name = $('#chosen_ped_name').val();
		var chosen_full_id =  $('#chosen_ped_id').val();
		ped_name=ped_name.replace(/\\\'/g, "'");
		ped_name=ped_name.replace(/\\\`/g, "`");
		
		// Hide ped select img in parent window
		$('#drop_img_ped').css({'display' : 'none'});
		
		// Display ped name
		$('#selected_ped_name').css({'display' : 'block'});
		$('#selected_ped_name').html(ped_name);
		
		// Display edit link
		$('#edit_ped_link').css({'display' : 'block'});
	
			// change img border for selected bike
		$('#'+chosen_full_id).css({'border' : '3px solid Green'});
}
	
	// If "date" chosen, calculate new
	if($('input[name="date_depart"]').val() != "" || $('input[name="date_arrive"]').val() != ""){
		init_number_of_bikes();
	}
	
	// If "size" is chosen, calculate new
	$('#size_id').change( function() {
		init_number_of_bikes();
	});
	
	// Dropdown "Bike Category"
	$('.cat_select').click(function(){
		
		// Proof if date is given
		if($('input[name="date_depart"]').val() == "" || $('input[name="date_arrive"]').val() == ""){
			
			$.get("./includes/ajax_get_lang_value.php?key=missing_date", function(data){
				// date missing
				alert(data);
			});

		}else{
			// calaculate current bikes
			try{
				init_number_of_bikes();
			} catch (e)  {}
		
			// Open Bike Cat Overlay
			$.blockUI({centerX: true, centerY: true, message: $('#overlay_cat'), css: { width: '650px', height:'690px', overflow:'auto', top:'20px', left:'11%', right:'75%'} }); 

			/* DISABLED DCT MVM 30.11.2009 Due to selection done on picture click.
			// Button "Choose"
			$('#choose_me_cat').click(function() { 
				// unblock overlay
				$.unblockUI();
				$('#preview').remove();
			});
			
			$('#cancel_me_cat').click(function() { 
				init_number_of_bikes();		
	 		 	$.unblockUI();
				$('#preview').remove();
			});
			*/
			// Button "Auswahl l򳣨en"

	        $('#unselect_me_cat').click(function() { 
				// Hide cat name
				$('#selected_cat_name').css({'display' : 'none'});
				// hide edit link
				$('#edit_cat_link').css({'display' : 'none'});
				// Show cat select img
				$('#drop_img_cat').css({'display' : 'block'});
				
				// delete hidden form values
				$('input[name="category_id"]').val('');
				$('input[name="category_name"]').val('');
				
				// set all img borders to standard
				$("img[class^='cat_bikes_img']").each(function(){
					$('.cat_bikes_img').css({'border' : '3px solid #ccc'});
				});
				init_number_of_bikes();		
	 		 	$.unblockUI();
				$('#preview').remove();
			});
						
			$('#x_me_cat').click(function() { 
				init_number_of_bikes();		
	 		 	$.unblockUI();
				$('#preview').remove();
			});
		
		}
			
	});	
	
	$('.cat_bikes_img').click(function(){
		var chosen_full_id = this.id;
		var single_chosen_ids = chosen_full_id.split("_");
		var c_chosen_id = single_chosen_ids[1];
		
		$('#choose_me_cat').attr("disabled", false); 
		
		// set all img borders to standard
		$("img[class^='cat_bikes_img']").each(function(){
			$('.cat_bikes_img').css({'border' : '3px solid #ccc'});
		});
		
		// change img border for selected bike
		$('#'+chosen_full_id).css({'border' : '3px solid Green'});
		
		// Hide cat select img in parent window
		$('#drop_img_cat').css({'display' : 'none'});
		
		// Display cat name
		var cat_name = $(this).parent().attr('rel');
		$('#selected_cat_name').css({'display' : 'block'});
		$('#selected_cat_name').text(cat_name).html(cat_name);
		
		// Display edit link
		$('#edit_cat_link').css({'display' : 'block'});
		
		// write cat_id and cat_name into hidden form field
		$('input[name="category_id"]').val(c_chosen_id);
		$('input[name="category_name"]').val(cat_name);

		// calculate number of bikes
		init_number_of_bikes();		
		$.unblockUI();
		$('#preview').remove();
	});
	

	// Dropdown "Accessoires"
	$('.ped_select').click(function(){
			
		// Open Bike Cat Overlay
		$.blockUI({ centerX: true, centerY: true, message: $('#overlay_ped'), css: { width: '650px', height:'auto', overflow:'auto', top:'70', left:'11%', right:'75%'} }); 
		
		/* DISABLED DCT MVM 30.11.2009 Due to selection done on picture click.
		// Button "Choose"
		$('#choose_me_ped').click(function() { 
			
			// unblock overlay
			$.unblockUI();
			$('#preview').remove();
		});
		
		$('#cancel_me_ped').click(function() { 
			$.unblockUI();
			$('#preview').remove();
		});
		*/
		
		// Button "Auswahl l򳣨en"
		$('#unselect_me_ped').click(function() { 
			
			// Hide cat name
			$('#selected_ped_name').css({'display' : 'none'});
			// hide edit link
			$('#edit_ped_link').css({'display' : 'none'});
			// Shoh cat select img
			$('#drop_img_ped').css({'display' : 'block'});
			
			// delete hidden form values
			$('#chosen_ped_id').val();
			$('#chosen_ped_name').val();
			
			// set all img borders to standard
			$("img[class^='cat_peds_img']").each(function(){
				$('.cat_peds_img').css({'border' : '3px solid #ccc'});
			});
			
			$.unblockUI();
			$('#preview').remove();			
		});
		
		$('#x_me_ped').click(function() { 
			$.unblockUI();
			$('#preview').remove();
		});
	});	

	$('.cat_peds_img').click(function(){
		var chosen_full_id = this.id;
		var single_chosen_ids = chosen_full_id.split("_");
		var chosen_id = single_chosen_ids[1];
		
		$('#choose_me_ped').attr("disabled", false); 
		
		// set all img borders to standard
		$("img[class^='cat_peds_img']").each(function(){
			$('.cat_peds_img').css({'border' : '3px solid #ccc'});
		});
		
		// change img border for selected bike
		$('#'+chosen_full_id).css({'border' : '3px solid Green'});
		
		// Hide cat select img in parent window
		$('#drop_img_ped').css({'display' : 'none'});
		
		// Display cat name
		var ped_name = $(this).parent().attr('rel');
		$('#selected_ped_name').css({'display' : 'block'});
		$('#selected_ped_name').html(ped_name);
		
		// Display edit link
		$('#edit_ped_link').css({'display' : 'block'});
				
		// write cat_id and cat_name into hidden form field
		$('#chosen_ped_id').val(chosen_id);
		$('#chosen_ped_name').val(ped_name);
		$.unblockUI();
		$('#preview').remove();
	});
	
	// On Page Load
	init_number_of_bikes();
	fitForm();
	}
	
});