addOnload(adminInit);
addOnload(newWinLinks);
addOnload(initialize);
//addOnload(expandInit);


//addOnload(filterInitDisplay);

function addOnload(newFunction) {
	var oldOnload = window.onload;
	
	if (typeof oldOnload == "function") {
		window.onload = function() {
			if (oldOnload) {
				oldOnload();
			}
			newFunction();
		}
	}
	else {
		window.onload = newFunction;
	} 
}


  $(document).ready(function() {
							 
	if( $("#adminStrip").length > 0) {
							 
		$('#admin-user').mouseover(function() {
		  
		  $('#user-links').show('fast'); 
		
		}).mouseleave(function(){
     
	 		 $('#user-links').hide('fast'); 
   		});
		
		

		$('#admin-gen').mouseover(function() {
		  
		  $('#gen-links').show('fast'); 
		
		}).mouseleave(function(){
     
	 		 $('#gen-links').hide('fast'); 
   		});
		
		

		$('#admin-store').mouseover(function() {
		  
		  $('#store-links').show('fast'); 
		
		}).mouseleave(function(){
     
	 		 $('#store-links').hide('fast'); 
   		});

	}


   });
  
   $(document).ready(function() {
							 
						 
		$('div.menu-toplevel').mouseover(function() {
												  		
		  $('div#'+this.id+'l').show('fast'); 
		  			  	
		
		}).mouseleave(function(){
     
	 		 $('div#'+this.id+'l').hide('fast'); 
   		});
		

   });


 //------------------------- Text editors ---------------------------------//
 
  $(document).ready(function() {
	
	   if( $("#input").length > 0) {
		    
        $("#input").cleditor();
	   }
      });
	   
	$(document).ready(function() {
		if($("#input2").length > 0) {
         $("#input2").cleditor();
		}
      });  
	  
  


//------------------------- Jumpmenu ---------------------------------//


$(document).ready(function() {
	
	$("select#area_sel").change(function() {
	
			var url = "../system_scripts/selectjump.php";
			
			$.post(url, { sel:$("#area_sel option:selected").val() }, function(xml) {
           		
				if(xml != "") {    
			  		$("#sections_sel").html(xml);    
			    	$("#sections_sel").attr("disabled", false);
					
					var url2 = "../system_scripts/selectjump2.php";

					
					$.post(url2, { sec:$("#sections_sel option:selected").val() }, function(xml2) {
																						   
					if(xml2 != "") {    
						 $("#subsection_sel").html(xml2);    
						 $("#subsection_sel").attr("disabled", false);
					
					} else {
						// format and output result
						$("#subsection_sel").html('<option value="0">No Subsections Availble</option>');   	
				 		$("#subsection_sel").attr("disabled", true);
					}
					
					});
					
				} else {
						// format and output result
					$("#sections_sel").html('<option value="0">No Sections Availble</option>');   	
 				 	$("#sections_sel").attr("disabled", true);
					$("#subsection_sel").html('<option value="0">No Subsections Availble</option>');   	
				 	$("#subsection_sel").attr("disabled", true);

				}
				
			});
  
    }); 
});


$(document).ready(function() {
    $("select#sections_sel").change(function() {
       
			var url = "../system_scripts/selectjump2.php";
			
			$.post(url, { sec:$("#sections_sel option:selected").val() }, function(xml) {
					
					if(xml != "") {    
						 $("#subsection_sel").html(xml);    
						 $("#subsection_sel").attr("disabled", false);
					
					} else {
						// format and output result
						$("#subsection_sel").html('<option value="0">No Subsections Availble</option>');   	
				 		$("#subsection_sel").attr("disabled", true);
					}
					
			});
  
    });
	
 
});


//------------------------- Text editors ---------------------------------//

	    
function slideSwitch() {
    var $active = $('#slideshow IMG.active');

    if ( $active.length == 0 ) $active = $('#slideshow IMG:last');

    var $next =  $active.next().length ? $active.next()
        : $('#slideshow IMG:first');

    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}

$(function() {
    setInterval( "slideSwitch()", 7000 );
});

