$(document).ready(function() {
	
	$('li:last-child').addClass('last_child');
	
		//searchbox
	jQuery("#googlesearch_input").keyup(function() {
		jQuery("div#cse td.gsc-input input").val(jQuery("#googlesearch_input").val());
	});
	
	
	function simpleTabs(){ 
		//Default Action
		$(this).find(".tab_content").hide(); //Hide all content
		$(this).find("ul.tabs li:first").addClass("active").show(); //Activate first tab
		$(this).find(".tab_content:first").show(); //Show first tab content	
		//On Click Event
		$("ul.tabs li").click(function() {
			$(this).parent().parent().find("ul.tabs li").removeClass("active"); //Remove any "active" class
			$(this).addClass("active"); //Add "active" class to selected tab
			$(this).parent().parent().find(".tab_content").hide(); //Hide all tab content
			var activeTab = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content
			$(activeTab).fadeIn(400); //Fade in the active content
			return false;
		});
	};//end function
	simpleTabs(); //Run function on any div with class name of "Simple Tabs"
	
	//Hide (Collapse) the toggle containers on load
	$(".toggle_container").hide();
	$("a.toggle").append("<div class='icon'>&nbsp;</div>");
		
	//Switch the "Open" and "Close" state per click then slide up/down (depending on open/close state)
	$("a.toggle").click(function(){
		$(this).toggleClass("active").next('.toggle_container').slideToggle(700);
		return false; //Prevent the browser jump to the link anchor
	});
	
	$('#sitemap_container').hide();
	$("a[href='#sitemap']").click(function() {
		$('#sitemap_container').slideToggle('fast');
  		$('html,body').animate({ scrollTop: $(document).height() }, 'slow');
  		return false;
	});

	// Events List Carousel
	
    $("#news_list").jCarouselLite({
        btnPrev: "#news_down",
        btnNext: "#news_up",
    	vertical: true,
    	speed:500,
    	auto:10000,
    	visible:3,
    	mouseWheel:true,
    	easing:"easeInOutSine"
    });
    
	// Events List Carousel initialisation
    
    var click=false;
    $("#events_tab").click(function(){
		if (!click){
	    	setTimeout ( events_list, 100 );
	    	click=true;
		}
	});
		
	function events_list(){
		if ($("#events_list li").size()>2){ // jCarousel needs minmum of 3 items to work correctly
			$("#events_list").jCarouselLite({
				btnPrev: "#news_down",
				btnNext: "#news_up",
				vertical: true,
				speed:500,
				auto:10000,
				visible:3,
				mouseWheel:true,
				easing:"easeInOutSine"
			});
		}
	}
	
	
	$("a.popup").click(function(){
		
		var modal_width = $(this).attr('width'); 
		var link_title = $(this).attr('title'); 
		var type = $(this).attr('href');
		var typeClean = ('wrapper_'+type.replace('#',''));
		
		$(type).dialog({
			width:modal_width,
			title:link_title,
			autoResize:true,
			draggable:true,
			resizable:false,
			modal:true,
			dialogClass:'custom_ui '+typeClean,
			close: function(event, ui) { $('#wrap').show(); },
       		open: function(event, ui) { $('.ui-widget-overlay').bind('click', function(){
       			$("#popup").dialog('close'); }); }			
		});
		
		$('.ui-widget-overlay').click(function() {
        //Close the dialog
        $(type).dialog("close");
      });  
		
		return false;
		
	});
	
	
	// Accordion 
	
	$('li .accordion_drawer').parent().addClass('openable');
	//Set default open/close settings
	$('li .accordion_drawer').hide(); //Hide/close all containers
	
	//On Click
	$('li.openable > a.accordion_activate').click(function(){
	
		if( $(this).parent().children('div.accordion_drawer').is(':hidden') ) {
			$(this).parent().siblings().removeClass('active').children('div.accordion_drawer').slideUp(); 
			$(this).parent().toggleClass('active').children('div.accordion_drawer').slideDown(); 
		}
		else {
			$(this).parent().removeClass('active').children('div.accordion_drawer').slideUp();
		}
		
		return true; //Prevent the browser jump to the link anchor
	});
	
	$('a.accordion_link').click(function(){
		var link_url = $(this).attr('href');
		window.location = link_url;
	});
	
	$().UItoTop({easingType: 'easeOutQuart'});
		
	// Prevent Top nav links from working
	$("#nav > ul > li:not(:first-child) > a").click(function(e) {
        e.preventDefault();
    });
    
    
    // Nav drawer
	function nothing(){}
	
   	$('#header #nav > ul li ul').css({"display":"block"}).hide(); 
    $('#header #nav > ul > li').not("#nav-home").hoverIntent(slideNavDown, nothing);
    $('#header #nav > ul').hoverIntent(nothing, slideNavUp);
    
    function slideNavDown(){
    	$("#header #nav > ul > li > ul").stop(true, true).slideDown(700, function(){ 
   			$('#header #nav > ul li ul').css({"display":"block"});  		
    	});
	}
    function slideNavUp(){
    	$("#header #nav > ul > li > ul").stop(true, true).slideUp(700);
	}
    
		
    
    // Directory Search
    

jQuery("#directory_listings ul[class!=a]").hide();

jQuery("#directory_listings h1[class!=a]").hide();

jQuery("div#directory_controls div#button_bar a").click(function(){

	jQuery("input#quickjump_title").val("Search List...");
	jQuery("div#directory_listings ul[class!="+this.innerHTML.toLowerCase()+"]").hide();
	jQuery("div#directory_listings h1[class!="+this.innerHTML.toLowerCase()+"]").hide();
	jQuery("div#directory_listings ul."+this.innerHTML.toLowerCase()+"").fadeIn();
	jQuery("div#directory_listings ul."+this.innerHTML.toLowerCase()+" li").fadeIn();
	
	jQuery("div#directory_listings h1."+this.innerHTML.toLowerCase()+"").fadeIn();
});

jQuery("input#quickjump_title").val("Search List...");
 
var currentul; 
jQuery("input#quickjump_title").keyup(function() {
	
				searchb = jQuery("input#quickjump_title").val();
				
				var regex = new RegExp(searchb, "gi");
				
				if (searchb == ''){
			
					 jQuery('div.content_frame li').each(function() {
							
							jQuery(this).show();
																																  
						});		
					
					 jQuery('div.directory h1').each(function() {
							
							jQuery(this).show();
																																  
						});			
																														  
				}
				else{
					jQuery("div#directory_listings ul").show();
					
					 jQuery('div.content_frame li a').each(function() {
					
						if (jQuery(this)[0].innerHTML.search(regex)!='-1'){
							console.log(jQuery(this).parent());
							jQuery(this).parent().fadeIn();
						}
						else{
							jQuery(this).parent().hide();
						}
					
					});
				
					//hide letter headings if children hidden
					jQuery('div#directory_listings ul').each(function() {
						currentul = jQuery(this)[0].className;
						if (currentul != '') { 
							 if(jQuery("div#directory_listings ul."+currentul+" li:visible").size() == 0){
								 jQuery("div#directory_listings h1."+currentul).hide();
							 }
							 else{
								 jQuery("div#directory_listings h1."+currentul).fadeIn();
							 }
						} 
					  
					});
					
				}
												 
});



});


