function Show(link)
{
    $('nav_show').setHTML('<a href=\"'+link+'\">'+link+'</a>');
}

function Hide()
{
    $('nav_show').empty();
}

Window.addEvent('domready', function() {
	
	/*
	 * accordion
	 */
	var accordion = new Accordion('h3.atStart', 'div.atStart', {
		opacity: false,
		onActive: function(toggler, element){
			toggler.setStyle('color', '#FFF');
			toggler.setStyle('font-weight', 'bold');
			toggler.setStyle('background', '#7e7e7e');
		},
	 
		onBackground: function(toggler, element){
			toggler.setStyle('color', '#000');
			toggler.setStyle('font-weight', 'normal');
			toggler.setStyle('background', '#c3c3c3');
		}
	}, $('accordion'));
	
	/*
	 * input select interaction
	 */
	var list = $$('#Import_Helper select');
	list.each(function(element) {
		var fx = new Fx.Styles(element, {duration:200, wait:false});
 		var type = element.getProperty('name');
		element.addEvent('change', function() {
			var value = this.getValue();
			if(value == "Add New Project")
			{
				Show_Hide(type,'','show');
			}
			else
			{
				if(type == 'project')
				{
					Show_Hide(type,value,'hide');
				}
				else if(type == 'category')
				{
					var type_array = $('Admin_Content').getElements('input[name='+type+']');
					type_array.each(function(type) {
						type.setProperty('value', value);
	   				});
				}
			}
		});	
	});
	
	/*
	 * Profile Catagory selection
	 */
	var catagory_list = $$('#header ul li');
	catagory_list.each(function(li) {
		var text = $E('a', li).innerHTML;
		if(text == 'corporate' || text == 'personal') {
		} else {
 			li.addEvent('click', function(){
				var url = "Grab.class.php?Grab=get_boxes&catagory_boxes="+text;
	  			new Ajax(url, {
	  				method: 'get',
	  				update: $('left'),
	  				onComplete: function() {
	    				Block_Effects();
	    				var first_block = $('left').getFirst();
	    				var block_event = first_block.getProperty('onmouseover');
	    				var block_events = block_event.split("'");
	    				Set_Right(block_events[1],block_events[3],block_events[5]);
	    				Set_Tail();
					}
	  			}).request();
			});
		}
	});

	
	/*
	 * submit data
	 */
	Setup_Posts();
	
	Set_Tail();
	//Block_Effects();
});

function Evaluate_Form_Response(text, element) {
	if(text == "Success")
	{
		var clear_effect = new Fx.Styles(element, {
	    	duration: 500,
	    	transition: Fx.Transitions.linear,
	    	onComplete: function() {
	    		element.remove();
	     	}
	   	});
	   	var flash_effect = new Fx.Style(element, 'background-color', {
	    	duration: 500,
	 		transition: Fx.Transitions.linear,
	     	onComplete: function() { 
	     	    clear_effect.start({
	     	    	'opacity': 0,
	             	'height': 0
	     	    });
	     	}
		});
		var children = element.getChildren();
		children.each(function(child){
			var empty = new Fx.Styles(child, {
	    		duration: 1000,
	 			transition: Fx.Transitions.linear,
	     		onComplete: function() {
	     			if(children.getLast() == child)
	     			{
	     				element.setHTML('<h1>Success</h1>');
	     			}
	     		}
			});
			empty.start({
	     	    	'opacity': 0,
	             	'background-color': '#ffffff'
	     	    });
		});
		flash_effect.start('#a3ad05');
	}
	else
	{
		var flash_effect = new Fx.Style(element, 'background-color', {
	    	duration: 500,
	 		transition: Fx.Transitions.linear
		});
		var children = element.getChildren();
		children.each(function(child){
			var empty = new Fx.Style(child, 'opacity', {
	    		duration: 1000,
	 			transition: Fx.Transitions.linear,
	     		onComplete: function() {
	     			if(children.getLast() == child)
	     			{
	     				element.setHTML(text);
	     			}
	     		}
			});
			empty.start(0);
		});
		flash_effect.start('#ff4e48');
	}
}