function makeScrollable(wrapper, scrollable){
	// Get jQuery elements
	var wrapper = $(wrapper), scrollable = $(scrollable);
	
	// Hide images until they are not loaded
	scrollable.hide();
	var loading = $('<div class="loading">Loading...</div>').appendTo(wrapper);
	
	// Set function that will check if all images are loaded
	var interval = setInterval(function(){
		var images = scrollable.find('img');
		var completed = 0;
		
		// Counts number of images that are succesfully loaded
		images.each(function(){
			if (this.complete) completed++;	
		});
		
		if (completed == images.length){
			clearInterval(interval);
			// Timeout added to fix problem with Chrome
			setTimeout(function(){
				
				loading.hide();
				// Remove scrollbars	
				wrapper.css({overflow: 'hidden'});						
				
				scrollable.slideDown('slow', function(){
					enable();	
				});					
			}, 1000);	
		}
	}, 100);
	
	function enable(){
		// height of area at the top at bottom, that don't respond to mousemove
		var inactiveMargin = 99;					
		// Cache for performance
		var wrapperWidth = wrapper.width();
		var wrapperHeight = wrapper.height();
		// Using outer height to include padding too
		var scrollableHeight = scrollable.outerHeight() + 2*inactiveMargin;
		// Do not cache wrapperOffset, because it can change when user resizes window
		// We could use onresize event, but it's just not worth doing that 
		// var wrapperOffset = wrapper.offset();
		
		// Create a invisible tooltip
		var tooltip = $('<div class="sc_menu_tooltip"></div>')
			.css('opacity', 0)
			.appendTo(wrapper);
	
		// Save menu titles
		scrollable.find('a').each(function(){				
			$(this).data('tooltipText', this.title);				
		});
		
		// Remove default tooltip
		scrollable.find('a').removeAttr('title');		
		// Remove default tooltip in IE
		scrollable.find('img').removeAttr('alt');	
		
		var lastTarget;
		//When user move mouse over menu			
		wrapper.mousemove(function(e){
			// Save target
			lastTarget = e.target;

			var wrapperOffset = wrapper.offset();
		
			var tooltipLeft = e.pageX - wrapperOffset.left;
			// Do not let tooltip to move out of menu.
			// Because overflow is set to hidden, we will not be able too see it 
			tooltipLeft = Math.min(tooltipLeft, wrapperWidth - 75); //tooltip.outerWidth());
			
			var tooltipTop = e.pageY - wrapperOffset.top + wrapper.scrollTop() - 40;
			// Move tooltip under the mouse when we are in the higher part of the menu
			if (e.pageY - wrapperOffset.top < wrapperHeight/2){
				tooltipTop += 80;
			}				
			tooltip.css({top: tooltipTop, left: tooltipLeft});				
			
			// Scroll menu
			var top = (e.pageY -  wrapperOffset.top) * (scrollableHeight - wrapperHeight) / wrapperHeight - inactiveMargin;
			if (top < 0){
				top = 0;
			}			
			wrapper.scrollTop(top);
		});
		
		// Setting interval helps solving perfomance problems in IE
		var interval = setInterval(function(){
			if (!lastTarget) return;	
										
			var currentText = tooltip.text();
			
			if (lastTarget.nodeName == 'IMG'){					
				// We've attached data to a link, not image
				var newText = $(lastTarget).parent().data('tooltipText');

				// Show tooltip with the new text
				if (currentText != newText) {
					tooltip
						.stop(true)
						.css('opacity', 0)	
						.text(newText)
						.animate({opacity: 1}, 1000);
				}					
			}
		}, 200);
		
		// Hide tooltip when leaving menu
		wrapper.mouseleave(function(){
			lastTarget = false;
			tooltip.stop(true).css('opacity', 0).text('');
		});			
		
		/*
		//Usage of hover event resulted in performance problems
		scrollable.find('a').hover(function(){
			tooltip
				.stop()
				.css('opacity', 0)
				.text($(this).data('tooltipText'))
				.animate({opacity: 1}, 1000);
	
		}, function(){
			tooltip
				.stop()
				.animate({opacity: 0}, 300);
		});
		*/			
	}
}
	
$(function(){	
	makeScrollable("div.sc_menu_wrapper", "div.sc_menu");
});

/*expand
function expandInit() {
	
	doexpand(document.getElementById("expander"));
	
}

function doexpand(thisLink) {
	thisLink.onmouseover = expand;	
	thisLink.onmouseout = collapse;
}

function collapse() {
	document.getElementById("expander").style.width = "135px";
	
}
function expand() {
	
	document.getElementById("expander").style.width = "500px";
	

} */




// admin div 

function adminInit() {
	for (var i=0; i<document.images.length; i++) {
		if (document.images[i].parentNode.className == "showDiv") {
			openadmin(document.images[i]);
		} else {}
	}
}

function openadmin(thisLink) {
	thisLink.onclick = showadmin;

}

