$nym=jQuery.noConflict();




// 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 );

}




// FORM SUBMIT + VALIDATE
$nym(document).ready(function(){
	function validateNoempty(text) {
		if (!text || text.length < 2) { return false;	}

		return true;
	}

	function validateText(text) {
		if (!validateNoempty(text)) { return false; }

		var re_text = /[^A-Za-z0-9\s!@#$%^&()_+-=}{';":.,><]/;
		if (text.match(re_text) != null ) { return false; }
		return true;
	}

	function validateEmail(email) {
		if (!validateNoempty(email)) { return false; }

		var splitted = email.match("^(.+)@(.+)$nym");
		if (splitted == null) return false;
		if (splitted[1] != null ) {
			var regexp_user=/^\"?[\w-_\.]*\"?$nym/;
			if (splitted[1].match(regexp_user) == null) { return false; }
		}

		if(splitted[2] != null) {
			var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$nym/;
			if (splitted[2].match(regexp_domain) == null) {
				var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$nym/;
				if (splitted[2].match(regexp_ip) == null) { return false; }
			}
			return true;
		}
		return false;
	}

	$nym('.js-proceed').click( function(event) {
										
										
			event.preventDefault();
			error = false;

			$nym(this).parents('form').find('input[type="text"], textarea').each( function() {
				if ($nym(this).hasClass('v-noempty')) {
					if (!validateNoempty($nym(this).val())) {
						
						
						error_animation($nym(this));
						
						
						error = true;
					}

				} else if ($nym(this).hasClass('v-text')) {
					if (!validateText($nym(this).val())) {
						
						error_animation($nym(this));
						
						error = true;
					}
				} else if ($nym(this).hasClass('v-mail')) {
					if (!validateEmail($nym(this).val())) {
						
						error_animation($nym(this));
						
						error = true;
					}
				}
			});

			if (!error) {

				$nym(this).parents('form').submit();
				$nym(this).parents('.add-commentbox').animate({height:'hide',opacity:'hide'}, 'slow');
			}

			return false;
		}
	);

});
//



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

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


	$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:'scrollHorz',
	next: '.next-slide', 
	prev:'.prev-slide',
	timeout:0
	 // choose your transition type, ex: fade, scrollUp, shuffle, etc...
	});
	
});
//


/*FANCY scrollers*/

var apply_scroller=
function(scroller) 
{

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

};





/*
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(function() {

    var galleries = $nym('.ad-gallery').adGallery(
	{
	animate_first_image:false
	}
	);
    
	$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;
      }
    );
  });

