/* this file contains functions for the myswitzerland map */
function docReady() {

	var PANORAMIO_ELEMENT_ID = 'category_06__panoramio_photos';
	
	/*-----------------------------------------------
	toggleable element
		initialise - get class='toggleable'
					 add switch element into h3 tag
					 wrap content with class='toggle_content' and hide
		             
		open - defaul is open
	----------------------------------------------*/
	$( '.toggleable' ).each(function() 
	{
		$( this ).children( 'h3' ).append( '<span class="icon_switch"></span>' ).next().wrap( '<div class="toggle_content" />' );
		
		if($( this ).hasClass( 'open' )== false) 
		{
			$( this ).addClass( 'closed' ).children( 'h3' ).next().slideUp();
		} 
		else 
		{
			$( this ).removeClass( 'closed' ).children( 'h3' ).next().slideDown();
		}
	});

	 
	/*-----------------------------------------------
	show hide content element 
		open   - show content
		closed - hide content
	----------------------------------------------*/
	
		/*-----------------------------------------------
		 nested toggle Elements
		----------------------------------------------*/

	if($( '.single_open' ).length > 0) {
		

		var hasParent = false;
		var thisGroup = null;

		var allTogglers = $('.single_open .toggleable');
		
		
		$( '.toggleable h3' ).click(function() 
		{

			/* this hasParent */
			if($( this ).parents('.toggleable').length > 1) {
				hasParent = true;
				thisGroup = $('.toggleable .toggleable');
				
			} else {
				hasParent= false;
				thisGroup =  $('.toggleable').not('.toggleable .toggleable');

			}
			
			
			var toggleTriger = $( this ).parent();
			var toggleContent  = $( this ).next();
			var childToggler = $( this ).next().find('.toggleable');

			function closeChildren(pchildToggler) {

			pchildToggler.each(function(){
					var curToggler = $( this );
					$( this ).find('h3').next().slideUp('slow', function () 
					{
						curToggler.removeClass( 'open' ).addClass( 'closed' );

					});

				});

			}

			if(toggleTriger.hasClass('open')) 
			{
				if(childToggler.length > 0 ){

					closeChildren(childToggler);
				}

				thisGroup.each(function(){
 					
					var curToggler = $( this );

					$( this ).find('h3').next().slideUp('slow', function () 
					{
						curToggler.removeClass( 'open' ).addClass( 'closed' );

					});
				});
			}
			else 
			{
				
				thisGroup.each(function () {
										 
					if(this != toggleTriger) 
					{
						$( this ).removeClass( 'open' ).addClass( 'closed' );
						$( 'h3', this ).next().slideUp('slow');
						if($( this ).find('.toggleable').length > 0) 
						{
							var childToggler = $( this ).find('.toggleable');
 							closeChildren( childToggler );
						}
					} else {
						
					
					}
				});
				
				toggleContent.slideDown('slow', function () 
				{
					toggleTriger.removeClass( 'closed' ).addClass( 'open' );
				});


			}
		});
	}
	else 
	{
		
		/*-----------------------------------------------
		 default toggle Elements
		----------------------------------------------*/

		$( '.toggleable h3' ).click(function() 
		{	
			var toggleTriger = $( this ).parent();
			var toggleContent  = $( this ).next();
			
			
			if(toggleTriger.hasClass('open')) 
			{
				toggleContent.slideUp('slow', function () 
				{
					toggleTriger.removeClass( 'open' ).addClass( 'closed' );
				});
			}
			else 
			{
				toggleContent.slideDown('slow', function () 
				{
					toggleTriger.removeClass( 'closed' ).addClass( 'open' );
				});
			}
	
		});
	}

	/*-----------------------------------------------
	replace link to input tag 
		rel   - checkbox or radio
		id  - id & label for
		title - name
		text - label text, value  
	----------------------------------------------*/

	$( '.link_to_input a' ).each(function () 
	{
	   var checkType = ['radio','checkbox'];
	   
		if(jQuery.inArray($( this ).attr( 'rel' ), checkType) > -1){
			
			var labelText = $( this ).text();
			var value = $( this ).attr( 'title' ).length < 0 ? labelText : $( this ).attr( 'title' );
			
			var html = '<input type="' + $( this ).attr( 'rel' ) +
				'" id="' + $( this ).attr( 'id' ) +
				// 25/3/2011 Pablo Vidal. Modified to give a name to the radio buttons
				'" name="' + ($( this ).attr('title')||$( this ).text()) +
				'" value="' + $( this ).attr('id').replace('finder-','')  + 
				'" channelid="' + $( this ).attr('channelid') + 
				'"/><label for="' +$( this ).attr( 'id' ) + '">' + 
				$( this ).html() + '</label>';
						
			 $( this ).replaceWith(html);
		}
	});
		
	// style for checked label
	
	$( 'input:checkbox' ).each(function () 
	{
		// in case refresh or set default value										
		$( this ).attr( 'checked' ) == true ? $( this ).next().addClass( 'checked' ) : $( this ).next().removeClass( 'checked' );
			 
		$( this ).click(function () 
		{ 
			$( this ).attr( 'checked' ) == true ? $( this ).next().addClass( 'checked' ) : $( this ).next().removeClass( 'checked' );
		});
	});

	$( 'input:radio' ).each(function () 
	{
		$( this ).attr( 'checked' ) == true ? $( this ).next().addClass( 'checked' ) : $( this ).next().removeClass( 'checked' );
			 
		$( this ).click(function () 
		{ 
			var radioGroup = $( 'input[name="' + $( this ).attr( 'name' ) + '"]' );
			radioGroup.each(function () 
			{
				 $( this ).attr( 'checked' ) == true ? $( this ).next().addClass( 'checked' ) : $( this ).next().removeClass( 'checked' );
			});
		});
	});
	
		
	/*-----------------------------------------------
	checkbox tree element
		initialise    - hide sublevel elment if not checked
		open sublevel - by check parent checkbox
		
		clear all -    remove all check
	----------------------------------------------*/
	
	$( '.checkbox_tree > * > input' ).each(function() {
	
		if($( this ).attr( 'checked' )) 
		{
			 $( this ).nextAll( 'ul' ).show();
		} 
		else 
		{
			$( this ).nextAll( 'ul' ).hide();
		}
	});

	// 15/04/2011 - Bjoern Sellnau ---
	var allCheckboxItemInput = [];
	// get a list with all first childs of the ul.checkbox_tree
	$allCheckboxItem = $('ul.checkbox_tree').map(function(i,el){var uls = $(el).children(); if(uls.length>1) return uls[0];});
	
	// loop through this list and get all input childs
	$allCheckboxItem.each(function(idx,ele){
		//console.log(idx,ele,$(this).children('input'));
		allCheckboxItemInput.push($(this).children('input')[0]);
	})
	
	//add click event to all inputs in checkbox_tree
	$('.checkbox_tree input').click(function(e) {

		if($(this).nextAll('ul').length > 0) {
			if($(this).attr('checked')) {
				 $(this).nextAll('ul').show().children().children('input').attr('checked', 'checked');
			} else {
				// 3/3/2011 Pablo Vidal. Modified for unchecking hidden checkboxes && 
				$(this).nextAll('ul').hide().find('input:checked').each(function(i,el){
						$(el).removeAttr('checked');
				});
			}
		}

		//get current first element normally this is the 'all' button
		var $parentInput = $(this).parents('ul.checkbox_tree').children(':first').find('input');//$('ul.checkbox_tree li:first input');
//		if(window.console){window.console.log('$parentInput',$parentInput)};
//		if(window.console){window.console.log($(this)[0])};
//		if(window.console){window.console.log(allCheckboxItemInput)};

		//check if the current element is not a first-child of an ul.checkbox_tree
		if($.inArray($(this)[0],allCheckboxItemInput) == -1){

			//check if the current parent element is a first-child of an ul.checkbox_tree
			if($.inArray($parentInput[0],allCheckboxItemInput) !== -1){
				//loop through all elements and deactivate them if the parent element is checked
				if($parentInput.attr('checked') === true){
					$parentInput.parent().nextAll().each(function(idx,ele){
						var $childs = $(this).children().filter('ul');
						if($childs.length){
							$childs.children().filter('li').each(function(idx,ele){
							//	var eleID = $(this).children(':eq(0)')[0];
							//	submitMapFinder(eleID, $(this).children(':eq(0)').val());
							if($(this).children(':eq(0)').attr('id') !== PANORAMIO_ELEMENT_ID){	
								hidePois($(this).children(':eq(0)').val(), $(this).children(':eq(0)').attr('channelid'));
							} else {
								_map.removeOverlay(_pano);
							}
							})
						}	else {
			//				var eleID = $(this).children(':eq(0)')[0];
				//			submitMapFinder(eleID, $(this).children(':eq(0)').val());
							if($(this).children(':eq(0)').attr('id') !== PANORAMIO_ELEMENT_ID){	
								hidePois($(this).children(':eq(0)').val(), $(this).children(':eq(0)').attr('channelid'));
							} else {
								_map.removeOverlay(_pano);
							}
						}
					});
				}
				//uncheck the current parent element
				$parentInput.attr('checked', false)
			}	
	
			//if this is the last element collapse ul
			var $checkedItems = $(this).parents('ul').filter(':not(ul.checkbox_tree)'),
					$checkedParent = "";
	
			if($checkedItems.length){
				$checkedParent = $checkedItems;
				$checkedItems = $checkedItems.children().find('input:checked');
				if($checkedItems.length === 0){
					//$checkedItems.removeAttr('checked');
					$checkedParent.css('display','none').prev().removeClass('checked')
						.prev().removeAttr('checked');
				}
			}

			var inputID = $(this)[0];
			var iconID = $(this).val();
			
			if($(this).parent().parent().hasClass("checkbox_tree")){
				//this runs if this element is a neighbor of the first element and gets all child inputs. if there is not a child it returns the element itself like typically swiss hotels
				var $poiItems	= $(this).nextAll('ul').children().find('input');
				if($poiItems.length === 0){
					$poiItems	= $(this);
				}
			} else {
				//this runs if this element is not a neighbor of the first element and gets all child inputs. if there is not a child it returns the element itself like typically swiss hotels
				var $poiItems = $(this).parents('ul').filter(':not(ul.checkbox_tree)').children().find('input');
			}
					
			//loop through elements an show them on the map
			$poiItems.each(function(idx,element){
			var eleID = $(this)[0];
			if($(this).attr('id') !== PANORAMIO_ELEMENT_ID){	
				submitMapFinder(eleID,$(this).val());
			} else {
				showPanoramio(eleID);
			}
		});
	}	
});

	// deselect all selected finder nodes
	$allCheckboxItem.each(function(idx,ele){
		$(this).click(function() {
		var current_checkstate = $('input', $(this)).attr('checked');
		
		//var inputs = $('#selector').find('input:checkbox');
		var inputs = $(this).parents('.checkbox_tree').find('input:checkbox');

		//var labels = $('#selector').find('label');
		var labels = $(this).parents('.checkbox_tree').find('label');
		
		inputs.attr('checked', false);
		labels.removeClass('checked' );

		//var subLevelUl = $('#selector').find('ul ul');
		var subLevelUl = $(this).parents('.checkbox_tree').find('ul');
		if(subLevelUl.length > 0) {	subLevelUl.hide(); }
		
		//set checked state
		if(current_checkstate === false)
		$('input', $(this)).attr('checked', false);
		else
		$('input', $(this)).attr('checked', true);
		
		//show data
		$(this).nextAll().each(function(idx,ele){
			var $childs = $(this).children().filter('ul');
			if($childs.length){
				$childs.children().filter('li').each(function(idx,ele){
				//	var eleID = $(this).children(':eq(0)')[0];
				//	submitMapFinder(eleID, $(this).children(':eq(0)').val());

					if(current_checkstate === false){
						if($(this).children(':eq(0)').attr('id') !== PANORAMIO_ELEMENT_ID){	
							hidePois($(this).children(':eq(0)').val(), $(this).children(':eq(0)').attr('channelid'));
						} else {
							_map.removeOverlay(_pano);
						}
					} else {
						if($(this).children(':eq(0)').attr('id') !== PANORAMIO_ELEMENT_ID){	
							showPois($(this).children(':eq(0)').val(), $(this).children(':eq(0)').attr('channelid'));
						} else {
							_map.addOverlay(_pano);
						}						
					}
				})
				
			}	else {
//				var eleID = $(this).children(':eq(0)')[0];
	//			submitMapFinder(eleID, $(this).children(':eq(0)').val());
					if(current_checkstate === false){
						if($(this).children(':eq(0)').attr('id') !== PANORAMIO_ELEMENT_ID){	
							hidePois($(this).children(':eq(0)').val(), $(this).children(':eq(0)').attr('channelid'));
						} else {
							_map.removeOverlay(_pano);
						}
					} else {
						if($(this).children(':eq(0)').attr('id') !== PANORAMIO_ELEMENT_ID){	
							showPois($(this).children(':eq(0)').val(), $(this).children(':eq(0)').attr('channelid'));
						} else {
							_map.addOverlay(_pano);
						}						
					}
			}
			
		});
		// 3/3/2011 Pablo Vidal. Modified for sending the request on time for the "all" checkbox
		//submitFinder();
	});
	
	})
// --- Bjoern Sellnau

	/*-----------------------------------------------
	Remove input field default value or 
		if empty set default value
		by checking  ( value = title )
		
		** preparation **
		<input value='default_text' value='default_text' class='set_value'>
	----------------------------------------------*/

	$( '.set_value' ).focus(function() 
	{	
		var defaultValue = $( this ).attr( 'title' );
		
		if($( this ).val() == defaultValue) 
		{
			$( this ).val( '' );
		}
		$( this ).addClass( 'onfocus' );
	});
	
	$( '.set_value' ).blur(function() 
	{	
		var defaultValue = $( this ).attr( 'title' );
		if($( this ).val() == '' ) 
		{
			$( this ).val(defaultValue);
			$( this ).removeClass( 'onfocus' );
		}
	});

//will be kicked later, this is just for debugging.	
function checkPoiItem(cb, item) {
//if(window.console){window.console.log('item: '+item)};	

if(cb == null)
return;
var channelid = cb.getAttribute('channelid');
//if(window.console){window.console.log('channel',channelid)};	

if(cb.checked == true){
//if(window.console){window.console.log('modus: show')};	
showPois(item, channelid);
} else {
//if(window.console){window.console.log('modus: hide')};	
hidePois(item, channelid);
}

} 	

	
	function submitMapFinder(inputID,iconID) {
		checkPoiItem(inputID, iconID);
	}

	//this shows the panoramio layer.
	function showPanoramio(cb) 
	{
		
		if(cb.checked == true){
//if(window.console){window.console.log('pano-modus: add')};	
			_map.addOverlay(_pano);
		} else {
//if(window.console){window.console.log('pano-modus: remove')};	
			_map.removeOverlay(_pano);
		}
	}
	
}
$(document).ready(docReady);