function Show_Hide(type,value,event) {
	var type_array = $('Admin_Content').getElements('input[name='+type+']');
	type_array.each(function(type) {
	    var row = type.getParent().getParent();
	    if(event == 'hide')
	    {
	   		var description_row = row.getPrevious();
	    	var description_box = $E('textarea', description_row);
	    	var description_effect = new Fx.Styles(description_box, { duration: 1000, transition: Fx.Transitions.Cubic.easeOut });
	    	var hide_effect = new Fx.Styles(row, {
	    		duration: 500,
	    		transition: Fx.Transitions.linear,
	    		onComplete: function() { 
	    			type.setProperties({
	    	 			type: 'hidden',
						value: value
	   				});
	   				description_effect.start({
	    			  'width': 300,
	    	    	  'height': 90
	    			});
	    		}
	   		});
	    	
	    	hide_effect.start({
	    		'opacity': 0,
	    	    'height': 0
	    	});
	    }
	    else
	    {
	    	type.setProperties({
	    	 	type: 'text',
				value: value
	   		});
	   		var show_effect = new Fx.Styles(row, { duration: 500, transition: Fx.Transitions.linear });
	    	var description_row = row.getPrevious();
	    	var description_box = $E('textarea', description_row);
	    	var description_effect = new Fx.Styles(description_box, { 
	    		duration: 1000,
	    		transition: Fx.Transitions.Cubic.easeOut,
	    		onComplete: function() {
	    			show_effect.start({
	    				'opacity': 1,
	    	    		'height': 16
	    			});
	    		}
	    	});
	    	
	    	description_effect.start({
	    		'width': 150,
	    		'height': 38
	    	});
	    }
	});
}


/*
 * import photos
 */
 function Import_Photos()
 {
	var url = "drop_box.php?Drop_Box=import";
    new Ajax(url, {
    	method: 'get',
    	update: $('Admin_Content'),
    	onRequest:function() {
    		$('Admin_Content').setHTML('<img src="css/loading.gif" />');
    	},
    	onComplete: function() {
	    	window.location.reload();
	    }
    }).request();
}

/*
 * Sets right box contents
 */
 
function Set_Right(text,width,height)
{
	var right = $('right');
	var comment = $('comment_helper');
	var percent = null;
	if (width > height) {
		Percent = (600 / width);
	} else {
	    Percent = (380 / height);
	}
	
	var new_height = Math.round(height*Percent);
	var new_width = Math.round(width*Percent);
	
	if(new_height > 380)
	{
		Percent = (380 / new_height);
		new_height = Math.round(height*Percent);
		new_width = Math.round(width*Percent);
	}
	else if (new_width > 600)
	{
		Percent = (600 / new_width);
		new_height = Math.round(height*Percent);
		new_width = Math.round(width*Percent);
	}
	var margin_left = Math.round((600-new_width)/2);
	var margin_top = Math.round((380-new_height)/2);
	
	var image = new Asset.images(['images/profile/content/'+text+'.jpg']);
	var url = "Grab.class.php?Grab=Comment&Comment="+text;
	
	image.setStyles({
	   width: new_width,
	   height: new_height,
	   'margin-left': margin_left,
	   'margin-top': margin_top,
		'opacity': 0
	});
    
    new Ajax(url, {
    	method: 'get',
    	update: comment,
    	onRequest:function() {
    		right.empty();
    	},
    	onComplete: function() {
    		right.empty();
    		image.injectInside(right);
    		Right_Effects();
    		if($('comment_helper').getStyle('height').toInt() > $('comment').getStyle('height').toInt()) {
    			var arrows_effect = new Fx.Style($('arrows'), 'opacity', { duration: 500, transition: Fx.Transitions.linear });
    			arrows_effect.start(1);
    		} else if($('arrows').getStyle('opacity').toInt() > 0) {
    			var arrows_effect = new Fx.Style($('arrows'), 'opacity', { duration: 500, transition: Fx.Transitions.linear });
    			arrows_effect.start(0);
    		}
	    }
    }).request();
}

function Grab(page)
{
	if(page == 'new_photos' || page == 'categories')
	{
		var show_effect = new Fx.Style($('Admin_Content'), 'height', {
			duration: 500,
			transition: Fx.Transitions.linear,
			onComplete: function() {
				hide_effect.start(1);
	    		}
			});
		var hide_effect = new Fx.Style($('Import_Helper'), 'opacity', { duration: 500, transition: Fx.Transitions.linear });
		show_effect.start(554);
	}
	else
	{
		var show_effect = new Fx.Style($('Admin_Content'), 'height', { duration: 500, transition: Fx.Transitions.linear });
		var hide_effect = new Fx.Style($('Import_Helper'), 'opacity', {
			duration: 500,
			transition: Fx.Transitions.linear,
			onComplete: function() {
				show_effect.start(579);
	    		}
			});
		hide_effect.start(0);
	}
	
	var url = "Grab.class.php?Grab="+page;
    new Ajax(url, {
    	method: 'get',
    	update: $('Admin_Content'),
    	onRequest:function()
    	{
    		$('Admin_Content').setHTML('<center><img src="css/loading.gif" /></center>');
    	}
    }).request();
}

