/*
	functions.js
	Author: 		James Cooke
	Description:	Core styles, generic container classes, grid changes
	Client:			Sunset Sounds (The PRODUCT)
*/

var weekday=new Array(7);
weekday[0]="Sun";
weekday[1]="Mon";
weekday[2]="Tue";
weekday[3]="Wed";
weekday[4]="Thu";
weekday[5]="Fri";
weekday[6]="Sat";

var month=new Array(12);
month[1]="Jan";
month[2]="Feb";
month[3]="Mar";
month[4]="Apr";
month[5]="May";
month[6]="Jun";
month[7]="Jul";
month[8]="Aug";
month[9]="Sep";
month[0]="Oct";
month[11]="Nov";
month[12]="Dec";

var width;

var $quicksandPreferences = {
		duration: 800,
		easing: 'easeInOutQuad',
		attribute: 'id',
		useScaling: true,
		adjustHeight: 'dynamic'
	};

	$(function () {

		/*$("a[rel*=external]").click(function () {
		window.open(this.href);
		return false;
		});*/

		// Module Lineup-home
		$(".module-lineup-home li").each(function () {
			var offset = $(this).position()
			if (offset.left == 0) {
				// This is the first item of the row, so add a class to the prev LI
				$(this).prev().addClass("no-bg");
			}
		});

		// Cufon
		if ($("#ribbon").length == 0) {
			Cufon.replace('h1, h2, .carousel-caption p, .module-lineup-home li a, .form-holding label, .cufon, .validation-summary-errors span, .event-date', { fontFamily: 'helveticaneue', hover: true });
			Cufon.replace('.module-nav ul li a:not(ul ul li a)', { hover: true });
		}


		// Setup lightwindows
		$(".lightwindow").fancybox({});
		$(".lightwindow-ajax").fancybox({
			autoDimensions: true,
			padding: 0,
			titleShow: false,
			onComplete: function () {
				Cufon.replace(".popup h2");
			}
		});
		$(".lightwindow-iframe").fancybox({
			autoDimensions: true,
			padding: 0,
			titleShow: false,
			type: "iframe"
		});
		$(".lightwindow-map").fancybox({
			autoDimensions: true,
			padding: 0,
			titleShow: true,
			onComplete: function () {
				setupMap();
			}
		});

		// Setup forms
		$("select").uniform();

		// make the stars the same size as the body
		setupBG();

		// Setup the sub-nav
		$(".module-nav ul").superfish({
			delay: 1000,
			autoArrows: false,
			pathClass: "hover",
			hoverClass: "hover"
		});

		$(".module-nav ul ul li:has(ul)").addClass("has-children");


		// Setup content slider
		$('.module-carousel').nivoSlider({
			effect: 'fade',
			animSpeed: 800,
			pauseTime: 7000,
			slices: 1,
			directionNav: false
		});



		$("input.clear-input").clearText();

		// Module Twitter
		$('.feed-twitter').jTweetsAnywhere({
			username: _tweeterUserName,
			//searchParams: ['q=%40sunsetsounds'],
			//searchParams: ['q=sunsetsounds'],
			tweetProfileImagePresent: true,
			count: 4,
			tweetTimestampDecorator: function (tweet, options) {
				var dateString = tweet.created_at;
				dateString = dateString.substr(0, dateString.length - 11);
				return '<p class="twitter-date">' + dateString + '</p>';
			}
		});


		$.fn.sorted = function (customOptions) {
			var options = {
				reversed: false,
				by: function (a) { return a.text(); }
			};
			$.extend(options, customOptions);
			$data = $(this);
			arr = $data.get();
			arr.sort(function (a, b) {
				var valA = options.by($(a));
				var valB = options.by($(b));
				if (options.reversed) {
					return (valA < valB) ? 1 : (valA > valB) ? -1 : 0;
				} else {
					return (valA < valB) ? -1 : (valA > valB) ? 1 : 0;
				}
			});
			return $(arr);
		};


		// Module tab slider
		var tabsWidth = 0;
		$('.tab-slider li').each(function () {
			tabsWidth += $(this).width();
		});

		//tabsWidth -= $('.tab-slider').width();

		if (tabsWidth < $(".tab-slider").width()) {
			$(".tab-nav").hide();
		}

		// Initial tab start

		$('.tab-nav a').each(function () {
			var direction = $(this).attr("rel");

			$(this).click(function () {
				if (direction == "prev") {
					var firstTab = $(".tab-slider ul").position();
					firstTab = firstTab.left;
					if (firstTab != 0) {
						$('.tab-slider ul').animate({
							left: '+=150'
						}, { queue: true, duration: 300, easing: "easeInQuart" });
					}
				} else {
					$('.tab-slider ul').animate({
						left: '-=150'
					}, { queue: true, duration: 300, easing: "easeInQuart" });
				}
			});
		});

		// Filter
		setupFilter();

	});  

