$nym=jQuery.noConflict();


// post image gallery
$nym(document).ready(function(){

	function _initPostGalleries()
		{
			$nym('div.post-gallery').each(function(){
				var gallery = $nym(this);
				
				var hiddenSpans = gallery.find('.hidden span');
				
				$nym('a.prev, a.next', gallery).click(function(e){
					e.preventDefault();
					var fn = $nym(this);
					var current = hiddenSpans.filter('.current');
					var show;
					
					if (fn.hasClass('next')) {
						show = current.next();
						if (!show.length) {
							show = hiddenSpans.filter(':first');
						}
					} else {
						show = current.prev();
						if (!show.length) {
							show = hiddenSpans.filter(':last');
						}
					}
					show.addClass('current').siblings().removeClass('current');
					
					showImage(show.attr('rel'), show.attr('alt'), show.attr('title'), $nym('.wrap', gallery));
					
				}).filter('a.next').trigger('click');
				
				$nym('a.prevtext, a.nexttext', gallery).click(function(e){
					e.preventDefault();
					var fn = $nym(this);
					var current = hiddenSpans.filter('.current');
					var show;
					
					if (fn.hasClass('nexttext')) {
						show = current.next();
						if (!show.length) {
							show = hiddenSpans.filter(':first');
						}
					} else {
						show = current.prev();
						if (!show.length) {
							show = hiddenSpans.filter(':last');
						}
					}
					show.addClass('current').siblings().removeClass('current');
					
					//showImage(show.attr('rel'), $nym('.wrap', gallery));
					showImage(show.attr('rel'), show.attr('alt'), show.attr('title'), $nym('.wrap', gallery));
					
				}).filter('a.nexttext').trigger('click');				
				
								
			
			});
			
			
			function showImage(img, alt, title, wrap)
			{
				wrap.fadeOut('fast', function(){
					$nym(this).html('<img src="' + img + '" alt="' + alt + '" title="' + title + '" />').fadeIn('fast');
				});
			}
		}
		_initPostGalleries();
				
});
//


// CLEAR FORM
$nym(document).ready(function(){

	$nym('.js-clear').click(
	function(event){ 
		$nym(this).parents('form').reset(); 
		
		}
	);

});
//



// FORM AUTOVALUES
$nym(document).ready(function(){

	function populateElement(selector, defvalue) {
		$nym(selector).focus(function() {
			if ($nym(selector).val() == 'Website') {
				$nym(selector).val('http://');
			} else if ($nym(selector).val() == defvalue) {
				$nym(selector).val('');
			}
		});

		$nym(selector).blur(function() {
				if ($nym.trim($nym(selector).val()) == '') {
						$nym(selector).val(defvalue);
				}
		});
	};

	$nym('form').find('input[type="text"], input[type="password"], textarea').each(function() {
				populateElement($nym(this), $nym(this).val());
			}
	);

});

//



var error_animation=function(objects)
{  

	objects.animate( { opacity: 0.2 }, 175 ).animate( { opacity: 1 }, 150 ).animate( { opacity: 0.2 }, 150 ).animate( { opacity: 1 }, 150 ).animate( { opacity: 0.2 }, 150 ).animate( { opacity: 1 }, 150 );

};





$nym(document).ready(function(){


	$nym('.js-proceed').click( function(event) {
		
		error=false;
		
		$nym(this).parents('form').find('input[type="text"], textarea').each( function() 
		{
			
			if($nym(this).hasClass('v-text')&&($nym(this).val()==$nym(this).attr('title')||$nym(this).val()==''))
			{
				error=true;	
				error_animation($nym(this));
			}
			
			if($nym(this).hasClass('v-mail')&&!isValidEmailAddress($nym(this).val()))
			{
				error=true;	
				error_animation($nym(this));		
			}
			
		});
		
		if(!error)
		{	
			var url_node=$nym(this).parents('form').find("input[name='url']");
			
			if(url_node.val()==url_node.attr('title'))
			{
				url_node.val('');	
			}
			
			$nym(this).parents('form').submit();
			$nym(this).parents('.add-commentbox').animate({height:'hide',opacity:'hide'}, 'slow');	
			url_node.val(url_node.attr('title'));
			
			
			
		}
		
	});
});



function isValidEmailAddress(emailAddress) {

var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);

return pattern.test(emailAddress);

}