function showadmin() {
	var loc = this.name;
	$("#"+loc).slideDown("slow");
	/*document.getElementById(loc).style.display = "block";*/

	return false;
}


// media changer

$(document).ready(function() {

	var loc = 0;
		
		for (var i=0; i<document.images.length; i++) {
			if (document.images[i].parentNode.className == "mediaItem") {
				setupmediadisplay(document.images[i]);
					
			}
		}
	
	
	function setupmediadisplay(thisLink) {
		
		thisLink.onclick = showMedia;	
		
	}	
	
	function showMedia() {
		var loc = this.id+"B";
		
		if(loc == "mediaVidHolderB" || loc == "mediaMapHolderB") { var h = 371; } else { var h = $("#img1B").height(); }
		
		
		if($.browser.mozilla) {
			
			if(loc == "mediaImgHolderB" || loc == "mediaMapHolderB") { $("#mediaVidHolderB").css('display', 'none'); } else { $("#mediaVidHolderB").css('display', 'block'); }
		
		} else {}
	
		for (var i=0; i< $('div').length; i++) {
		if (document.getElementsByTagName('div')[i].className == "mediaImgHolder") {
				document.getElementsByTagName('div')[i].style.visibility = "hidden";	
					
			}
		}
		
		for (var i=0; i<document.images.length; i++) {
		if (document.images[i].parentNode.className == "mediaImg") {
				document.images[i].parentNode.style.visibility = "hidden";
					
			}
		}
		
			document.getElementById(loc).style.visibility = "visible";
		
		
			//document.getElementById("outer").style.height = h+48;
			//$("#outer").height(h+48);
			
			$("#outer").animate({ height: h+48 }, "slow");

			
					
		
		if(loc == "mediaImgHolderB") {
			document.getElementById("showload").style.visibility = "visible";
			document.getElementById("showload").style.height = h;
			
			
		} else {
			document.getElementById("showload").style.visibility = "hidden";
		}
		
		
		return false;
	
	}


});

// new image changer 

$(document).ready(function() {
						   
	var loc = 0;
		
		
		for (var i=0; i<document.images.length; i++) {
			if (document.images[i].parentNode.className == "imgChange") {
				setupRolloverDisplay(document.images[i]);
			}
		}
	
		
		function setupRolloverDisplay(thisLink) {
			
			thisLink.onclick = rollOverDisplay;	
			
		}			
		
		
		function rollOverDisplay() {
			
			var loc = this.id+"B";
			
			var img = document.getElementById(loc); 
			var width = img.clientWidth;
			var h = img.clientHeight;
		
		
		for (var i=0; i<document.images.length; i++) {
		if (document.images[i].parentNode.className == "mediaImg") {
				document.images[i].parentNode.style.visibility = "hidden";
				
				
					
			}
		}
	
			document.getElementById(loc).parentNode.style.visibility = "visible";
		
			$("#showload").height(h);
			$("#mediaImgHolderB").height(h);
			
			$("#outer").animate({ height: h+48 }, "slow");
			
		return false;
	
		}
	
});






//media browser
function newWinLinks() {
	for(var i=0; i<document.links.length; i++) {
		if(document.links[i].parentNode.className == "browser") {
			openBrowser(document.links[i]);
		}
	}	
}

function openBrowser(thisLink) {
		thisLink.onclick = openWindow;
}


function openWindow() {
	var loc = this.title;
	var fol = this.id;
	var mediaWin = window.open("/filebrowser/filebrowser.php?loc="+loc+"&fol="+fol+"", "File Browser", "toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=440,height=600");
	return false;
}

  function initialize() {
	  if(!document.getElementById('lat')) {} else {
    var latlng = new google.maps.LatLng(document.getElementById("lat").title, document.getElementById("long").title);
    var myOptions = {
      zoom: 15,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.HYBRID
    };
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
	
	var marker = new google.maps.Marker({ position: latlng });
  
  // To add the marker to the map, call setMap();
  marker.setMap(map);  
	  }
  } 
  
  //------------------------- String Match ---------------------------------//

$(document).ready(function() {
	$.ajax({
		url: "/system_scripts/word_search.php",
		success: function(html){
		

				var words = html.split(",");	
				
				for(var i=0; i<words.length; i++) {
							
					
					$("*:contains('"+words[i]+"')").filter(".storeMultiHolder p").each(function() {
						
							
						 	$(this).html( 
							  	$(this).html().replace(words[i], "<a href='/content/search.php?words=" + words[i] + "' class='search_link'>" + words[i] + "</a>" )  
							  ) 
						
					});
					
			} 
		}
	});

});