function Grab_Profile_Content(project,category)
{
	var url = "Grab.class.php?Grab=project&Project="+project+"&Category="+category;
    new Ajax(url, {
    	method: 'get',
    	update: $('Profile_Content'),
    	onRequest:function()
    	{
    		$('Profile_Content').setHTML('<center><img src="css/loading.gif" /></center>');
    	},
    	onComplete:function()
    	{
			Setup_Posts();
    	}
    }).request();
}

var page_width = document.body.offsetWidth;
var page_height = document.body.offsetHeight;

function First_Load() {
	page_width = document.body.offsetWidth;
	page_height = document.body.offsetHeight;
	
	// load nav images
	
	var images = [
		'images/menubg.png',
		'images/menubg1.png',
		'images/menubg1A.png', 
		'images/menubg1B.png', 
		'images/menubg1C.png', 
		'images/menubg1D.png'
	];
	new Asset.images(images, {
		onComplete: function() {
		}
	});
	
	$('intro_white').setStyle('top', ((page_height-120)/2));
	
	$('intro_black').setStyles({
		'top': (((page_height-120)/2)+100),
		'left': page_width
	});
	
	$('intro_white').addEvent('mousedown', function() { open(); });
	$('intro_black').addEvent('mousedown', function() { open(); });	
	$('intro_mclean').addEvent('mousedown', function() { open(); });	
	$('intro_design').addEvent('mousedown', function() { open(); });	
	
	var intro_white = new Fx.Styles($('intro_white'), { 
		duration: 1500,
	    transition: Fx.Transitions.Cubic.easeOut,
	    onComplete: function() {
	    	var intro_mclean = new Fx.Styles($('intro_mclean'), { 
				duration: 1500,
			    transition: Fx.Transitions.Cubic.easeOut
			});
			
			intro_mclean.start({
				'top': 50,
				'height': 50
			});
	    }
	});
	
	var intro_black = new Fx.Styles($('intro_black'), { 
		duration: 1500,
	    transition: Fx.Transitions.Cubic.easeOut,
	    onComplete: function() {
	    	var intro_design = new Fx.Styles($('intro_design'), { 
				duration: 1500,
			    transition: Fx.Transitions.Cubic.easeOut
			});
			
			intro_design.start({
				'top': 0
			});
	    }
	});
	
	intro_white.start({
	    'width': page_width
	});
	intro_black.start({
	    'left': 0,
	    'width': page_width
	});
	
}

function nav_run_through() {
	var the_chain = new Chain();
	the_chain.chain(function() { 
		$('nav_bg').setProperty('src','images/menubg1.png');
		$('nav_show').empty();
	});
	the_chain.chain(function() { 
		$('nav_bg').setProperty('src','images/menubg1A.png');
		$('nav_show').setHTML('<a href=\"about_us.php\">about_us</a>');
	});
	the_chain.chain(function() { 
		$('nav_bg').setProperty('src','images/menubg1B.png');
		$('nav_show').setHTML('<a href=\"portfolio.php\">portfolio</a>');
		//show('about us');
	});
	the_chain.chain(function() { 
		$('nav_bg').setProperty('src','images/menubg1C.png');
		$('nav_show').setHTML('<a href=\"knapping.php\">rocks-n-such</a>');
		//show('about us');
	});
	the_chain.chain(function() { 
		$('nav_bg').setProperty('src','images/menubg1.png');
		$('nav_show').empty();
	});
	the_chain.chain(function() { 
		ticker();
	});
	(function(){ the_chain.callChain(); }).delay(1500);
	(function(){ the_chain.callChain(); }).delay(3000);
	(function(){ the_chain.callChain(); }).delay(4500);
	(function(){ the_chain.callChain(); }).delay(6000);
	(function(){ the_chain.callChain(); }).delay(7500);
	(function(){ the_chain.callChain(); }).delay(9000);
}

function ticker() {
	var ticker_id = $('ticker');
	var tickers = ticker_id.getElements('span');
	var ticker_chain = new Chain();
	tickers.each(function(span,i) {
		ticker_chain.chain(function() {
			var span_fx_in = new Fx.Styles(span, { 
					duration: 1500,
				    transition: Fx.Transitions.Cubic.easeOut
				});
			var span_fx_out = new Fx.Styles(span, { 
					duration: 1500,
				    transition: Fx.Transitions.Cubic.easeIn
				}); 
			span_fx_in.start({
				'left': [800,0]
			}).chain(function() {
				(function(){ span_fx_out.start({'left': -1000}); }).delay(3500);
			});
		});
	});
	(function(){ ticker_chain.callChain(); }).delay(0);
	(function(){ ticker_chain.callChain(); }).delay(6000);
}

