function is_mobile() {
	var deviceAgent = navigator.userAgent.toLowerCase();
	var agentID = deviceAgent.match(/(ipad|ipod|iphone|blackberry|android)/);
	return (agentID == null) ? false : true;
}

var apply_scroller = function (scroller) {
	scroller.jScrollPane({scrollbarWidth:'6'});
};

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


// CLEAR FORM
$(function(){
	$('.js-clear').click(
			function(event){
				$(this).parents('form').reset();

			}
	);
});



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

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

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

});








// FORM SUBMIT + VALIDATE
$(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("^(.+)@(.+)$");
		if (splitted == null) return false;
		if (splitted[1] != null ) {
			var regexp_user=/^\"?[\w-_\.]*\"?$/;
			if (splitted[1].match(regexp_user) == null) { return false; }
		}

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

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

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


							error_animation($(this));


							error = true;
						}

					} else if ($(this).hasClass('v-text')) {
						if (!validateText($(this).val())) {

							error_animation($(this));

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

							error_animation($(this));

							error = true;
						}
					}
				});

				if (!error) {

					$(this).parents('form').submit();
				}

				return false;
			}
	);

});
//



// TOP MENU
$(document).ready(function(){
	$('ul.js-automenu').children('li').hover(
			function() {
				$(this).children('ul').show('fast');
			},
			function() {
				$(this).children('ul').hide('fast');
			}
	);

});
//




var enableimg=function(objects)
{
	objects.each
			(
					function(i,val)
					{


						var i=$(this).find('span.post-picture');


						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=$(this);

						var container=$(this).parents('.img-holder');

						loadimg(img,destination,container);

					}

			);

}







var loadimg=function(img,dest,cont)
{

	$(cont).addClass('loading');
	$(cont).addClass('fade15');

	$(img).load
			(
					function()
					{
						$(dest).append( $(this) );
						$(cont).removeClass('loading');
						$(cont).removeClass('fade15');
					}
			);
}





/*makes first image from the post*/
$(function() {

	check=$('.post');
	if(check.length>0)
	{



		check.each
				(
						function()
						{
							var cont=$(this).children('.post-container').children('.post-content');

							first_img=cont.find('img').css('display','none');
							img_title_container=cont.parents('.post').children('.post-title').children('.post-title-i').children('.pt-open');
							img_title_container.prepend(first_img.css('display','block'));


							first_piece=cont.find('.first-piece').html();
							first_piece_container=$(this).find('.b-post-title-scroll');
							first_piece_container.html(first_piece);
							cont.find('.first-piece').html('');
							//first_piece_container.prepend(first_piece.css('display','block'));

						}

				);



	}

	img=$('.x-intro-t').find('img').css('display','none');
	noder=$('.intro-img');
	noder.prepend(img.css('display','block'));



});
//





