	// ImageScreen Plugins


	// IS Object
	$.IS = new Object();


	// IS_load
	(function( $ ){
		$.fn.IS_load = function(options){
			var d = {
				url					: '/cached/cached_menu.html',
				prepend			: 'false'
	    };
			return this.each(function(){
	      if (options){
	        $.extend(d, options);
	      }
	      var o = this;
	      
				$.ajax({        
					type: 'POST',
					url: d.url,
					dataType: 'html',
					async: false,
					success: function(data){
						$(o).html(data);
						if (d.prepend == 'true'){
							$('ul:first', o).prepend('<li><a data-category_id="1" data-category_password="false">All</a></li>');
						}
					}
				});
		  });
		};
	})( jQuery );


	// IS_grid
	(function( $ ){	
		$.fn.IS_grid = function(options){
			var d = {
				random			: 'true',
				master			: 19,
				number			: 1
	    };
			return this.each(function(){
	      if (options){
	        $.extend(d, options);
	      }
	      var o = this;
	      
				$.ajax({        
					type: 'POST',
					url: '/php/gallery_items.php',
					dataType: 'json',
					async: false,
					data: {number: d.number, master: d.master},
					success: function(data){
											
						for (var key in data) {
						  if (data.hasOwnProperty(key)){								
								function img(){
									var rnd = Math.floor(Math.random()*49)
									var html = $('div:nth-child('+rnd+')', o).html();
	
									if (html == ''){
										$('div:nth-child('+rnd+')', o).html('<a href="#category" data-category_id="'+key+'"><img src="/content/thumbnails/'+data[key]+'" /></a>');
									}
									else {
										img();
									}
								}
								
								img();

						  }
						}
					}
				});
		  });
		};
	})( jQuery );


	// IS_menu
	(function( $ ){
		$.fn.IS_menu = function(options){
			var d = {
				animation		: 'fade',
	      speed				: 250,
	      easing			: 'swing',
	      markup			: 'a',
				markup_css	: 'menu',
	      extra				: '',
				extra_css		: '',
	      close				: 'false'
	    };
			return this.each(function(){
	      if (options){
	        $.extend(d, options);
	      }
	      var o = this;
	      
				$('> li > ul', this).hide();
				$('> li > '+d.markup+'', this).bind('click', function(e){
					if (! ($(this).hasClass('menu'))){
						$('> li > ul', o).hide();
						$('> li > '+d.markup+'', o).removeClass(d.markup_css);
						$(d.extra).parent().removeClass(d.extra_css);
						$(this).addClass(d.markup_css);
						
						if (d.animation == 'fade'){
							$(this).siblings().fadeToggle(d.speed, d.easing);
						}
						if (d.animation == 'slide'){
							$(this).siblings().slideToggle(d.speed, d.easing);
						}
					}
				});
				
				$(d.extra).bind('click', function(e){
					$(this).parent().addClass(d.extra_css);
	
					$('> li > ul', o).hide();
					$('> li > '+d.markup+'', o).removeClass(d.markup_css);
				});
		  });
		};
	})( jQuery );
	
	
	// IS_tabs
	(function( $ ){
		$.fn.IS_tabs = function(options){
			var d = {
				speed				: 200,
				direction		: 'vertical',
				markup			: '#menu a, #logo a',
				url					: 'true'
	    };
			return this.each(function(){
	      if (options){
	        $.extend(d, options);
	      }
	      var o = this;
	   
				$.IS.tabs = new Object();
	
				// build array with each page id
				$.IS.tabs.tabs = new Array();
				$('> div', o).each(function(i){
					$.IS.tabs.tab = $(this).attr('id');
					$.IS.tabs.tabs[i] = '#' + $.IS.tabs.tab;
				});
			
				$.IS.tabs.width = $('.page', o).css('width');
				$.IS.tabs.width = parseFloat($.IS.tabs.width, 10);
				$.IS.tabs.height = $('.page', o).css('height');
				$.IS.tabs.height = parseFloat($.IS.tabs.height, 10);
							
	/*
				if (window.location.hash){
					$.IS.tabs.link = window.location.hash;
					$('#menu2 a[href=' + $.IS.tabs.link + ']').addClass('menu_on');
				
					for (i=0; i<$.IS.tabs.tabs.length; i++){
						if ($.IS.tabs.link == $.IS.tabs.tabs[i]){
							$.IS.tabs.position = i;
						}
					}
					$.IS.tabs.left = 0 - ($.IS.tabs.position * $.IS.tabs.width);
					$('#slider').css("left", $.IS.tabs.left);
				}
	*/
				
				$(d.markup).bind("click", tabs_scroll);
				
				function tabs_scroll(e){
					e.preventDefault(); 
				
					$.IS.tabs.tab = $(this).attr('href');
					for (i=0; i<$.IS.tabs.tabs.length; i++){
						if ($.IS.tabs.tab == $.IS.tabs.tabs[i]){
							$.IS.tabs.position = i;
						}
					}
				
					$.IS.tabs.logo_id = $(this).parent().attr('id');
				
					if (d.direction == 'horizontal'){
						$.IS.tabs.left = 0 - ($.IS.tabs.position * $.IS.tabs.width);
						$(o).animate(
							{"left": $.IS.tabs.left}, d.speed, function(){ url(); }
						);
					}
					if (d.direction == 'vertical'){
						$.IS.tabs.top = 0 - ($.IS.tabs.position * $.IS.tabs.height);
						$(o).animate(
							{"top": $.IS.tabs.top}, d.speed, function(){ url(); }
						);				
					}
					
					return false;
				}
	
				function url(){
					if (d.url == 'true'){
						if ($.IS.tabs.logo_id != 'logo'){
							window.location.hash = $.IS.tabs.tab;
						}
						else {
							window.location.hash = $.IS.tabs.tab;
						}
					}
				}
	
		  });
		};
	})( jQuery );


	// IS_slideshow
	(function( $ ){
		$.fn.IS_slideshow = function(options){
			var d = {
				duration		: 2500,			// milliseconds
				fade				: 500,			// milliseconds
				controls		: 'true'		// true | false
	    };
			return this.each(function(){
	      if (options){
	        $.extend(d, options);
	      }
	      var o = this;
			   
				$.IS.slideshow = new Object();
				$.IS.slideshow.spos = 0;
				$.IS.slideshow.scount = 0;
							
				function start_slideshow(){
					$.IS.slideshow.timer = setInterval(show_slideshow, d.duration);
				}
				function stop_slideshow(){
					$.IS.slideshow.stop = clearInterval($.IS.slideshow.timer);
				}
				function next_slideshow(){
					$.IS.slideshow.stop = clearInterval($.IS.slideshow.timer);
				}
				
				function setup_slideshow(){
					$(o).children().each(function(index, element){
						$.IS.slideshow.scount++;
						$(this).css('position', 'absolute');
						if (index == $.IS.slideshow.spos){
							$('*', this).css('opacity', 1);
							$(this).css('z-index', 501);
						}
						if (index != $.IS.slideshow.spos){
							$('*', this).css('opacity', 0);
							$(this).css('z-index', 500);
						}
						next_slideshow();
					});
				}
				
				function position_slideshow(){
					$.IS.slideshow.spos++;
					if ($.IS.slideshow.spos >= $.IS.slideshow.scount){
						$.IS.slideshow.spos = 0;
					}
				}
				
				function next_slideshow(){
					$.IS.slideshow.snext = $.IS.slideshow.spos + 1;
					if ($.IS.slideshow.snext >= $.IS.slideshow.scount){
						$.IS.slideshow.snext = 0;
					}
				}
				
				function show_slideshow(){
					$(o).children().each(function(index, element){
						if (index == $.IS.slideshow.spos){
							$('*', this).animate({opacity: 0}, d.fade);
							$(this).css('z-index', 500);
					}
						if (index == $.IS.slideshow.snext){
							$('*', this).animate({opacity: 1}, d.fade);
							$(this).css('z-index', 501);
						}
					});
					position_slideshow();
					next_slideshow();
				}
				
				$('#slideshow_controls_stop').bind('click touchstart', function(event){
						stop_slideshow();
						if (!$(this).hasClass('control_stop')){
							$(this).toggleClass('control_stop');
							$('#slideshow_controls_next').toggleClass('control_next');
						}
				});
				
				$('#slideshow_controls_next').bind('click touchstart', function(event){
						show_slideshow();
						stop_slideshow();
						start_slideshow();
						if (!$(this).hasClass('control_next')){
							$(this).toggleClass('control_next');
							$('#slideshow_controls_stop').toggleClass('control_stop');
						}
				});
						
				function slideshow_load(){
					$.ajax({        
						type: 'POST',
						url: 'php/slideshow_load.php',
						async: false,
						success: function(data){
							$(o).html(data);
						}
					});
					setup_slideshow();
					start_slideshow();
				}
				
				slideshow_load();
					
		  });
		};
	})( jQuery );





	// IS_category
	(function( $ ){
	
	  var methods = {
	     init : function( options ) {
	
					var d = {
						duration		: 2500,			// milliseconds
						fade				: 500,			// milliseconds
						controls		: 'true',		// true | false
						category_id	: '1'				// value
			    };
			    
					return this.each(function(){
			      if (options){
			        $.extend(d, options);
			      }
			      var o = this;
			
						$.IS.category = new Object();
						$.IS.category.spos = 0;
						$.IS.category.scount = 0;


						description_load();
						
						function description_load(){
							$.ajax({
								type: 'POST',
								url: 'php/category_description.php',
								data: {category_id: d.category_id},
								success: function(data){
									$('#category_description').html(data);
								}
							});
						}
					
						
						slideshow_load();
						
						function slideshow_load(){
							$.ajax({
								type: 'POST',
								url: 'php/category_load.php',
								data: {category_id: d.category_id},
								success: function(data){
									$(o).html(data);
									slideshow_setup();
								}
							});
						}
						
						
						function slideshow_setup(){
							$(o).children().each(function(index, element){
							
								$.IS.category.scount++;
								$(this).css('position', 'absolute');
								
								if (index == $.IS.category.spos){
									$('*', this).css('opacity', 1);
									$(this).css('z-index', 501);
								}
								
								if (index != $.IS.category.spos){
									$('*', this).css('opacity', 0);
									$(this).css('z-index', 500);
								}
							});
						}


						$('#slideshow_controls_next').unbind().bind('click touchstart', function(event){
							event.preventDefault();
							$.IS.category.spos += 1;
							if ($.IS.category.spos >= $.IS.category.scount){
								$.IS.category.spos = 0;
							}
							slideshow_change();
						});
						
						
						$('#slideshow_controls_prev').unbind().bind('click touchstart', function(event){
							event.preventDefault();
							$.IS.category.spos -= 1;
							if ($.IS.category.spos <= -1){
								$.IS.category.spos = $.IS.category.scount - 1;
							}
							slideshow_change();
						});
						
						
						function slideshow_change(){
							$(o).children().each(function(index, element){
								var css = $(this).css('z-index');
								
								if (css == 501){
									$('*', this).animate({opacity: 0}, d.fade);
									$(this).css('z-index', 500);
								}
								if (index == $.IS.category.spos){
									$('*', this).animate({opacity: 1}, d.fade);
									$(this).css('z-index', 501);
								}
							});
						}


						// IS_category - controls
						if (d.controls == 'true'){
							$('#category_pages').children().hide();
							$('#category_images .image_description').hide();
							
							$('#category_pages').bind('mouseenter', function(){
								$(this).children().show();
								$('#category_images .image_description').show();
							});
							
							$('#category_pages').bind('mouseleave', function(){
								$(this).children().hide();
								$('#category_images .image_description').hide();
							});
						}
						
						if (d.controls == 'false'){
							$('#category_pages').children().hide();
							$('#category_pages').unbind();
							
							$('#category_images .image_description').hide();
						}

							
				  });
		
				},
			update : function( ) {
	
				return this.each(function(){
					//delete $.IS.category;
				})
	
			}
	  };
	
	  $.fn.IS_category = function(method){
	    
	    if ( methods[method] ) {
	      return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
	    } else if ( typeof method === 'object' || ! method ) {
	      return methods.init.apply( this, arguments );
	    } else {
	      $.error( 'Method ' +  method + ' does not exist on jQuery.IS_category' );
	    }    
	  
	  };
	
	})( jQuery );