$(window).resize(setupBG);

function setupBG(){
	if($(window).height() > $("#container").height()){
		$(".body-stars").css({height : $(window).height()});
	} else{
		$(".body-stars").css({height : $("#container").height()});
	}
}

function setupMap() {
    var address = '';
    if ($('#HiddenLocality').val() !== '') {
        address = $('#HiddenVenueName').val() + ' : ' + $('#HiddenStreetAddress').val() + ($('#HiddenStreetAddress').val() !== "" ? ", " : "") + $('#HiddenLocality').val() + " " + $('#HiddenState').val() + " " + $('#HiddenPostcode').val();
    }
    else {
        address = $('#HiddenVenueName').val() + ' : ' + $('#HiddenStreetAddress').val() + ", " + $('#HiddenState').val() + " " + $('#HiddenPostcode').val();
    }
   // $('#mapDialog').dialog('option', 'title', address);
   // $('#mapDialog').dialog('open');
   // $('#mapDialog').dialog('option', address);
    iniGoogleMap();
    return false;
}

function setupFilter(){


	/* PROGRAM
	var $programFilter = $('select#filter-category');
	$(".module-program .category-all").quicksand($(".category-source li"), $quicksandPreferences);
	
	$programFilter.change(function(e) {
		$(this).filterPage($programFilter, "quicksand", true);
    });*/
	

	// LINEUP
	var $lineupFilter = $('.form-filter-lineup select.filter-type');  
	
	$(".module-lineup .category-all").quicksand($(".category-source li"), $quicksandPreferences);
	
	$lineupFilter.change(function(e) {
		$(this).filterPage($lineupFilter, "quicksand");
    });
}

$.fn.filterPage = function(filter, method, pagination){
	
	if(method == "standard"){
		if (filter.val() == 'category-all') {
			$('.module-program .category-all li').slideDown();
			// Rebuild pagination
			
		} else{
			$('.module-program .category-all li').not('[rel=' + filter.val() +']').fadeOut(1000);
			$('.module-program .category-all li[rel*=' + filter.val() +']').fadeIn(1000);
			
		}
		
		if ($(filter).val() == 'category-all') {
			$('.category-all').quicksand($('.category-source li'), $quicksandPreferences);
		} else {
			$('.category-all').quicksand($('.category-source li[rel*=' + $(filter).val() +']'), $quicksandPreferences);
		}
		
	} else if(method == "quicksand"){
		if ($(filter).val() == 'category-all') {
			$('.category-all').quicksand($('.category-source li'), $quicksandPreferences);
		} else{
			$('.category-all').quicksand($('.category-source li[rel*=' + $(filter).val() +']'), $quicksandPreferences);
		}
	}
}

// Clear default inputs
$.fn.clearText = function(){
	return this.focus(function() {
		if( this.value == this.defaultValue ) {
			this.value = "";
		}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
		}
	});
}



$.fn.equalHeights = function(minHeight, maxHeight) {
	tallest = (minHeight) ? minHeight : 0;
	this.each(function() {
		if($(this).height() > tallest) {
			tallest = $(this).height();
		}
	});
	if((maxHeight) && tallest > maxHeight) tallest = maxHeight;
	return this.each(function() {
		$(this).height(tallest).css("overflow","auto");
	});
}