function open() {
	var intro_white = new Fx.Styles($('intro_white'), { 
			duration: 1000,
		    transition: Fx.Transitions.Cubic.easeOut,
		    onComplete: function() {
		    	var children = $('inner').getChildren();
				children.each(function(element) {
					element.setStyles({
					   opacity: 0,
					   visibility: 'hidden'
					});
				});
				var inner = new Fx.Styles($('inner'), { 
					duration: 600,
				    transition: Fx.Transitions.Cubic.easeOut
				});
				
				$('inner').setStyle('opacity',1);
				$('intro_white').remove();
				inner.start({
				    'height': 630
				}).chain(function() {
					inner.start({
				    	'height': 630
				    });
				}).chain(function() {	
					if(window.location == "http://mcleandesign.com/order.php"||window.location == 'https://mcleandesign.com/order.php') $('inner').setStyle('height','1375px');
					children.each(function(child) {
						child.setStyle('visibility', 'visible');
				    	var child_effect = child.effect('opacity', {
				    		duration: 600,
				    		onComplete: function() {
				    				nav_run_through().delay(1000);
				    			}  
				    		});
				    	child_effect.start(1);
				    });
				});	
				
				Block_Effects();
		    }
		});
	var intro_black = new Fx.Styles($('intro_black'), { 
			duration: 500,
		    transition: Fx.Transitions.Cubic.easeOut
		});
	intro_black.start({
		'height': 0
	}).chain(function() {
		$('intro_black').remove();
		intro_white.start({
			'height': 50,
			'width': 800,
			'margin-left': ((page_width-800)/2),
			'top': ((page_height-50)/2)
		});
	});
}

function Setup_Posts()
{
	var forms = $$('.Input form');
	forms.each(function(form) {
		var category_input = form.getElements('input[name=category]');
		var delete_button = form.getElements('input[class=delete]');
		var category_value = category_input.getProperty('value');
		delete_button.addEvent('mouseenter', function() {
			category_value = category_input.getProperty('value');
			category_input.setProperty('value','delete');
		});
		delete_button.addEvent('mouseleave', function() {
			category_input.setProperty('value',category_value);
		});
		form.addEvent('submit', function(e) {
			new Event(e).stop();
			/**
			 * Status Effects
			 */
			var post = form.getParent().getParent();
	 		var effect = post.effect('background-color', {duration: 600});
			var periodical;
			 
			var fx = function() {
				effect.start('#d9d9d9').chain(function() {
					effect.start('#ffffff');
				});
			}
			fx();
			periodical = fx.periodical(1300);
			
			/**
			 * send takes care of encoding and returns the Ajax instance.
			 * onComplete removes the spinner from the log.
			 */
			this.send({
			  onComplete: function(text, xml) {
			  	$clear(periodical);
			  	Evaluate_Form_Response(text,post);
			  }
			});
		});
	});
}


function Set_Tail()
{
	var tail = $('tail');
	var first_block = $('left').getFirst();
	if(first_block)  {
		var block_event = first_block.getProperty('onmouseover');
		var block_events = block_event.split("'");
		tail.onmouseover = function(event){
			Set_Right(block_events[1],block_events[3],block_events[5]);
		};
	}
}

function Block_Effects()
{
	var blocks = $('left').getChildren();
	var delay = 500;
	var delay_add = 2500 / blocks.length;
	blocks.each(function(block) {
		block.setStyle('margin-top',400);
		var block_effect = block.effect('margin-top', { duration: delay, transition: Fx.Transitions.Quad.easeOut });
		var block_effect2 = block.effect('opacity', { duration: delay, transition: Fx.Transitions.Quad.easeIn });
		(function(){block_effect.start(5);block_effect2.start(1);}).delay(delay);
		delay = delay + delay_add;
	});
	
}

function Right_Effects() {
	var right = $('right').getFirst();
	var right_position = right.getStyle('margin-left');
	var block_effect = new Fx.Styles(right, { duration: 500, transition: Fx.Transitions.Quad.easeOut });
	block_effect.start({
		'opacity': [0,1],
		'margin-left': [400,right_position]
	});
}

function comment_move(direction) {
	var comment_box_height = $('comment').getStyle('height').toInt();
	var comment_height = $('comment_helper').getStyle('height').toInt();
	var comment_margin_top = $('comment_helper').getStyle('margin-top').toInt();
	var space = Math.floor(comment_box_height*0.55);
	//alert(comment_margin_top);
	if(comment_height > comment_box_height) {
		var arrows_effect = new Fx.Style($('comment_helper'), 'margin-top', { duration: 400, transition: Fx.Transitions.linear });
		if(direction == 'up') {
			if(comment_margin_top<0) {
				arrows_effect.start(comment_margin_top+space);
			}
		}
		if(direction == 'down') {
			if(Math.abs(comment_margin_top)<(comment_height-comment_box_height)) {
				arrows_effect.start(comment_margin_top-space);
			}
		}
	}
}