// TOP MENU
$nym(document).ready(function(){

	$nym('ul.js-automenu').children('li').stop().hover(
		function() {
			$nym(this).children('ul').slideDown(100);
		},
		function() {
			$nym(this).children('ul').slideUp(100);
		}
	);


	$nym('ul.js-automenu li ul').hover(
		function() {
			
			node=$nym(this).parents('li').attr('class');
			if($nym(this).hasClass('two-levels'))
			{
				$nym(this).parents('li').attr('class',node+'active');
			}
		},
		function() {
			
			node=$nym(this).parents('li').attr('class');
			if($nym(this).hasClass('two-levels'))
			{
				$nym(this).parents('li').attr('class',node.replace('active',''));
				var vnode=$nym(this).find('.children'); 
				vnode.animate({height:'hide'}, 'fast');
		
			}
		}
	);


});
//




var enableimg=function(objects)
{  

	objects.each
	(
			function(i,val)
			{	
			
			
				var i=$nym(this).find('span');
				
				
							o = {
									id : i.attr('id'),
									cl : i.attr('class'),
									ttl : i.attr('title'),
									src : i.attr('source'),
									alt : i.attr('alt')
								};
				
				var img='<img src="'+o.src+'" id="'+o.id+'" class="'+o.cl+'" alt="'+o.alt+'" title="'+o.ttl+'"  />';
				
				var destination=$nym(this);
				
				var container=$nym(this).parents('.img-holder');
				
				loadimg(img,destination,container);
			
			}
		
	);
	
};
 	






var loadimg=function(img,dest,cont)
{   
	
	$nym(img).load
	(		
		function()
		{
			$nym(dest).append( $nym(this) );
		}
	);	
};









// SHOW post
$nym(document).ready
(
 
 function(){
	$nym('a.open-post').click( function(event) 
		{
			
			var node = $nym(this).parents('.post').children('.post-container');
			var nodeshow = $nym(this).parents('.post').find('.pt-open-link');
			var nodeclose = $nym(this).parents('.post').find('.pt-close-link');
			var scroller= $nym(this).parents('.post').children('.post-container').find('.b-com-scroll');
			var imgs=$nym(this).parents('.post').children('.post-container').children('.post-content').find('.img-holder');
			
			
			
			if(node.css('display') == 'none')
			{	
				
				$nym('.post-container').css('display','none');
				nodeshow.css("display","none");
				nodeclose.css("display","block");
				node.animate({height:'show'}, 'slow');
				apply_scroller(scroller);
				enableimg(imgs);
							
			}
			else
			{
			
				node.animate({height:'hide'}, 'slow');
				nodeshow.css("display","block");
				nodeclose.css("display","none");

			}
			
			var obj=$nym(this).parents('.post');
			var goto='.post-title';
			$nym.scrollTo( obj.find(goto), 1000 );
							
});});
//


//INITIATE SHOW POST

$nym(document).ready
(
 function() 
 {

setTimeout( 
 function(){


			var count=$nym('.post').length;

			var node=$nym('.post-container');
			
			var nodeshow = $nym('.pt-open-link');
			var nodehide = $nym('.pt-close-link');
			
			var imgs=$nym('.post').children('.post-container').children('.post-content').find('.img-holder');
			
			if(count<2)
			{
				node.animate({height:'show'}, 'slow');
				nodeshow.css("display","none");
				nodehide.css("display","block");
				apply_scroller($nym('.b-com-scroll'));
				enableimg(imgs);
			}
			

  }
  ,300);
 }
);
//



// SHOW ADD COMMENT BOX
$nym(document).ready
(
 function(){
	$nym('.add-comment').click( function(event) 
		{			
			var node = $nym(this).parents('.post-container').children('.add-commentbox');
			//var nodeview = $nym(this).parents('.b-com-add').children('.js-view');
			//var nodehide = $nym(this).parents('.b-com-add').children('.js-hide');
			
			//var node = $nym(".b-com-viewer"); // custom
			
			if(node.css('display') == 'none')
			{
				
				node.animate({height:'show',opacity:'show'}, 'slow');
				//nodeview.css("display","none");
				//nodehide.css("display","block");
				
					var obj=$nym(this).parents('.post-container').children('.comment-box');
					$nym.scrollTo( obj, 1000 );

			}
			else
			{
			
				node.animate({height:'hide',opacity:'hide'}, 'slow');
				//nodeview.css("display","block");
				//nodehide.css("display","none");
			}
			
			

			
		}
	);

  }
);
//



