$(function(){

	var currurl = document.URL;
	if ((currurl.indexOf('worship') >= 0) || (currurl.indexOf('residential') >= 0) || (currurl.indexOf('public') >= 0)) {
		$('.projects_menu').slideDown();
	}
	
	function locus_resize() {
		var w = $(window).width();
		if((w <= 1120) && (w >= 1040)) {
			$('#container').css('margin-left',w - 1020);
		} else if (w < 1040) {
			$('#container').css('margin-left',20);
		} else if (w > 1120) {
			$('#container').css('margin-left',100);
		}
		
		var h = $(window).height();
		if (h < 620) {
			$('#bot_left').css('position','absolute').css('top','460px');
		} else {
			$('#bot_left').css('position','fixed').css('top','');
		}
	}
	locus_resize();
	

	// Browser
	if ( $.browser.msie ) {
	
		$('#raw_block').css('position','absolute').css('bottom','0').css('right','0').css('margin-right','0');
		
	}

	
	window.onresize = function() {
		locus_resize();
	}
	// $('#sidebar').css('top','85px');
	
	// Menu functions
	
	  // Set 1st item open
	  //$('#nav > ul > li:first-child > ul.sub-menu').slideDown();
	
	$('#nav ul li a').click(function(){
		
		var submenu = $(this).parent('li').children('ul.sub-menu');
		
		if (submenu.size() > 0 && submenu.is(':hidden')) {
			
			submenu.slideDown(); 
			//return false;
			
			// To close other menu items when one opens:
			// $(this).parent('li').siblings().children('ul.sub-menu').slideUp();
			
		} /*else if (submenu.size() > 0 && submenu.is(':visible')) {
			
			if(!this.hasClass('project')) {
			submenu.slideUp();
			//return false;
			}
		} */
		
		
	});
	
	
	// Home page img Rollovers
	$('#main-image div.homeimg').hover(function(){
		$(this).children('.bg, .content').fadeIn(900).clearQueue();//animate({height:'100%'});
		
	}, function(){
		$(this).children('.bg, .content').fadeOut().clearQueue();//animate({height:'0'});
	});
	
	var i = 1;
	$('#home_content div.ngg-galleryoverview').each(function(){
		//alert($(this).html());
		$(this).find('a.gallery').addClass('gallery'+i);
		//$('a.gallery'+i).fancybox();
		i++;
	})
	
	$('#nav > ul > li, #menu-primary > li, #menu-primary > li > ul > li').each(function(){
		var item = $(this).children('a').text();
		var item_low = item.toLowerCase();
		var url = window.location.href;
		var contains = url.search(item_low);
		if (contains != -1) {
			$(this).children('ul.sub-menu').show();
		} 
	});
	
	
});

$(window).load(function(){
	var i = 1;
	$('#home_content div.ngg-galleryoverview').each(function(){
		//alert($(this).html());
		var h = 1;
		$(this).find('a.gallery').each(function(){
			$(this).attr('rel','gallery'+i);
			if (h == 1) { $(this).attr('id','first'+i) }
			h++;
		});
		//$('a.gallery'+i).fancybox();
		i++;
	})
	$('a.gallery').fancybox({
		'titleFormat' : function(title, currentArray, currentIndex, currentOpts, content) { 
	    	return '<div id="fancybox-title-over">' +  (currentIndex + 1) + ' / ' + currentArray.length + ' <span class="caption">' + title+ '</span></div>';
		}
	});
});

// Ajaxify Wordpress

jQuery(document).ready(function($) {
    var $mainContent = $("#main"),
        siteUrl = "http://" + top.location.host.toString(),
        url = ''; 

	if ( $.browser.msie ) { }
	else {

	    $(document).delegate("a.project[href^='"+siteUrl+"']:not([href*=/wp-admin/]):not([href*=/wp-login.php]):not([href$=/feed/])", "click", function() {
	        location.hash = this.pathname;
	        return false;
	    }); 
	}

    $("#searchform").submit(function(e) {
        location.hash = '?s=' + $("#s").val();
        e.preventDefault();
    }); 

    $(window).bind('hashchange', function(){
        url = window.location.hash.substring(1); 

        if (!url) {
            return;
        } 

        url = url + " #content"; 
		
		//loadobjs('http://www.locusarchitecture.com/wp-content/plugins/nextgen-gallery/js/jquery.cycle.all.min.js?ver=2.88', 'http://www.locusarchitecture.com/wp-content/plugins/nextgen-gallery/js/ngg.slideshow.min.js?ver=1.05');
		
		/* $.getScript('http://www.locusarchitecture.com/wp-content/plugins/nextgen-gallery/js/jquery.cycle.all.min.js?ver=2.88', function(data, textStatus){
		   console.log(data); //data returned
		   console.log(textStatus); //success
		   console.log('Load was performed.');
		});
		$.getScript('http://www.locusarchitecture.com/wp-content/plugins/nextgen-gallery/js/ngg.slideshow.min.js?ver=1.05', function(data, textStatus){
		   console.log(data); //data returned
		   console.log(textStatus); //success
		   console.log('Load was performed.');
		}); */
		
        $mainContent.animate({opacity: "0.1"}).html('<p>Please wait...</>').load(url, function() {
            $mainContent.animate({opacity: "1"});
			if(url.indexOf('worship') >= 0) {
				jQuery(document).ready(function(){ 
				jQuery("#ngg-slideshow-23-3094-1").nggSlideshow( {id: 23,fx:"fade",width:805,height:400,domain: "http://www.locusarchitecture.com/",timeout:7000});
				});
			} else if (url.indexOf('residential') >= 0) {
				jQuery(document).ready(function(){ 
				jQuery("#ngg-slideshow-21-3098-1").nggSlideshow( {id: 21,fx:"fade",width:805,height:400,domain: "http://www.locusarchitecture.com/",timeout:7000});
				});
			} else if (url.indexOf('public') >= 0) {
				jQuery(document).ready(function(){ 
				jQuery("#ngg-slideshow-22-3101-1").nggSlideshow( {id: 22,fx:"fade",width:805,height:400,domain: "http://www.locusarchitecture.com/",timeout:7000});
				});
			}
        });
    });

    $(window).trigger('hashchange');
});

