$(document).ready(function(){ 

	var edit_one = 0;
	var current = 1;
	var pathname = window.location.pathname;
	var browser = navigator.userAgent;


	$('#bg'+current).fadeIn();
	$("#content1 .full_image img:first").fadeIn();
	$("#content1 .title_block .image_title:first").fadeIn();
	$("#content2 .full_image img:first").fadeIn();
	$("#content2 .title_block .image_title:first").fadeIn();
	$("#content3 .full_image img:first").fadeIn();
	$("#content3 .title_block .image_title:first").fadeIn();
	$("#content4 .full_image img:first").fadeIn();
	$("#content4 .title_block .image_title:first").fadeIn();
	$(".content:first").fadeIn();
	$(".video_content:first").fadeIn();

	$('#home_content a').click(function(){
		$('#home').fadeOut();
	});

	$('.video_content').each(function(){
		video_id = (this).id;
		if (browser.match('iPhone') || browser.match('iPad') ) {
			$('#'+video_id).html('<video src="/files/'+video_id+'.mp4" width="480" height="360" controls poster="/files/'+video_id+'.jpg" type="video/mp4"></video>');
		} else {
			var flashvars = {width: 480,height: 384,file: '/files/'+video_id+'.mp4',image: '/files/'+video_id+'.jpg',autostart: false};
			var params = {allowfullscreen:'true',wmode:'transparent',allowscriptaccess:'always' };
			var attributes = {'name': 'player'};
			swfobject.embedSWF("js/player.swf", 'vid'+video_id, "480", "384", "9.0.0", '', flashvars, params, attributes);
		}
	});	
		

	$("#menu a").click(function(){
		$('#menu a').removeClass('active');
		$(this).addClass('active');
		$('#bg'+current).fadeOut();
		$('#content'+current).fadeOut();
		$('#bg'+(this).id).fadeIn();
		$('#content'+(this).id).fadeIn();
		current = (this).id;
	});

	$('.athumb').hover(
		function() {
			if (edit_one == 0) {
				$('.bigimage').fadeOut();
				$('.image_title').fadeOut();
				$('.video_content').fadeOut();
				$('#big'+(this).id+'.bigimage').fadeIn();
				$('#title'+(this).id).fadeIn();
				$('#'+(this).id+'.video_content').fadeIn();

				if (pathname.match('admin')) {
					$(this).append($('<DIV class="edit_button"></div><DIV class="delete_button"></div>'));
					$('.edit_button').bind('click', function() {
						edit_one = $(this).parent().get(0).id;
						edit_title_block(edit_one, $(this).parent().parent().get(0).id);
						edit_image(edit_one);
					});
					$('.delete_button').bind('click', function() {
						delete_image($(this).parent().get(0).id,$(this).parent().parent().get(0).id);
					});
				}
			}
		},
		function() {
			if (pathname.match('admin')) {
				if (edit_one == 0) {
					$(this).find("div:last").remove();
				}
			}
		}
	);


	$('.thumbs').hover(
		function() {
			if ($(this).children().size() == 0) {
				if (edit_one == 0) {
					$('#content'+current+' .bigimage').fadeOut();
					$('#content'+current+' .image_title').fadeOut();
					$('#content'+current+' .video_content').fadeOut();
					$(this).append($('<DIV class="add_button"></div>'));
					$('.add_button').bind('click', function() {
						edit_one = $(this).id;
						edit_image($(this).parent().get(0).id);
					});
				}
			}
		},
		function() {
			if (edit_one == 0) {
				$(this).find("div:last").remove();
			}
		}
	);

	function edit_image(position) {
		if (current == 4) {
			$('#content'+current+' .full_image').append($('<DIV ID="grayout"></div><DIV ID="edit_image"><DIV ID="cancelbox"><A ID="cancel_edit">Cancel</A></div><div class="upload_box"><P>Click the "Browse" button to select a new movie.  The ideal size is 480px wide by 320px high.  Other sizes will display with black bands.</P><DIV ID="upload"></div></div></div>'));
		} else {
			$('#content'+current+' .full_image').append($('<DIV ID="grayout"></div><DIV ID="edit_image"><DIV ID="cancelbox"><A ID="cancel_edit">Cancel</A></div><div class="upload_box"><P>Click the "Browse" button to select a new image.  If your image does not measure exactly 378px wide by 474px tall it will be resized.</P><DIV ID="upload"></div></div></div>'));
		}
		$('#cancel_edit').click(function() {
			cancel_edit_image();
		});
		$("#upload").uploadify({
			'uploader'       : '../js/uploadify/uploadify.swf',
			'script'         : '/functions/upload.php',
			'cancelImg'      : 'images/cancel.png',
			'folder'         : '/files',
			'auto'           : true,
			'scriptData'     : {'position':position,'type':current},
			'onComplete'     : function(event, ID, fileObj, response, data) {
				load_image(response,current,position);
			}
		});
	}

	function load_image(image_id,current,position) {
		$('#content'+current+' #'+position).html('<A CLASS="athumb" ID="'+image_id+'"><IMG SRC="/files/'+image_id+'_thumb.jpg"></A>');
		$('#content'+current+' .title_block').append('<DIV CLASS="image_title" ID="title'+image_id+'"></div>');
		if (current == 4) {
			$('#content'+current+' .full_image').append('<div class="video_content" id="'+image_id+'"></div>');
			if (browser.match('iPhone') || browser.match('iPad') ) {
				$('#content4 #'+image_id).html('<video src="/files/'+image_id+'.mp4" width="480" height="360" controls poster="/files/'+image_id+'.jpg" type="video/mp4"></video>');
			} else {
				var flashvars = {width: 480,height: 384,file: '/files/'+image_id+'.mp4',image: '/files/'+image_id+'.jpg',autostart: false};
				var params = {allowfullscreen:'true',wmode:'transparent',allowscriptaccess:'always' };
				var attributes = {'name': 'player'};
				swfobject.embedSWF("js/player.swf", image_id, "480", "384", "9.0.0", '', flashvars, params, attributes);
			}
			$('#content4 #'+image_id).fadeIn();
		} else {
			$('#content'+current+' .full_image').append('<IMG SRC="/files/'+image_id+'.jpg" class="bigimage" ID="big'+image_id+'">');
			$('#big'+(this).id+'.bigimage').fadeIn();
		}

		$('#'+image_id+'.athumb').hover(
			function() {
				if (edit_one == 0) {
					$('.bigimage').fadeOut();
					$('.image_title').fadeOut();
					$('.video_content').fadeOut();
					$('#'+image_id+'.video_content').fadeIn();
					$('#big'+(this).id+'.bigimage').fadeIn();
					$('#title'+(this).id).fadeIn();
					$(this).append($('<DIV class="edit_button"></div><DIV class="delete_button"></div>'));
					$('.edit_button').bind('click', function() {
						edit_one = $(this).parent().get(0).id;
						edit_title_block(edit_one, $(this).parent().parent().get(0).id);
						edit_image(edit_one);
					});
					$('.delete_button').bind('click', function() {
						delete_image($(this).parent().get(0).id,$(this).parent().parent().get(0).id);
					});
				}
			},
			function() {
				if (pathname.match('admin')) {
					if (edit_one == 0) {
						$(this).find("div:last").remove();
					}
				}
			}
		);
		$('#big'+image_id+'.bigimage').fadeIn();
		cancel_edit_image();
	}


	function cancel_edit_image() {
		$('#grayout').remove();
		$('#edit_image').remove();
		$('.edit_button').remove();
		$('.delete_button').remove();
		$('.add_button').remove();
		edit_one = 0;
	}

	function delete_image(id,pos) {
	    $.ajax({
	        type: "GET",
	        url: "/functions/delete_image.php?image_id="+id,
		    error: function(){ alert('Error loading delete document'); },
	        success: function(a) {
				$('#content'+current+' #'+pos).html('');
				$('#content'+current+'.full_image').html('');
				$('#title'+id).html('');
	        }
	    });
	}

	function edit_title_block(id) {
	    $.ajax({
	        type: "GET",
	        url: "/functions/edit_title_block.php?image_id="+id,
		    error: function(){ alert('Error loading title document'); },
	        success: function(a) {
				$('#title'+id).fadeOut();
				$('#content'+current+' .title_block').append(a);
				$('#title_block_form #update').click(function() {
					var data = $("#title_block_form").serialize();
				    $.ajax({
				        type: "POST",
				        url: "/functions/edit_title_block.php",
				        dataType: "text",
				        data: data,
					    error: function(){ alert('Error loading document'); },
				        success: function(a) {
							$('#content'+current+' #title'+id).html(a);
							$('#title'+id).fadeIn();
							$('.title_block form').remove();
							cancel_edit_image();
				        }
				    });
				});
	        }
	    });
	}



});

