$(document).ready(function(){
	
	//set class for links to external sites and other document types
	$('#content a[href^="http://"]').addClass('external').attr('target', '_blank');
	$('#content a[href^="https://"]').addClass('external').attr('target', '_blank');
	$('#content a[href^="http://projectcool"]').removeClass('external').attr('target', '_self');
	$('#content a[href^="http://client.grcmc"]').removeClass('external').attr('target', '_self');
	$('#content a[href$=".pdf"]').addClass('pdf').attr('target','_blank');
	$('#content a[href$=".ppt"]').addClass('powerpoint').attr('target','_blank');
	$('#content a[href$=".doc"]').addClass('word').attr('target','_blank');
	$('#content a[href$=".xls"]').addClass('excel').attr('target','_blank');
	$('#content a[href$=".csv"]').addClass('excel').attr('target','_blank');
	$('#content a.remove_external').removeClass('external').attr('target', '_blank');


	// GALLERY LIGHTBOX
	$('#gallery a:has(img)').attr('rel', 'gallery').fancybox({
			cyclic				:true,
			overlayColor	: '#000',
			transitionIn	:'elastic',
			transitionOut	:'elastic'
	
	});

	$("a.gallery").fancybox();
	
	$('a.gallery_album_img').click(function() {
		$.fancybox.showActivity();		
		$.getJSON($(this).attr('href'), function(data){			
			//console.log(data);
			$.fancybox(data, {
				cyclic			:true,
				overlayColor	: '#000',
				type            : 'image',
				transitionIn	:'elastic',
				transitionOut	:'elastic'
			});	
		});
		return false;
	});


	
	 // ROUNDED IMAGES
	// any image that has a class roundborder
	$('img.roundborder').each(function(){
		$div = $('<div/>').addClass('rounded_image round_10').css('background',"url('"+$(this).attr('src')+"') no-repeat");
		$(this).wrap($div);
		
	}); 


	// HIDE-SHOW 
	$('.view_details').click(	
		function(){
			var toggleHide = 'images/templates/hide.gif';
			var toggleShow = 'images/templates/view.gif';
			var eDiv = $('div.'+$(this).attr('rel'));
			var eImg = $(this).children('img');
			
		if(eDiv.is(':hidden')){
				$(this).children('img').attr('src', toggleHide);
				eDiv.show('slow');
				
			}								
			
		else if(eDiv.is(':visible')){
				$(this).children('img').attr('src', toggleShow);
				eDiv.hide('slow');
				
			}
		
		 $(this).blur();
		return false;				
	});

if ($.browser.msie) {
	var option = {
		 x:      1, 
		 y:      2, 
		radius: 3,
		color:  "#ccff00"
	}
  $("h1").textShadow(option);
}

$('.gsc-control').css({width:'800px'}); 

$('#search-submit').hover(
	function(){
		$(this).css({'cursor':'pointer'});
	},
	function(){
		$(this).css({'cursor':'default'});
	}
);


	// DIALOG LINKS
	$('a.dialog').click(function(){
		// do a dialog for the video embed
		makeDialog('<h3><img src="layout_imgs/loading_bar.gif" alt="loading" /> Loading '+$(this).attr('title')+'</h3><p>please wait</p>', $(this).attr('title'));
		var element_rel = $(this).attr('name');
		// ajax in the data
		$('#made_dialog').load($(this).attr('href')+(element_rel ? ' '+element_rel : ''), function(rsp){
			// animate the containers to show everything
			$(this).parent().css('width','705px').animate( {
				top: $(this).parent().offset().top - (screen.height/4) + 20,
				left: '50%',
				marginLeft: -($(this).width() / 2)
			}, 'fast');
		});

		return false;
	});

	

// BLOG
// toggle the comments
$('a.view_comments').each(function(){
	var count = $(this).parent('.links').next('.comments').find('.comment').length;
	$(this).text($(this).text()+' ('+count+')');
}).click(function(){
	$(this).trigger('toggle', true);
	return false;
}).bind('toggle', function(event, jumpTo){
	$link = $(this);
	// slide the comments into place with a callback
	$(this).parent('.links').next('.comments').slideToggle('fast',function(){
		// toggle view/leave with hide
		text = $link.text();
		//console.log('text ' + text);	
		
		if(text.indexOf("View/Leave")>=0){
			text = text.replace("View/Leave","Hide");
			if(jumpTo)
				$link.trigger('jumpTo');
		} else {
			text = text.replace("Hide","View/Leave");
			// dont need a scroll to since we are already at the links
		}
		$link.text(text);
	});
	return false;	
}).bind('jumpTo', function(){
	$.scrollTo($(this).parent(),'fast');	
});




// if there's only one blog post show the comments automatically

if($('div.blog_post').length==1){
	$('div.blog_post .comments').css({display : 'block'});
	//$('div.blog_post').find('a.view_comments').trigger('toggle');

}


// SUBMITTING COMMENTS
$('.comment_form').submit(function(){
	if(!$(this).find('input[name="email"]').val()){
		alert('You must supply your email address');
		return false;
	} else if(!$(this).find('textarea').val()){
		alert('You must supply a comment');
		return false;
	}
	// save the parent div.comments for inserting new comment to proper place
	var $comments = $(this).parents('.comments');
	var i = $(this).find('input[name="i"]').val() * 1;
	var new_i = i;
	new_i = new_i + 1;
	
	var form_data = $(this).serialize();
	$.ajax({
		url:'blog_includes/ajax_comment.php',
		data: form_data,
		type: "POST",
		beforeSend: function(){
			$('body').css('cursor','wait');
			$('.comment_form input, .comment_form textarea').attr('disabled','disabled');
		},
		success: function(data){
			if(data!="0"){
				// insert the comment
				if($('.comment:last',$comments).length){
					$('.comment:last',$comments).after(data);
				}else if($('.none',$comments).length){
					$('.none',$comments).replaceWith(data);
				}else{
			
					window.location.reload();// if there's neither of the two options just refresh cause I don't know whats going on
					
				}	
				// update the i count
				$('input[name^="i"]',$comments).val(new_i);

				// now update the comment link
				var text = $comments.prev('.links').children('a.view_comments').text();
				if(text.indexOf("(")>=0){
					text = text.replace(/\(\d+\)/, "("+i+")")
				} else {
					text += ' ('+i+')';
				}
				$link.text(text);
			} else {
				alert('Sorry, but there was an error and you comment could not be posted at this time. Please try again later');
			}
			$('body').css('cursor','auto');
			$('.comment_form input, .comment_form textarea').removeAttr('disabled').not('input[type="submit"]').not('input[type="hidden"]').val('');
		}
	});

	return false;
	});
	


	$('#archives-blog li.year').click(function(){
		$(this).children('ul').toggle();
	}).find('li.month').click(function(){
		$(this).children('ul').toggle();
		return false;
	}).find('a').bind('click',function(e){
		e.stopPropagation();
	});



}); // end document ready


function makeGoogleMap(address){
	var base = "http://maps.google.com/maps?f=q&hl=en&geocode=&time=&date=&ttype=&q=";
	return base + escape(address);
}


function makeDialog(text, dialog_title, buttons, width, height, hideTitleBar){
	if(!buttons){
		buttons = {
			Ok: function() {
				$(this).dialog('close');
			}
		}
	}

	$('<div id="made_dialog">'+text+'</div>').dialog({
		title: dialog_title,
		dialogClass: hideTitleBar ? 'hide_title' : 'alert',
		bgiframe: true,
		modal: true,
		stack:true,
		autoOpen:true,
		width: width ? width : 460,
		minHeight: height ? height : 220,
		draggable: true,
		resizable: true,
		buttons: buttons,
		close: function(event, ui){
			$(this).remove();
		}
	});
}

window.alert = function(txt){
	makeDialog(txt, 'The page said:');
}


//CATCH CONSOLE.LOG FOR NO-FIREBUG
if (typeof console == 'undefined' || typeof console.log == 'undefined') { console = { log : function (text) { return false; } } }