$(window).load(function(){  
	
	//formstack
	
	jQuery('#popup_submit_event').html('<iframe style="width:680px;height:644px;" src="http://www.formstack.com/forms/?1073453-LJcbnLHf8K" title="UL.ie - Submit an Event"></iframe>');
	
	jQuery("a#submit_event").click(function(){
		jQuery('#popup_submit_event').html('<iframe style="width:680px;height:644px;" src="http://www.formstack.com/forms/?1073453-LJcbnLHf8K" title="UL.ie - Submit an Event"></iframe>');
	});
	
	jQuery('#popup_submit_advert').html('<iframe style="width:680px;height:644px;" src="http://www.formstack.com/forms/tricycle-ulie_submit_an_advert" title="UL.ie - Submit an Advert"></iframe>');
	
	jQuery("a#submit_advert").click(function(){
		jQuery('#popup_submit_advert').html('<iframe style="width:680px;height:644px;" src="http://www.formstack.com/forms/tricycle-ulie_submit_an_advert" title="UL.ie - Submit an Advert"></iframe>');
	});
	
	if($.browser.msie && parseInt($.browser.version) <= 7) {
		 $('#image_box').css('display','none');
		 $('#IE7_image_box').css('display','block');
		 
		 $('#IE7_image_box').cycle({
			fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
			speed:1000,
			timeout:10000,
			after:   onAfterCycle 
	});
		 
	}
	else{
		 $('#image_box').css('display','block');
		 $('#image_box').kenBurns({
			imageWidth:950,
			imageHeight:300
		 });
	}
});
var countslides = 0;
function onAfterCycle() { 

$("div#nav_thumbnails ul li a").removeClass("current");
$("div#nav_thumbnails ul li a.feature_thumb_"+(countslides+1)).addClass("current");
$("div#overlay_box div.info_box").removeClass("current");
$("div#overlay_box div.feature_info_"+(countslides+1)).addClass("current");

countslides = countslides + 1;
	if (countslides == jQuery('div#IE7_image_box img').size()){
		countslides = 0;
	}
} 	 
