/* SETUP GALLERIES
*************************************************************/
function SetupGalleries(){
	var self = this;
	self.gallery_url = '/t/get_gallery.php';
	
	self.init = function(){
		$('#design').unbind('click').click(function(){
			$('#about_box').show();
			$('#temp_content').remove();
			self.get_gallery('design');
			return false;
		});
		$('#animation').unbind('click').click(function(){
			$('#about_box').show();
			$('#temp_content').remove();
			self.get_gallery('animation');
			return false;
		});
		$('#live_action').unbind('click').click(function(){
			$('#about_box').show();
			$('#temp_content').remove();
			self.get_gallery('live_action');
			return false;
		});
	}
	
	self.get_gallery = function(gallery_type){
		$('#viewer_window').load(self.gallery_url,{ "type": gallery_type }, function(){
			$('#viewer_window').removeClass('video');
			self.setup_gallery();
		});
	}
	self.setup_gallery = function(){
		$('.gallery_item.image').unbind('click').click(function(){
			self.load_gallery($(this).children('.gallery_meta').html(), $(this).children('.gallery_images').clone(true));
			return false;
		});
		
		$('.gallery_item.video').unbind('click').click(function(){
			var video_num = $(this).attr('rel');
			$('#about_box').hide();
			$('#about_box').parent().append('<div id="temp_content"></div>');
			$('#temp_content').html($(this).find('.gallery_meta').html());
			$('#viewer_window').addClass('video');
			$('#viewer_window').html('<iframe src="http://player.vimeo.com/video/'+video_num+'" width="495" height="279" frameborder="0"></iframe>');
			return false;
		});
	}
	
	self.load_gallery = function(title, gallery_images){
		self.close_overlay();
		self.setup_overlay();
		$('#pop_content').html(gallery_images);
		$('#pop_container').append("<div id='pop_title'>"+title+"</div>");
		var image_count = "1 of " + $('#pop_content .gallery_images img').length;
		$('#pop_title span').html(image_count);
		
		var height='460px';
		var width=480;
		$('#pop_container').css('width',(width + 96) + 'px');
		$('#pop_content').css('width',width + 'px');
		$('#pop_content .gallery_images').css('width',width+ 'px');
		$('#pop_content .gallery_images').css('height',height);
		
		$('#pop_left a').hide();
		$('#pop_right a').hide();
		if($('#pop_content .gallery_images img').length > 1){
			$('#pop_right a').show();
		}
		
		$('#pop_left a').unbind('click').click(function(){
			self.prev_image();
			return false;
		});
		$('#pop_right a').unbind('click').click(function(){
			self.next_image();
			return false;
		});
		
		$('#pop_content .gallery_images').show();
		$('#pop_content .gallery_images img:eq(0)').show();
	}
	self.prev_image = function(){
		var current_image = $('#pop_content .gallery_images img:visible').index();
		var total_images = $('#pop_content .gallery_images img').length -1;
		
		$('#pop_right a').show();
		
		if(current_image == 0){
			$('#pop_left a').hide();
		} else {
			if(current_image > 0){
				$('#pop_content .gallery_images img:eq('+current_image+')').hide();
				$('#pop_content .gallery_images img:eq('+(current_image-1)+')').show();
			}
			if((current_image-1) == 0){
				$('#pop_left a').hide();
			}
		}	
		
		var image_count = current_image + " of " + $('#pop_content .gallery_images img').length;
		$('#pop_title span').html(image_count);
	}
	self.next_image = function(){
		var current_image = $('#pop_content .gallery_images img:visible').index();
		var total_images = $('#pop_content .gallery_images img').length -1;
		if(current_image < total_images){
			$('#pop_content .gallery_images img:eq('+current_image+')').hide();
			$('#pop_content .gallery_images img:eq('+(current_image+1)+')').show();
		}
		$('#pop_left a').show();
		if((current_image+1) == total_images){
			$('#pop_right a').hide();
		}
		
		var image_count = (current_image+2) + " of " + $('#pop_content .gallery_images img').length;
		$('#pop_title span').html(image_count);
	}
	
	self.setup_overlay = function(){
		$('#shell').append("<div id='pop_over'><div id='pop_container'><a id='pop_close' href='#'>X</a><div id='pop_left'><a href='#'>&lt;</a></div><div id='pop_content'></div><div id='pop_right'><a href='#'>&gt;</a></div></div></div>");
		$('#pop_over').unbind('click').click(function(){
			self.close_overlay();
			return false;
		});
		$('#pop_close').unbind('click').click(function(){
			self.close_overlay();
			return false;
		});
	}
	self.close_overlay = function(){
		$('#pop_over').remove();
	}
	self.init();
}

var setup_galleries = new SetupGalleries();

