/*Mantis Creative Group*/
/*2011*/

(function($){ 
    $.fn.extend({
		
		center: function() {
			
			//our total width
			var totalWidth = 960;
			
			//the current width of our object
			var curWidth = this.width();
			
			//The amount of margin to be added on the left
			var marginLeft = (totalWidth - curWidth) / 2;
			var marginLeft = Math.round(marginLeft);
			
			this.css({ 'margin-left': marginLeft });
			
		},
		
		mcgSlide: function(wait) {
			
			//main container & ul container
			var mainCont = this;
			var ulCont = this.children('ul');
			
			//gets the height of our UL container
			var ulHeight = mainCont.children('ul').height();
			
			//sets our maincontainer height to show our absolutely positioned ul container
			mainCont.css({ height: (ulHeight + 20) });
			
			//our initial state slide
			mcgIsSlidePlaying = 0;
			
			//if wait is defined auto-play
			if(wait) {
				
				var autoPlay = setInterval(function() {
					
					mainCont.mcgNextSlide();
					
				}, wait);
				
			}
			
		},
		
		mcgNextSlide: function() {
			
			//if our slide is NOT playing then play it
			if(mcgIsSlidePlaying === 0) {
			
				//main container & main container width & ul container object
				var mainCont = this;
				var mainContWidth = this.width();
				var ulCont = this.children('ul');
				
				//our initial ul width
				var ulWidth = 0;
				
				mainCont.children('ul').children('li').each(function() {
					
					//sums each of our lis width
					ulWidth = ulWidth + (jQuery(this).width());
					
					//lets check if it has the last class
					var thisClass = jQuery(this).attr('class').split(' ');
					
					if(thisClass[0] != 'last' && thisClass[1] != 'last') {
						
						ulWidth = ulWidth + 20;
						
					}
					
				});
				
				//the total of slides possible
				var totalSlides = Math.ceil((ulWidth / mainContWidth));
				
				//calculates which slide we currently are
				var curSlide = (Math.abs(parseInt(ulCont.css('left'))) + 920) / 920;
				
				//if the current slide is not greater than the total of slides possible
				if(curSlide < totalSlides) {
					
					//gets our current left and adds 920 more
					var curLeft = Math.abs(parseInt(ulCont.css('left')));
					var newLeft = curLeft + 920;
					
					//our slide is playing now
					mcgIsSlidePlaying = 1;
					
					//animates our UL
					ulCont.stop().animate({ opacity: 0 }, 500, function() {
						
						ulCont.css({ left: '-'+newLeft+'px' }).animate({ opacity: 1 }, 500, function() {
							
							mcgIsSlidePlaying = 0;
							
						});
						
					});
					
					
				}
				// if our current slide is greater or equals the total of slides
				else {
					
					//our slide is playing now
					mcgIsSlidePlaying = 1;
					
					//animates our UL
					ulCont.stop().animate({ opacity: 0 }, 500, function() {
						
						ulCont.css({ left: 0 }).animate({ opacity: 1 }, 500, function() {
							
							mcgIsSlidePlaying = 0;
							
						});
						
					});
					
				}
			
			}
			
		},
		
		mcgPrevSlide: function() {
			
			//if our slide is NOT playing then play it
			if(mcgIsSlidePlaying === 0) {
			
				//main container & main container width & ul container object
				var mainCont = this;
				var mainContWidth = this.width();
				var ulCont = this.children('ul');
				
				//our initial ul width
				var ulWidth = 0;
				
				mainCont.children('ul').children('li').each(function() {
					
					//sums each of our lis width
					ulWidth = ulWidth + (jQuery(this).width());
					
					//lets check if it has the last class
					var thisClass = jQuery(this).attr('class').split(' ');
					
					if(thisClass[0] != 'last' && thisClass[1] != 'last') {
						
						ulWidth = ulWidth + 20;
						
					}
					
				});
				
				//the total of slides possible
				var totalSlides = Math.ceil((ulWidth / mainContWidth));
				
				//calculates which slide we currently are
				var curSlide = (Math.abs(parseInt(ulCont.css('left'))) + 920) / 920;
				
				//if the current slide is not greater than the total of slides possible
				if(curSlide != 1) {
					
					//gets our current left and adds 920 more
					var curLeft = Math.abs(parseInt(ulCont.css('left')));
					var newLeft = curLeft - 920;
					
					//our slide is playing now
					mcgIsSlidePlaying = 1;
					
					//animates our UL
					ulCont.stop().animate({ opacity: 0 }, 500, function() {
						
						ulCont.css({ left: '-'+newLeft+'px' }).animate({ opacity: 1 }, 500, function() {
							
							mcgIsSlidePlaying = 0;
							
						});
						
					});
					
					
				}
				// if our current slide is greater or equals the total of slides
				else {
					
					//our new left
					var newLeft = (totalSlides - 1) * 920;
					
					//our slide is playing now
					mcgIsSlidePlaying = 1;
					
					//animates our UL
					ulCont.stop().animate({ opacity: 0 }, 500, function() {
						
						ulCont.css({ left: '-'+newLeft+'px' }).animate({ opacity: 1 }, 500, function() {
							
							mcgIsSlidePlaying = 0;
							
						});
						
					});
					
				}
			
			}
			
		},
		
		mcgTabbed: function() {
			
			isTabbedPlaying = 0;
			
			//our main containers
			var mainCont = this;
			var tabsCont = this.children('ul.tabs');
			var tabbedCont = this.children('ul.tabbed');
			
			//adds our sorting classes to tabs
			var i = 1;
			tabsCont.children('li').each(function() {
				
				jQuery(this).addClass('tab_'+i);
				
				i++;
				
			});
			
			//adds our sorting classes to tabbed content
			var i2 = 1;
			tabbedCont.children('li').each(function() {
				
				jQuery(this).addClass('tabbed_'+i2);
				
				i2++;
				
			});
			
		},
		
		callMcgTabbed: function(num, title, menu) {
			
			if(isTabbedPlaying === 0) {
				
				//main vars
				var mainCont = this;
				var tabbedCont = this.children('ul.tabbed');
				
				//sets our menu as the current
				jQuery('#menu ul li.current').removeClass('current');
				menu.parent().addClass('current');
				
				//if our first one is not set
				if(mainCont.parent().children('h3.title').text() != '') {
				
					//sets up the title and 'cufons it'
					mainCont.parent().children('h3.title').stop().animate({ opacity: 0 }, 250, function() {
						
						jQuery(this).html(title);
					
						//CUFON
						Cufon.replace('.title', {
							
							hover: true,
							textShadow: '#fff 1px 1px'
			
						});
						
						jQuery(this).animate({ opacity: 1 }, 250);
						
					});
				
				} else {
					
					mainCont.parent().children('h3.title').html(title);
					
						//CUFON
						Cufon.replace('.title', {
							
							hover: true,
							textShadow: '#fff 1px 1px'
			
						});
					
				}
				
				if(tabbedCont.children('li.current').length === 0) {
					
					isTabbedPlaying = 1;
					
					//current doesnt exist lets show it :)
					tabbedCont.children('li.tabbed_'+num).stop(true, true).fadeIn(250,function() { isTabbedPlaying = 0; }).addClass('current');
					
				} else {
					
					isTabbedPlaying = 1;
					
					//now we have a current item :)
					tabbedCont.children('li.current').fadeOut(250, function() {
						
						//shows our next one
						tabbedCont.children('li.tabbed_'+num).fadeIn(250, function() {
							
							tabbedCont.children('li.current').removeClass('current');
							jQuery(this).addClass('current');
							isTabbedPlaying = 0;
							
						});
						
					})
					
				}
				
			}
			
		},
		
		mcgSort: function() {
			
			//containers objects
			var mainCont = this;
			var sortCont = this.children('.sort');
			var sortableCont = this.children('.sortable');
			
			//adds the current class to the first sortable item and calls the rest
			sortCont.children('li:first').addClass('current');
			
			var thisCat = sortCont.children('li.current').attr('class').split(' ');
			
			mainCont.sortSortable(thisCat[0]);
			
			
			//var to make sure its not playing stacked
			isPlayingSort = 0;
			
			//when we click one of the buttons
			sortCont.children('li').click(function() {
				
				//if its not playing
				if(isPlayingSort === 0) {
					
					//adds current class
					sortCont.children('li.current').removeClass('current');
					jQuery(this).addClass('current');
					
					//amakes sure we dont overplay it
					isPlayingSort = 1;
					
					var thisCat = jQuery(this).attr('class').split(' ');
			
					mainCont.sortSortable(thisCat[0]);
					
				}
				
			});
			
			//hover effects
			sortableCont.children('div').hover(function() {
				
				//fades out all other items
				sortableCont.children('div').children('.image').stop().animate({ opacity: .5 }, 200);
				jQuery(this).children('.image').stop().animate({ opacity: 1 }, 200);
				
				//animates our tooltip
				jQuery(this).children('.sortTooltip').css({ display: 'block', opacity: 0, bottom: '120px' }).stop().animate({ opacity: 1, bottom: '140px' }, 200);
				
			}, function() {
				
				//fades all of the mback in again
				sortableCont.children('div').children('.image').stop().animate({ opacity: 1 }, 200);
				
				//animates back our tooltip
				jQuery(this).children('.sortTooltip').stop().animate({ opacity: 0, bottom: '150px' }, 200, function() {
					
					jQuery(this).hide();
					
				});
				
			});
			
		},
		
		sortSortable: function(cat) {
			
			//containers objects
			var mainCont = this;
			var sortCont = this.children('ul.sort');
			var sortableCont = this.children('.sortable');
			
			//if our category is set first
			if(cat) {
				
				//fadesout our curretn shown ones
				sortableCont.children('div').children('.image').stop().animate({ opacity: 0 }, 150, function() {
					
					//removes the one-third class from them
					sortableCont.children('div').removeClass('one-third');
				
					jQuery(this).css({ display: 'none' });
				
					//if its 'all'
					if(cat == 'all') {
						
						//lets get all our lis and add the last class to every thid image
						var i = 1;
						sortableCont.children('div').each(function() {
							
							jQuery(this).removeClass('last').addClass('one-third');
							if((i % 3) == 0) { jQuery(this).addClass('last') }
							i++;
							
						});
						
						//show all our items
						sortableCont.children('div').children('.image').each(function() {
							
							jQuery(this).css({ display: 'block', opacity: 0 }).stop().animate({ opacity: 1 }, 200, function() {
								
								isPlayingSort = 0;
								
							});
							
						});
						
						sortableCont.children('div:first').children('.image').css({ opacity: 1, display: 'block' });
						
					} else {
						
						sortableCont.children('div').children('.image').hide();
						
						//lets get our lis with the cats and add the last class to every third image
						var i = 1;
						sortableCont.children('div.'+cat).each(function() {
							
							jQuery(this).removeClass('last').addClass('one-third');
							if((i % 3) == 0) { jQuery(this).addClass('last') }
							i++;
							
						});
						
						//show all our cat items
						sortableCont.children('div.'+cat).children('.image').each(function() {
							
							jQuery(this).css({ display: 'block', opacity: 0 }).stop().animate({ opacity: 1 }, 200, function() {
								
								isPlayingSort = 0;
								
							});
							
						});
						
						sortableCont.children('div.'+cat+':first').children('.image').css({ opacity: 1, display: 'block' });
						
					}
					
				});
				
			}
			
		},
		
		mcgScrollTo: function(anc) {
			
			//troiggers our event
			this.click(function(el) {
			
				// prevent hard jumping
				el.preventDefault();
				
				//our target
				var target = jQuery(this).attr("href");
				
				//if its just the hash
				if(target == '#') {
				
					//animates our page
					jQuery('html,body').stop().animate({ scrollTop: 0 }, 500, function() {
						
						//adds the hash to our url
						location.hash = target;
						
					});
					
				} else {
				
					//animates our page
					jQuery('html,body').stop().animate({ scrollTop: jQuery(target).offset().top }, 500, function() {
						
						//adds the hash to our url
						location.hash = target;
						
					});
				
				}
			 	
			});
			
		},
		
		mcgSuperMenu: function() {
			
			//lets tart by setting the width of the submenu depending on the quantity of lis
			var totalLi = this.children('ul:first').children('li').length;
			this.children('ul:first').css({ width: ((totalLi * 220) - 40)+'px' });
			
			this.children('ul:first').children('li:last').css({ margin: 0, padding: 0, background: 'none' });
			
			var mainCont = this;
			
			this.hover(function() {
				
				jQuery(this).addClass('hovered');
				jQuery(this).children('.supermenu').stop(true, true).slideDown(250);
				
			}, function() {
				
				jQuery(this).removeClass('hovered');
				jQuery(this).children('.supermenu').stop(true, true).slideUp(250);
				
			});
			
		}
		
	});
	
})(jQuery);
