// JavaScript Document

var $j = jQuery.noConflict();


$j(document).ready(function(){

	
	
	
	
	
	
	
	
	
	
	
	//// //// //// //// //// /// //// //// //// //// /// //// //// //// //// /// //// //// //// //// /// //// //// //// //// 
	// cookie stuff
	// dragging
	
	
	function numKeys(obj) {
		var count = 0;
		for(var prop in obj)
		{
			count++;
		}
		return count;
	}
	
	
	
	
	
	//console.log( $j.cookie('Lichtung_settings_positions') );
	
	




	//$j('#resetpict').hide();

	$j('#resetpict').click(
			function () {
				//alert(1);
				$j.cookie('Lichtung_settings_positions', '');
				reset_boxpositions();
			}
	);
	


	if ( $j.cookie("Lichtung_settings_positions") ) {
		
				$j('#resetpict').show();
				
				var cookie_items = $j.cookie("Lichtung_settings_positions");
				var literal = $j.evalJSON(cookie_items);
				
				
				//console.log( 'cookie_items' );
				//console.log( literal );
				
				
				//console.log( numKeys(literal) );
		
				//console.log( cookie_items_lit[IDname] );
				
	
				$j(".moveable").each(function() {
						
						var IDname = $j(this).attr("ID");
					//	var itemstyle = literal[IDname] 
						var items_pos = literal[IDname] 
						//$j(this).attr("style", itemstyle);
						if (items_pos) {
							$j(this).css("top",items_pos.top+'px');
							$j(this).css("left",items_pos.left+'px');
						}
				})
	
	} else {
		var literal = {};
	}
	
	
	
	function reset_boxpositions() {
		
				$j('#resetpict').hide();
		
				$j(".moveable").each(function() {
						
						//$j(this).removeAttr("style");
						
						$j(this).css("top","");
						$j(this).css("left","");
						
						//$j(this).attr("style", itemstyle);
					 
				})
	}
	
	
	
	$j(".moveable a img").mousedown(function(){
		return false;
	});
	
	$j(".moveable").draggable({
						   
		revert: true,	
		//helper: '' ,
		//zIndex: 2700,
		stack: '.moveable',
		cursor: "move",
		cancel: 'a' ,
		
		// Find original position of dragged image.
		start: function(event, ui) {
			
			
			},
	
		// Find position where image is dropped.
		stop: function(event, ui) { 
			document.getElementById("targetdiv").style.display = "none";
			
			
			var IDname = $j(this).attr("ID");
			var IDstyle = $j(this).attr("style");
			var IDpos = $j(this).position();
			
			//if (!literal) var literal = {};
			literal[IDname] = IDpos;
			
			//console.log( 'literal' );
			//console.log( literal );
	
						
			$j.cookie("Lichtung_settings_positions", $j.toJSON(literal)  );
			
			$j('#resetpict').show();
			
			
			
			
			//alert(cookie_items2['box_image_96']);
		},
		
		 drag: function(event, ui) {
			 
			//$(this).data('draggable').offset.click.top -= x; 
			 
			var grid = 202;
			grid = grid + 10; 
			
			
			//var off_top = $j(this).data('draggable').offset.click.top;
			
			ui.originalPosition.left 	= Math.floor(ui.position.left 	/ grid) * grid; 
			ui.originalPosition.top 	= Math.floor(ui.position.top 	/ grid) * grid; 
			
			
			
			var cont_w = $j(this).width();
			var cont_h = $j(this).height();
			
			var border = 1;
			
			
			if (document.getElementById("targetdiv")) {
				var targetDiv = document.getElementById("targetdiv");
				document.getElementById("targetdiv").style.display = "block";
			} else {
				var targetDiv = document.createElement("div");
				targetDiv.id = "targetdiv";
				
				//document.getElementsByTagName("body")[0].appendChild(targetDiv)
				document.getElementById("boxconbtainer").appendChild(targetDiv);
			}
			
			
			targetDiv.style.position = "absolute";
			targetDiv.style.width 	= cont_w - (border*2)  +"px";
			targetDiv.style.height 	= cont_h - (border*2)  +"px";
			
			targetDiv.style.left 	= Math.floor(ui.position.left / grid) * grid  + "px";
			targetDiv.style.top 	= Math.floor(ui.position.top  / grid) * grid  + "px";
			
	
			 
			}
	});
	
	
	$j('.moveable').draggable({ cancel: 'a' });
	
	$j('.moveable').draggable({ cancel: 'textarea' });
	

	//$j('.moveable').draggable({ cancel: 'form' });
	

	
	$j('.moveable').hover(function() {
	 $j(this).css('cursor','move');
	 }, function() {
	 $j(this).css('cursor','auto');
	});
	
	
	
	
	/*$j('.moveable a').mouseenter(
			function () {
				$j(this).parent().draggable( { disabled: true });
			})
	
	$j('.moveable a').mouseleave(
			function () {
				$j('.moveable').draggable( "option", "disabled", false );
			})
	*/
	
	
	
	
	
	
    //// //// //// //// //// /// //// //// //// //// /// //// //// //// //// /// //// //// //// //// /// //// //// //// //// 
	// newsbox
	
	
	$j('.newsbox ul li').each(function() {
											  
											  
	  	$j(this).hover(
			function () {
				$j(this).addClass("lihover");
				
		  	},
		  	function () {
				$j(this).removeClass("lihover");
		  	}			   
					   
		);
		
		
		
		$j(this).click(
			function () {
				
				
				if(	$j(this).hasClass('newsactive') ) {
					
					hide_all_newsboxes();
			
				} else {
				
					var IDname = $j(this).attr("ID");
					var IDNR = IDname.replace("newsbutton-", "");
					
					
					// hide all
					hide_all_newsboxes();
					
					//show boxes
					/*$j('#newstext-'+IDNR).removeClass("boxhidden");
					$j('#newsimage-'+IDNR).removeClass("boxhidden");
					*/
					
					$j('#newstext-'+IDNR).fadeIn('slow', function() {
						// Animation complete
						$j('#newstext-'+IDNR).removeClass("boxhidden");
					});
					
					$j('#newsimage-'+IDNR).fadeIn('slow', function() {
						// Animation complete
						$j('#newsimage-'+IDNR).removeClass("boxhidden");
					});
					
					//alert(IDNR);
					
					$j(this).addClass("newsactive");
				
				}

		  	}		   
					   
		);
		
		
		
	})


	function hide_all_newsboxes() {
		
		/*$j('.newsboximage').each(function() {
	 	 	$j(this).addClass("boxhidden");
			$j(this).hide();
		})
		
		$j('.newsboxtext').each(function() {
	 	 	$j(this).addClass("boxhidden");
			$j(this).hide();
		})*/
		
		
		$j('.newsboximage').fadeOut('slow', function() {
						// Animation complete
						$j('.newsboximage').addClass("boxhidden");
		});
		$j('.newsboxtext').fadeOut('slow', function() {
						// Animation complete
						$j('.newsboxtext').addClass("boxhidden");
		});
		
		$j('.newsbox ul li').each(function() {
	 	 	$j(this).removeClass("newsactive");
		})
		
	}
	
	

	if ($j('#ma_easy_contact_msg').hasClass('ma_easy_contact_msg_ok')) {
		$j('#kontaktformbox').removeClass("boxhidden");
	}
	
	
	
	$j('#MAeasyformbutton').click(
			function () {
				//alert(1);
				//kontaktformbox
				
				if(	$j('#kontaktformbox').hasClass('boxhidden') ) {
				
					$j('#kontaktformbox').fadeIn('slow', function() {
						// Animation complete
						$j('#kontaktformbox').removeClass("boxhidden");
					});
				
				} else {
					
					$j('#kontaktformbox').fadeOut('slow', function() {
						// Animation complete
						$j('#kontaktformbox').addClass("boxhidden");
					});
				}
				
	})
			
			
	

});