/* SETUP NEWS
*************************************************************/
function SetupNews(){
	var self = this;
	self.news_url = '/t/get_news.php';
	
	self.init = function(){
		$('#news').unbind('click').click(function(){
			$('#about_box').show();
			$('#temp_content').remove();
			self.get_news();
			return false;
		});
	}
	
	self.get_news = function(){
		$('#viewer_window').load(self.news_url, function(){
			$('#viewer_window').removeClass('video');
			self.setup_gallery();
		});
	}
	self.setup_gallery = function(){
		$('#viewer_window .article_images a').unbind('click').click(function(){
			self.load_gallery($(this).attr('rel'), $(this).parent().parent().children('.article_gallery').clone(true), $(this).index());
			return false;
		});
	}
	
	self.load_gallery = function(title, gallery_images, active){
		self.close_overlay();
		self.setup_overlay();
		$('#pop_content').html(gallery_images);
		$('#pop_container').append("<div id='pop_title'>"+title+" <span></span></div>");
		var total_images = $('#pop_content .gallery_images img').length;
		var image_count = (active + 1) + " of " + total_images;
		$('#pop_title span').html(image_count);
		
		var height='460px';
		var width=480;
		$('#pop_container').css('width',(width + 96) + 'px');
		$('#pop_content').css('width',width + 'px');
		$('#pop_content .gallery_images').css('width',width+ 'px');
		$('#pop_content .gallery_images').css('height',height);
		
		$('#pop_left a').hide();
		$('#pop_right a').hide();
		if($('#pop_content .gallery_images img').length > 1 && active != (total_images-1)){
			$('#pop_right a').show();
		}
		if(active > 0){
			$('#pop_left a').show();
		}
		
		$('#pop_left a').unbind('click').click(function(){
			self.prev_image();
			return false;
		});
		$('#pop_right a').unbind('click').click(function(){
			self.next_image();
			return false;
		});
		
		$('#pop_content .gallery_images').show();
		$('#pop_content .gallery_images img:eq('+active+')').show();
	}
	self.prev_image = function(){
		var current_image = $('#pop_content .gallery_images img:visible').index();
		var total_images = $('#pop_content .gallery_images img').length -1;
		
		$('#pop_right a').show();
		
		if(current_image == 0){
			$('#pop_left a').hide();
		} else {
			if(current_image > 0){
				$('#pop_content .gallery_images img:eq('+current_image+')').hide();
				$('#pop_content .gallery_images img:eq('+(current_image-1)+')').show();
			}
			if((current_image-1) == 0){
				$('#pop_left a').hide();
			}
		}	
		
		var image_count = current_image + " of " + $('#pop_content .gallery_images img').length;
		$('#pop_title span').html(image_count);
	}
	self.next_image = function(){
		var current_image = $('#pop_content .gallery_images img:visible').index();
		var total_images = $('#pop_content .gallery_images img').length -1;
		if(current_image < total_images){
			$('#pop_content .gallery_images img:eq('+current_image+')').hide();
			$('#pop_content .gallery_images img:eq('+(current_image+1)+')').show();
		}
		$('#pop_left a').show();
		if((current_image+1) == total_images){
			$('#pop_right a').hide();
		}
		
		var image_count = (current_image+2) + " of " + $('#pop_content .gallery_images img').length;
		$('#pop_title span').html(image_count);
	}
	
	self.setup_overlay = function(){
		$('#shell').append("<div id='pop_over'><div id='pop_container'><a id='pop_close' href='#'>X</a><div id='pop_left'><a href='#'>&lt;</a></div><div id='pop_content'></div><div id='pop_right'><a href='#'>&gt;</a></div></div></div>");
		$('#pop_over').unbind('click').click(function(){
			self.close_overlay();
			return false;
		});
		$('#pop_close').unbind('click').click(function(){
			self.close_overlay();
			return false;
		});
	}
	self.close_overlay = function(){
		$('#pop_over').remove();
	}

	self.init();
}

//var setup_news = new SetupNews();

/* SETUP CONTACT
*************************************************************/
function SetupContact(){
	var self = this;
	self.contact_url = '/t/get_contacts.php';
	
	self.init = function(){
		$('#contact').unbind('click').click(function(){
			$('#about_box').show();
			$('#temp_content').remove();
			self.get_contact();
			return false;
		});
	}
	
	self.get_contact = function(){
		$('#viewer_window').load(self.contact_url, function(){
			$('#viewer_window').removeClass('video');
		});
	}

	self.init();
}

var setup_contact = new SetupContact();

/* SETUP ABOUT
*************************************************************/
function SetupAbout(){
	var self = this;
	self.about_url = '/t/get_about.php';
	
	self.init = function(){
		$('#about h2').unbind('click').click(function(){
			$('#about_box').show();
			$('#temp_content').remove();
			self.get_about();
			return false;
		});
		$('#about_box').unbind('click').click(function(){
			$('#about_box').show();
			$('#temp_content').remove();
			self.get_about();
			return false;
		});
	}
	
	self.get_about = function(){
		$('#viewer_window').load(self.about_url, function(){
			$('#viewer_window').removeClass('video');
		});
	}

	self.init();
}

var setup_about = new SetupAbout();

/* SETUP QUOTE
*************************************************************/
function SetupQuote(){
	var self = this;
	self.quote_url = '/t/get_quote.php';
	
	self.init = function(){
		$('#get_quote').unbind('click').click(function(){
			$('#about_box').show();
			$('#temp_content').remove();
			self.get_quote();
			return false;
		});
	}
	
	self.get_quote = function(){
		$('#viewer_window').load(self.quote_url, function(){
			$('#viewer_window').removeClass('video');
			self.setup_quote_form();
		});
	}
	
	self.setup_quote_form = function(){
		$('#quote_form').submit(function(){
			var content_vars = $('#quote_form').serialize();
			$.post(self.quote_url,content_vars,function(data){
				$('#viewer_window').html(data);
				self.setup_quote_form();
			});
			return false;
		});
	}

	self.init();
}

var setup_quote = new SetupQuote();
		
/* DOCUMENT READY FUNCTION
*************************************************************/
$(document).ready(function(){
	
});