// SHOW COMMENTS
$nym(document).ready
(
 function(){
	$nym('a.js-show-comments').click( function(event) 
		{
			
			
			var node = $nym(this).parents('.b-com-box').children('.b-com-viewer');
			var nodeview = $nym(this).parents('.b-com-view').children('.js-view');
			var nodehide = $nym(this).parents('.b-com-view').children('.js-hide');
			
			//var node = $nym(".b-com-viewer"); // custom
			
			
			if(node.css('display') == 'none')
			{
				node.animate({height:'show',opacity:'show'}, 'slow');
				nodeview.css("display","none");
				nodehide.css("display","block");	
			
				node.children('.b-com-scroll').jScrollPane();
			
			
			}
			else
			{
			
				node.animate({height:'hide',opacity:'hide'}, 'fast');
				nodeview.css("display","block");
				nodehide.css("display","none");
			}

			
			
		}
	);

  }
);
//



// send com
var loadnote=function(button) 
		{
			
			
			var node = button.parents('.comment-box').children('.com-note');
			var nodehide = button.parents('.comment-box').children('.add-commentbox');
			
			if(node.css('display') == 'none')
			{
				node.animate({height:'show',opacity:'show'}, 'slow');
				nodehide.animate({height:'hide',opacity:'hide'}, 'slow');
			}
			
			else
			{			
				node.animate({height:'hide',opacity:'hide'}, 'slow');
				nodehide.animate({height:'show',opacity:'show'}, 'slow');
			}
			
			
		};










	



$nym(document).ready
(
function()
{

	$nym('#archives .down').click( function(event) 
		{
			var node=$nym(this).parents('#archives').children('.archives-m').children('div');
			go_down(node,node.children('p'),0);
		});
	
	$nym('#archives .up').click( function(event) 
		{
			var node=$nym(this).parents('#archives').children('.archives-m').children('div');
			go_up(node,node.children('p'),0);
		});
	
}
);





/*
node - items container with overflow hidden parameter
step_node - items to roll
fix - cufont fix, usually cufont adds some pixels
*/

var go_down=function(node,step_node,fix) 
		{
			var len=step_node.length;
			var h=node.innerHeight();
			var step=Math.floor(h/len)+fix;
			var top=node.css("top").replace("px","");
			var topv=top*1;
			var go_to_h=topv-step;
			
			var fl=top/step - Math.floor(top/step); 
			
			if((h>(-go_to_h)) && fl=="0")
			{
				go_to_h=go_to_h+"px";
				node.animate({top:go_to_h}, 'slow');
			}
			
			
		};



/*
node - items container with overflow hidden parameter
step_node - items to roll
fix - cufont fix, usually cufont adds some pixels
*/

var go_up=function(node,step_node,fix) 
		{
			var len=step_node.length;
			var h=node.innerHeight();
			var step=Math.floor(h/len)+fix;
			var top=node.css("top").replace("px","");
			var topv=top*1;  
			var fl=top/step - Math.floor(top/step);
			var go_to_h=topv+step;
			
			if(top<0 && fl=="0")
			{   
				go_to_h=go_to_h+"px";
				node.animate({top:go_to_h}, 'slow');
			}
			
			
		};





$nym(document).ready(function() {
					   
    $nym('.sp-slides').cycle({
	fx:'fade',
	next: '.next-slide', 
	prev:'.prev-slide',
	timeout:0
	 // choose your transition type, ex: fade, scrollUp, shuffle, etc...
	});
	
});
//




$nym(document).ready(function() {
					   
    $nym('.sp-slides-h').cycle({
	fx:'fade',
	next: '.next-slide', 
	prev:'.prev-slide',
	timeout:5000
	 // choose your transition type, ex: fade, scrollUp, shuffle, etc...
	});
	
});
//




/*FANCY scrollers*/

var apply_scroller=
function(scroller) 
{

	scroller.jScrollPane({scrollbarWidth:'4'});

};













$nym(document).ready(function(){

	
	$nym('.news-item em a').click(function() 
	{
		$nym('.news-item em a').removeClass('a-h');
		$nym(this).addClass('a-h');
		fade_out($nym('.news-item-content'));
		fade_in($nym(this).parents('.news-item').children('.news-item-content'));
		
		
		
	}
	);
	
	
	
	
	$nym('.news-holder .next').click(function() 
	{	
		var what=$nym('.news-current').parents('.news-item').find('.selector');
		var node=what.attr('id').replace('it-','');
		scrollit(node,'next',what);
	}
	);
	
	$nym('.news-holder .prev').click(function() 
	{	
		var what=$nym('.news-current').parents('.news-item').find('.selector');
		var node=what.attr('id').replace('it-','');
		scrollit(node,'prev',what);
	}
	);




	$nym('.item-image-holder a').hover(function() 
	{	
		$nym('.item-image-holder p').css('top','-99999px').css('opacity','0.00');
		var node=$nym(this).parents('.item-image-holder').children('p').css('top','0px').animate({opacity: '1.00'}, 500);
	}
	);
	




});



