$(document).ready(function(){

	

	// preload images
	$.preloadCssImages();
	
	
	if ( $("#homeGallery").length>0 ) {
		$("#homeGallery").quesksgallery({
			transition: 'fade',
			interval: 6, 
			navHolder : 'ul.navCarousel'
		});
	}

	if ( $(".section .photo").length>0 ) {		
		$(".section .photo").each(function(){
			id = '#' + $(this).attr('id');
			if ( $(id + ' li').length>1 ) {
				$(id).quesksgallery({
					transition: 'fade',
					interval: 3,
					showArrow: true
				});
				
			}
		});
	}

	
	// adjust width of pull down menu
	$("#nav ul ul").each(function(){
		if ( !$.browser.msie ) {
			$(this).css({
				 '-moz-box-shadow': '0 6px 16px #404040',
				 '-webkit-box-shadow': '0 6px 16px #404040',
				 'box-shadow': '0 6px 16px #404040'
			});
		}
	});

	// nav pull down 
	$("#nav li").hover(
		function(){
			var subnav = $(this).children().eq(1);
			if (subnav.length>0) {		
				$(this).children().eq(0).addClass('navActive');
				subnav
					.show()
					.css('opacity',0)
					.stop()
					.animate({
						opacity: 1
					}, 300 );
			}
		},
		function() {
			var subnav = $(this).children().eq(1);			
			if (subnav.length>0) {
				subnav.hide();	
				$(this).children().eq(0).removeClass('navActive');
			}
			
		}
	);


	// apply image bar opacity
	$(".imageBar").css('opacity', 0.7);

	
	$(".section .description p:last").css('padding-bottom', 0);
	
	// overview link	
	$(".overviewLink").click(function(){
		var $desc = $(this).parent().parent().children('.description');
		$desc.slideDown('fast');
		$desc.parent().prev().children('.close').show();
		
		// close the photo if opened
		$(this).parent().parent().children('.photo').slideUp('fast');
		
		// show social media
		if ( $(this).parent().next().hasClass('socialMedia') ) {
			$(this).parent().next().show();	
		}
		
		return false;								  
	});

	// photo link	
	$(".photoLink").click(function(){
		var $photo = $(this).parent().parent().children('.photo');
		$photo.slideDown('fast');
		$photo.parent().prev().children('.close').show();
		
		// close the description if opened
		$(this).parent().parent().children('.description').slideUp('fast');
		
		return false;								  
	});


	// close overview and photo
	$(".section .head .close").click(function(){
		$(this).hide();
		$(this).parent().next().children('.description').slideUp('fast');
		$(this).parent().next().children('.photo').slideUp('fast');
		
		// close social media
		$(this).parent().next().children('.socialMedia').hide();
		
		return false;
	});
	
	
	// tool tips
	if ( $(".tip").length>0 ) {
		$(".tip").each(function(i){		
			$me = $(this);
			$offset = $me.offset();
			$("body").append('<div id="tooltip-'+i+'" class="tooltip hide">'+$me.attr('title')+'</div>');
			
			$("#tooltip-"+i).css({
					left: $offset.left + ($me.width()/4),
					top: $offset.top - 120
				})
				.data('top', $offset.top - 120);
				
			$me.attr('rel', 'tooltip-'+i);
			
			$(this).attr('title', '');
		});
	}
	$(".tip").hover(
		function(){						
			$me = $("#"+$(this).attr('rel'));					
			$me.show()
				.css('opacity', 0)
				.stop()
				.animate({
					top: parseInt($(this).offset().top) - 55, 
					opacity: 1
				}, 300);

		},
		function(){
			$me = $("#"+$(this).attr('rel'));					
			$me.stop()
				.animate({
					top: $me.data('top'), 
					opacity: 0
				}, 300);
		}
	);
	
	
	// auto open minitries section when clicked from home page
	var currentUrl = document.location.toString();
	if (currentUrl.match('#')) { // the URL contains an anchor  
		var url = currentUrl.split('#');
		
		if (url[1]!='') {
			setTimeout(function(){
				var $desc = $("#"+url[1]+" .description");				
				$desc.slideDown('fast');
				$desc.parent().prev().children('.close').show();
			}, 600);
		}
	}
	
	
	
	
	/**
	 * Modal Functionalities 
	 **/
	positionModal = function(){
		 var wWidth = window.innerWidth;
		 var wHeight = window.innerHeight;
	
		 if (wWidth==undefined) {
			 wWidth = document.documentElement.clientWidth;
			 wHeight = document.documentElement.clientHeight;
		 }
	
		 var boxLeft = parseInt((wWidth / 2) - ( $("#modal").width() / 2 ));
		 var boxTop = parseInt((wHeight / 2) - ( $("#modal").height() / 2 ));
	
		 // position modal
		 $("#modal").css('margin', boxTop + 'px 0 0 ' + boxLeft + 'px');		
	}

	loadModal = function(url, className){
		 $("body").append('<div id="modalBackground"></div>');
		 $("#modalBackground").css("opacity", 0).fadeTo("slow", "0.60");
		 $("body").append('<div id="modalWrapper"></div>');
		 $("#modalWrapper").append('<div id="modal"></div>');	
		 if (className!='') {
			 $('#modal').addClass(className);
		 }

		$.ajax({
			 url: url,
			 cache: false, 
			 success: function(html) {		
				 $("#modal").html(html);					
				 $("#modal").prepend('<div class="closeModalWrap"><a href="#" class="closeModal"><span>x</span></a></div>');
				 positionModal();
				 
				 if ( $(".progress").length>0) {
					 $(".progress").animate({width: '513px', 'background-position': '100% 0'}, 6000, function(){ closeModal(); });
				 }				 
			 }, 
			 error: function() {
				alert("Error loading page");
				closeModal();
			 }
		 });
		
		return false;
	}
	
	
	closeModal = function(){
		 $("#modalWrapper").remove();
		 $("#modalBackground").remove();
	}	

	// close modal trigger
	$(".closeModal, .cancelModal").live('click', function(){
		 closeModal();
		 return false;
	});	
		
				
	// open modal
	$('.modal').live('click', function(){
		var url = $(this).attr('href');
		loadModal(url);
		return false;
	});	
	
	
	/* File Portal */
	if ( $("#btnUpload").length>0 ) {
		$('#file').MultiFile({
			STRING: {
				remove:'Remove', 
				icon_url: '/static/media/i/'
			},
			list: '#fileContainer'
		});
	}
	
	$("#btnUpload").click(function(){
		$('#formAddFiles').submit();
		
		update_progress();
	});
	
	update_progress = function() {
		$.ajax({
			async: false,
			cache: false,
			data: {'upload_id':$('#upload_id').val()},
			dataType: 'json',
			error: function(request, status, error) {
				$(".progress").animate({width:'0%'});
				$(".uploadStatus").html('unexpected error retrieving progress');
				setTimeout(update_progress, 1000);
			},
			success: function(data, status, request) {
				if(data) {
					if($("#portalModal").length==0) {
						$("body").append('<div id="modalBackground"></div>');
						$("#modalBackground").css("opacity", 0).fadeTo("slow", "0.60");
						$("body").append('<div id="modalWrapper"></div>');
						$("#modalWrapper").append('<div class="boxPortal" id="modal"></div>');
						$("#modal").html('\
		<div id="portalModal" class="modalBody">\
			 <h2 id="hdrUploading"><span class="hide">Uploading...</span></h2>\
			<div class="modalDotSection portalDotSection">&nbsp;</div>\
			<!-- end of .modalDotSection -->\
			<div class="modalDotSection">\
				<div class="uploader">\
					<div class="progressBar"><div class="progress"></div></div>\
					<p class="uploadStatus"></p>\
				</div>\
			</div>\
			<!-- end of .modalDotSection -->\
			<div class="modalFooter">\
				 <button type="button" id="btnCancel" class="cancelModal"><span class="hide">Cancel</span></button>\
			</div>\
		</div>');
						$("#modal").prepend('<div class="closeModalWrap"><a href="#" class="closeModal"><span>x</span></a></div>');
						positionModal();
					}
					$(".progress").animate({width:data['received']+'%'});
					$(".uploadStatus").html(data['status']);
				}
				setTimeout(update_progress, 1000);
			},
			type: 'get',
			url: window.location.pathname+'upload-progress/'
		});
	}
	
	
	$("label.inField").click(function(){
		$(this).fadeOut('fast');
	});
	$("input.inField").focus(function(){
		$(this).prev().fadeOut('fast');
	});
	$("input.inField").blur(function(){
		if ( $(this).val()=='' ) {
			$(this).prev().fadeIn('fast');
		}
	});
	
	
});

/*
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Uploading</title>
</head>

<body>

	<div id="portalModal" class="modalBody">
    	 <h2 id="hdrUploading"><span class="hide">Uploading...</span></h2>

        <div class="modalDotSection portalDotSection">&nbsp;</div>
        <!-- end of .modalDotSection -->    
        
        <div class="modalDotSection">
            <div class="uploader">
                <div class="progressBar"><div class="progress"></div></div>
                <p class="uploadStatus">58% - 4.8MB of 8.0 MB</p>
            </div>
        </div>
        <!-- end of .modalDotSection -->   
        
        <div class="modalFooter">
             <button type="button" id="btnCancel" class="cancelModal"><span class="hide">Cancel</span></button>
        </div>
          
    </div>
    <!-- end of .modalBody -->

</body>
</html>
*/