// SHOW post
$(function(){


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

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

	function addImageToBook(src) {
		var list = $('#design-book-content .track ul');
		//make sure it doesn't already exist
		if(list.find('img[src='+src+']').length == 0) {
			//get active folder
			var folder = $('#design-book .folder-view ul a.active').parent('li');
			var fid = folder.attr('id').substr(7);
			$.post(_ajax_load_url, 'action=design_book_user&subaction=addimage&fid='+fid+'&src='+src, function(html) {
				html = cleanAjax(html);
				$(html).appendTo(list).imageMakeClick();
			});
		}
	}

	$.fn.imageMakeClick = function() {
		var i = $(this);
		i.hover(function() {
			$(this).find('img').animate({opacity:.2}, 500);
			$(this).find('a').fadeIn();
		}, function() {
			$(this).find('a').fadeOut();
			$(this).find('img').animate({opacity:1}, 500);
		});

		i.find('.fimage').fancybox();

		//erase btn
		i.find('.ferase').click(function(e) {
			e.preventDefault();
			var iid = $(this).closest('li').attr('id');
			iid = iid.substr(6);
			ajaxIt('subaction=deleteimage&iid='+iid);
			i.remove();
		});
	}

	//clean stupid 0 off the end of result
	function cleanAjax(html) {
		var last = html.substr(html.length-1);
		if(last == '0') {
			return html.substr(0, html.length-1);
		}else {
			return html;
		}
	}

	var open_post_handler = function(event)
	{
		event.preventDefault();


		var post = $(this).parents('.post');

		var node = post.children('.post-container');
		var nodeshow = post.children('.pt-open-link');
		var nodeclose =post.children('.pt-close-link');
		var loading = post.find('div.loading');
		var scroller= node.find('.b-com-scroll');
		var imgs=node.children('.post-content').find('.img-holder');

		var obj=$(this).parents('.post');

		if(node.is(':hidden')) {

			if (node.text() == '') {

				loading.show();

				node.load(_ajax_load_url, {
					'action':'spt_load_post',
					'id':node.attr('rel')
				}, function(){

					loading.hide();

					node.find('.pt-open-link').hide();
					node.find('.pt-close-link').show();
					node.slideDown('slow', function(){
						apply_scroller(node.find('.b-com-scroll'));
					});

					enableimg(node.children('.post-content').find('.img-holder'));
					$.scrollTo(node);

					Cufon.replace('h3', { fontFamily: 'Bickham Script Pro' } );
					Cufon.replace('h1,h2,#tail', { fontFamily: 'Trajan Pro' } );

					node.find('input[type="text"], input[type="password"], textarea').each(function() {
								populateElement($(this), $(this).val());
							}
					);
					node.find('.add-comment').click( function(event) {
						var node = $(this).parents('.post-container').children('.add-commentbox');
						if(node.css('display') == 'none') {
							node.animate({height:'show',opacity:'show'}, 'slow');
							var obj=$(this).parents('.post-container').children('.comment-box');
							$.scrollTo( obj, 1000 );
						} else {
							node.animate({height:'hide',opacity:'hide'}, 'slow');
						}
					});
					if ($('#design-book').length > 0)
					{

						//add design btn
						node.find('img').each(function() {
							//check for parent <a>
							var p = $(this).parent('a');
							w = (p.length>0) ? p : $(this);
							w.wrap('<p class="design-book-image" />').after('<a href="#" class="add-to-design-book">Add to design book</a>');
						});

						//design btn
						node.find('p.design-book-image').each(function() {
							var wrap = $(this);
							var img = wrap.find('img'), src = img.attr('src'), w = img.attr('width');
							var trig = wrap.find('a.add-to-design-book');

							//size
							wrap.width(w);

							//hover
							wrap.hover(function() {
								trig.fadeIn();
							}, function() {
								trig.fadeOut();
							});
							//link
							trig.click(function(e) {
								e.preventDefault();
								addImageToBook(src);
							});
						});
					}
				});
				return;
			} else {
				nodeshow.css("display","none");
				nodeclose.css("display","block");
				node.animate({height:'show',opacity:'show'}, 'slow');
				apply_scroller(scroller);
				enableimg(imgs);
				var goto='.post-content';
			}
		}
		else {

			node.css('display','none');
			nodeshow.css("display","block");
			nodeclose.css("display","none");
			var goto='.post-title';

		}
		$.scrollTo( obj.find(goto), 1000 );
	}

	if (!is_mobile()) {
		$('a.open-post').click(open_post_handler);
	}

});
//


//INITIATE SHOW POST

$(document).ready
		(
				function()
				{

					setTimeout(
							function(){


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

								var node=$('.post-container');

								var nodeshow = $('.pt-open-link');
								var nodehide = $('.pt-close-link');

								var imgs=$('.post').children('.post-container').children('.post-content').find('.img-holder');

								if(count<2)
								{

									node.animate({height:'show',opacity:'show'}, 'slow');
									nodeshow.css("display","none");
									nodehide.css("display","block");
									apply_scroller($('.b-com-scroll'));
									enableimg(imgs);
								}


							}
							,300);
				}
		);
//



// SHOW ADD COMMENT BOX
$(function(){
	$('.add-comment').click( function(event) {
		var node = $(this).parents('.post-container').children('.add-commentbox');
		if(node.css('display') == 'none') {
			node.animate({height:'show',opacity:'show'}, 'slow');
			var obj=$(this).parents('.post-container').children('.comment-box');
			$.scrollTo( obj, 1000 );
		} else {
			node.animate({height:'hide',opacity:'hide'}, 'slow');
		}
	});
});



// SHOW COMMENTS
$(document).ready
		(
				function(){
					$('a.js-show-comments').click( function(event)
							{


								var node = $(this).parents('.b-com-box').children('.b-com-viewer');
								var nodeview = $(this).parents('.b-com-view').children('.js-view');
								var nodehide = $(this).parents('.b-com-view').children('.js-hide');

								//var node = $(".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');
	}


}









$(document).ready(function() {

	$('.p-i-2-right, .p-i-2-left').each(

			function()
			{
				var h=$(this).children('.text').children('h2').height();
				var hh=(Math.ceil((226-h)/16)-2)*16;
				var node=$(this).children('.text').children('.text-l');
				$(this).children('.text').find('.text-l').css('height',hh+'px');
				var cont=node.html();
				var node2=$(this).children('.text').children('.text-r').children('p');
				node2.html(cont);
				node2.css('margin-top','-'+hh+'px');
				$(this).children('.text').children('.text-r').css('height',hh+h+8+16+'px');
			}

	);




	$('.p-i-1-right, .p-i-1-left').each(

			function()
			{
				var h=$(this).children('.text').children('h2').height();
				var hh=(Math.ceil((390-h)/16)-2)*16;
				var node=$(this).children('.text').children('.text-l');
				$(this).children('.text').find('.text-l').css('height',hh+'px');
			}


	);



});


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


	Cufon.replace('h3', { fontFamily: 'Bickham Script Pro' } );
	Cufon.replace('h1, h2, #tail', { fontFamily: 'Trajan Pro' } );
});
//


/*FANCY scrollers*/