var scrollit=
function(node,where,what) 
{


		$nym('#it-'+node).removeClass('a-h');
		if(where=="next")
		{
			node=parseInt(node)+1;
		}
		else
		{
			node=parseInt(node)-1;	
		}
		
		var check=$nym('#it-'+node).parents('.news-item').find('.news-item-content').attr('class');
		
		if(check)
		{
		
			fade_out($nym('.news-item-content'));
			fade_in($nym('#it-'+node).parents('.news-item').find('.news-item-content'));
			$nym('#it-'+node).addClass('a-h');
		
		}
};




var fade_out=
function(what) 
{

what.css('top','-999999px').css('opacity','0.00').removeClass('news-current');

};

var fade_in=
function(what) 
{

what.css('top','0px').animate({opacity: '1.00'}, 500).addClass('news-current');

};






/*
holder - overall holder, must contain '.g-out' and '.g-in'
itemmarker - item class
items - number of items in the '.g-in'
padding - item separator width ( width between items )
where - where to scroll (next/prev)
*/

var scroll_next=function(where,holder,itemmarker,items,padding)
{
	

			var step=$nym(holder).find('.g-out').innerWidth()+padding;
			
			var node=$nym(holder).find('.g-in');
			
			var itemstep=node.find(itemmarker).innerWidth()+padding;
			
			var pp=node.css('left').replace('px','');
			
			var pp_1=pp/step; var pp_2=Math.ceil(pp_1);
			
			if((pp_1-pp_2)==0)
			{
				
				var divs=(node.find('div').length);
				
				if(where=='next')
				{
					var node_p=pp*1-step;
					var ce=Math.ceil(divs/items)*items*itemstep;
					var ind=-1;
				}
				else
				{
					var node_p=pp*1+step;	
					var ce=1;
					var ind=1;
				}
				

				
				
				
				
				if(node_p*(ind)<ce)
				{
					node.animate({left:node_p+'px'},500);
				}
				
				
			}


};









$nym(document).ready
(
function()
{
	
	
	
	$nym('#homepage-left .galleries-holder .next').click( function(event) 
	{
		scroll_next('next','#homepage-left .galleries-holder','div',5,25);
	});
	
	$nym('#homepage-left .galleries-holder .prev').click( function(event) 
	{
		scroll_next('prev','#homepage-left .galleries-holder','div',5,25);
	});
	

	$nym('#galleries .galleries-holder .next').click( function(event) 
	{
		scroll_next('next','#galleries .galleries-holder','div',5,12);
	});
	
	$nym('#galleries .galleries-holder .prev').click( function(event) 
	{
		scroll_next('prev','#galleries .galleries-holder','div',5,12);
	});

	$nym('#posts .galleries-holder .next').click( function(event) 
	{
		scroll_next('next','#posts .galleries-holder','div',5,12);
	});
	
	$nym('#posts .galleries-holder .prev').click( function(event) 
	{
		scroll_next('prev','#posts .galleries-holder','div',5,12);
	});




	$nym('.two-levels li a').click( function(event) 
	{	
		var node=$nym(this).closest('li').children('.children'); 
		var vnode=$nym(this).parents('ul').find('.children'); 
		
		if(node.css('display')=='none')
		{	
			
			var ssz=$nym(this).closest('li').find('ul').size();
			
			if(ssz>1)
			{
				vnode.animate({height:'hide'}, 'fast');
			}
			node.animate({height:'show'}, 'fast');
		}
		else
		{
			node.animate({height:'hide'}, 'fast');
		}		
	});





 
        $nym(".two-levels li a.first-level").live('click', function(event) {

            event.preventDefault();
        });
   


		
}
);









  $nym(function() {

    var galleries = $nym('.ad-gallery').adGallery(
	{
	animate_first_image:true,	
	slideshow: 
		{
			speed:5000,
			autostart:true
		},
	effect:'fade'	
	}
	);
    
	$nym('#switch-effect').change(
      function() {
        galleries[0].settings.effect = $nym(this).val();
        return false;
      }
    );
    $nym('#toggle-slideshow').click(
      function() {
        galleries[0].slideshow.toggle();
        return false;
      }
    );
  });





$nym(function() {
            $nym('img').bind("contextmenu", function(e) {
                e.preventDefault();
            });
        });



	var flashvars = {};
	var params = {wmode:'transparent'};
	var attributes = {wmode:'transparent',loop:'false'};
	swfobject.embedSWF(_site_path+'david-pullum-300x180-noBG.swf', "logo-content", "300", "180", "9.0.0",flashvars,params,attributes);






