/* --------------------------------------------------------------------
 
    PROJECT NAME
 
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    Description: 		Javascript File
    Name: 				jquery.functions.js
    Browser Version:	N/A
    Version: 			1.0
    Date: 				
    Last Modified:		
 
    This file is part of a site built by Advanced Arts
    See: http://www.advancedarts.ca
    Copyright 2011 Advanced Arts. All Rights Reserved.
    
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

   --------------------------------------------------------------------- */

/* jQUERY FUNCTIONS
  --------------------------------------------------------------------- */

$(function(){
	
	// Make sure the three buckets on the homepage have equal heights
	if ($('#home-content aside').length){
		var $biggestHeight = 0;
		var $mainColumns = $('#home-content aside');
		
		$mainColumns.each(function(i){
			var $this = $(this);
			var $currentHeight = $this.height();
			
			if ($currentHeight > $biggestHeight){
				$biggestHeight = $currentHeight;
			}
			
			if (i == 2){
				$mainColumns.height($biggestHeight);
			}
		});
	}
	
	if ($('#slideshow').length){
		
		$("#flowpanes").scrollable({ circular: true, mousewheel: true }).autoscroll({ autoplay: true, interval: 10000 }).navigator({

				// select #flowtabs to be used as navigator
				navi: "#flowtabs",

				// select A tags inside the navigator to work as items (not direct children)
				naviItem: 'a',

				// assign "current" class name for the active A tag inside navigator
				activeClass: 'current',

				// make browser's back button work
				history: true

			});
	}
	
	// Teams page
	if ($('#teams').length){
		$("#teams aside img").reflect({height:40,opacity:0.2});

		var $biggestHeight = 0;
		var $mainColumns = $('#teams aside .overview');
		
		$mainColumns.each(function(i){
			var $this = $(this);
			var $currentHeight = $this.height();
			
			if ($currentHeight > $biggestHeight){
				$biggestHeight = $currentHeight;
			}
			
			if (i == ($mainColumns.length -1)){
				$mainColumns.height($biggestHeight);
			}
		});
	}
	
	
	// contact form
	if ($('#contact-content').length){
		$('#contact-form').validate();
	}
	
	// Bounce Page
	if ($('#reply-form').length){
		$('#reply-form').validate();
	
		$('.reply .reply-btn').click(function(){
			$('#reply-form').slideToggle();
			return false;
		});
	}
	if ($('#tweets').length){
		$.Juitter.start({
				searchType:"fromUser", // needed, you can use "searchWord", "fromUser", "toUser"
				searchObject:"jammisports", // needed, you can insert a username here or a word to be searched for, if you wish multiple search, separate the words by comma.

				// The values below will overwrite the ones on the Juitter default configuration. 
				// They are optional here.
				// I'm changing here as a example only
				lang:"en", // restricts the search by the given language
				live:"live-60", // the number after "live-" indicates the time in seconds to wait before request the Twitter API for updates.
				placeHolder:"tweets", // Set a place holder DIV which will receive the list of tweets example <div id="juitterContainer"></div>
				loadMSG: "Loading Tweets...", // Loading message, if you want to show an image, fill it with "image/gif" and go to the next variable to set which image you want to use on 
				imgName: "loader.gif", // Loading image, to enable it, go to the loadMSG var above and change it to "image/gif"
				total: 3, // number of tweets to be show - max 100
				readMore: "", // read more message to be show after the tweet content
				nameUser:"", // insert "image" to show avatar of "text" to show the name of the user that sent the tweet 
				openExternalLinks:"newWindow", // here you can choose how to open link to external websites, "newWindow" or "sameWindow"
		        filter:"sex->*BAD word*,porn->*BAD word*,fuck->*BAD word*,shit->*BAD word*"  // insert the words you want to hide from the tweets followed by what you want to show instead example: "sex->censured" or "porn->BLOCKED WORD" you can define as many as you want, if you don't want to replace the word, simply remove it, just add the words you want separated like this "porn,sex,fuck"... Be aware that the tweets will still be showed, only the bad words will be removed
			});
	}
	
	// two column pages
	if ($('.two-cols').length){
		$('.two-cols').columnize({width:400,lastNeverTallest: true});
	}
	
	// Profile slideshows
	if ($('.scrollable').length){
		$(".scrollable").scrollable();
		
		$("a#hd-image").fancybox();
		
		$(".items img").click(function() {
			var $this = $(this);
			// see if same thumb is being clicked
			if ($this.hasClass("active")) { return; }
			
			var $hdUrl = $this.attr("data-hd-image");
			// calclulate large image's URL based on the thumbnail URL
			//var $url = $this.attr("src").replace("-thumb", "-main");
			var $url = $this.attr("data-lg-image");
			// get handle to element that wraps the image and make it semi-transparent
			var $wrap = $("#large-image").fadeTo("medium", 0.5);

			// the large image
			var img = new Image();


			// call this function after it's loaded
			img.onload = function() {
				// make wrapper fully visible
				$wrap.fadeTo("fast", 1);

				// change the image
				$("#large-image").attr("src", $url);
				$("#hd-image").attr("href", $hdUrl);

			};

			// begin loading the image
			img.src = $url;

			// activate item
			$(".items img").removeClass("active");
			$this.addClass("active");

		// when page loads simulate a "click" on the first image
		}).filter(":first").click();
	}
	
	// Schedule Filtering
	if ($('#date-picker').length){
		function removeDupedH2(){
			$('h2.date').each(function(){
				var $this = $(this);
				if ($this.text() == $this.prevAll('h2.date').html()){
					$this.remove();
				}
			});
		}
		removeDupedH2();
		
		$(":date").dateinput({ trigger: true, format: 'mmmm dd, yyyy', min: '2011-01-01' });
		$(':date:last').val($(':date:last').data("dateinput").getValue('mmmm dd, yyyy'));

		// use the same callback for two different events. possible with bind
		$(":date").bind("onShow onHide", function()  {
			$(this).parent().toggleClass("active"); 
		});

		// when first date input is changed
		$(":date:first").data("dateinput").change(function() {
			var $year = $(':date:first').data("dateinput").getValue('yyyy');
			var $month = parseInt($(':date:first').data("dateinput").getValue('mm'));
			var $day = parseInt($(':date:first').data("dateinput").getValue('dd'));
			var $setMin = $year + '-' + $month + '-' + ($day+1);

			// we use it's value for the seconds input min option
			if (($(':date:last').data("dateinput").getValue('yyyy') < $year) || (parseInt($(':date:last').data("dateinput").getValue('mm')) < $month) || (parseInt($(':date:last').data("dateinput").getValue('dd')) <= $day)){
				if ($month >= parseInt($(':date:last').data("dateinput").getValue('mm')) && parseInt($(':date:last').data("dateinput").getValue('dd')) <= $day){
					$(":date:last").data("dateinput").setValue(7);
					$(":date:last").data("dateinput").setMin($setMin, true);
				}else if ($(':date:last').data("dateinput").getValue('yyyy') < $year){
					$(":date:last").data("dateinput").setValue(7);
					$(":date:last").data("dateinput").setMin($setMin, true);
				}else if ($(':date:last').data("dateinput").getValue('yyyy') == $year){
					$(":date:last").data("dateinput").setMin($setMin, true);
				}else{
					
				} 
			}else{
				if ($(':date:last').data("dateinput").getValue('yyyy') >= $year){
					$(":date:last").data("dateinput").setMin($setMin, true);
				}
			}
		});

		$(":date").change(function(){
		// $('.calweek a').live('click',function(){
			var $endDate = $(':date:last');
			var $startDate = $(':date:first');
			var $startDateVal= $startDate.data("dateinput").getValue('yyyy-mm-dd');;
			var $endYear = $endDate.data("dateinput").getValue('yyyy');
			var $endMonth = parseInt($endDate.data("dateinput").getValue('mm'));
			var $endDay = parseInt($endDate.data("dateinput").getValue('dd'));
			var $endDateVal = $endYear + '-' + $endMonth + '-' + ($endDay+1);

			var $url = '/schedule/schedule-picker?startdate=' + $startDateVal + '&enddate=' + $endDateVal + '&schedule=true/';
			var $loader = $('<div/>',{
					id: 'loader',
					css: {
							position: 'absolute', 
							left: '215px',
							top: '163px',
							width: '550px',
							height: '15px',
							background: 'url(../assets/images/ajax-loader.gif) repeat-x 0px 0px'
						}
				});
			$('#generic .container').prepend($loader);
			$('#schedule-container').load($url,function(){
				$('#generic .container #loader').fadeOut(300,function(){$(this).remove();});
				removeDupedH2();
			});
		});
	}
	
	// Images Masonry plugin
	if ($('.gallery').length){
		var $totalImages = $('#thumb-container .thumb-item img').length;
		var $currentImage = 0;
		var $container = $('#thumb-container');
		
		// $('a[rel="game-gallery"]').fancybox();
		
		$('#thumb-container .thumb-item img').each(function(i){
			$(this).load(function(){
				$currentImage = $currentImage + 1;
				// if ($currentImage == $totalImages){
					$container.masonry({
						itemSelector : '.thumb-item',
						columnWidth : 230
					});
				// }
			});
		});
	}

	/*****
	 * Shared validation classes (must be defined after all other jquery)
	 *****/
	if ($('form').length){
		
		$('.isRequired').each(function(){
			$(this).rules("add", {
				required: true,
				messages: {required: "This field is required."}
			});
		});	
		$('.emailRequired').each(function(){
			$(this).rules("add", {
				required: true,
				email: true,
				messages: {
					required: "Please enter your email address.",
					email: "Please enter a VALID email address (e.g. name@domain.com)"
				}
			});
		});	
		$('.numRequired').each(function(){
			$(this).rules("add", {
				required: true,
				digits: true,
				messages: {
					required: "This field is required.",
					digits: "Please enter a numerical value only."
				}
			});
		});
		$('.zipRequired').each(function(){
			$(this).rules("add", {
				required: true,
				digits: true,
			    minlength: 5,
			    maxlength: 5,
				messages: {
					required: "This field is required.",
					minlength: "Please enter a valid zip code.",
					maxlength: "Please enter a valid zip code.",
					digits: "Please enter a valid zip code."
				}
			});
		});
	}
});  
