$(document).ready(function(){

	//-------------- Memeber panel
	$('#trigger').animPanel({
		target: '#member_login',
		topStart: '0px',
		topEnd: '-111px',
		textOpen: 'Close',
		textClose: 'Member Login'
	});
	  
	if ($('#main_nav').length) {
		//-------------- Main Menu
		ddsmoothmenu.init({
			mainmenuid		: 'main_nav', 
			orientation		: 'h', 
			contentsource	: 'markup'
		})
	}
 
	$('#main_nav ul li ul').hover(function(){
		$(this).parent('li').addClass('active');
	}, function() {
		$(this).parent('li').removeClass('active');
	});
 
	//------------- Main Menu corners	
	DD_roundies.addRule('#main_nav ul li ul', '0 5px 5px 5px' , true);
	DD_roundies.addRule('#feature_slider', '7px' , true);	
	//DD_roundies.addRule('.news_tabs li a', '5px 5px 0 0', true);	
	DD_roundies.addRule('#subNav li ul', '10px' , true);	
	DD_roundies.addRule('#header_image, #map_wrapper', '10px' , true);
	DD_roundies.addRule('#user_menu', '0 0 5px 5px' , true);
	DD_roundies.addRule('#events_info, table.form', '5px' , true);


	//------------- Home page slider
	$(".scrollable").scrollable({
		size: 1, 
		vertical: true,
		clickable: false,
		items: ".items"
	}).navigator({ 
        navi: "#feature_menu", 
        naviItem: 'a', 
        activeClass: 'current' 
    }); 	
	$('#feature_menu li a').eq(0).addClass('current');
	
	// Galleries created with snippet
	$("#slideshowWrapper").scrollable({
		size: 1,
		clickable: false,
		circular: true,
		items: "#gallery_items",
		easing: 'custom',
		speed: 2000
	}).autoscroll({ autoplay: true, interval: 4000 });	
	
	//---------- News tabs
	$("ul.news_tabs").tabs("div.news_panes > div",{
		effect: 'default'
	});

	$("ul.vid_tabs").tabs("div.news_panes > div",{
		effect: 'default'
	});	
	
	//Faq tabs
	$("ul.faq_tabs").tabs("div.panes > div");	
	
	// Shortlist tabs
	$("ul.shortlist_tabs li a").click( function() { 
	
		// Because we have multiple tab sets, make sure others are hidden when we click on another.
		$("div.shortlist_panes").hide();
		$(this).parents('div.shortlist').children("div.shortlist_panes").show(); 		
		
		return true; 
	});
	$("ul.shortlist_tabs").tabs("div.shortlist_panes > div", { initialIndex: 0, effect: 'fade' } );

	//Pagination fix
	( !$('a.ditto_page').length ) ? $('span.ditto_currentpage').css('display','none') : '';
	
	//Twitter
	$('ul.twitter_update_list li span').each(function() { 
		var str = $(this).text().substring(0, 120);		
		$(this).html(str+'...');	
	});
	$('ul.twitter_update_list li a').attr('target','_blank');
	
	// Stripe tables
	$('table tbody tr:odd').addClass('odd');
	$('table tbody tr:even').addClass('even'); 


	//--------- Newsletter form
	$("#subForm").validate({
		 rules: { 
	      "cm-name": { 
			  required: true,
			  checkForDefaultVale: true 
	      },
		  "cm-oithru-oithru": {
			  required: true,
			  email: true
		  }
	    }, 
		messages: { 
 	      "cm-name": "Please enter a your full name",
		  "cm-oithru-oithru": "Please enter your email address"
 	    },
		submitHandler: function(form) { sumbitForm(form.id); }
	});

	
	//Job Form
	/*
	var status = $("input[name='posting_type']").val();
	alert(status);
	if (status == 'featured_posting') {
		$('tr.logo').show();
	} else {
		$('tr.logo').hide();
	}
	
	$("input[name='posting_type']").click( function() {
	  var data = $(this).val();
	  if (data == 'featured_posting') {
	 	$('tr.logo').show();
	  } else {
		$('tr.logo').hide();
	  }

	});
*/
	
	//Membership form
	$('tr.hide_cv').hide();
    $('#experience').change(function() {
        if ($("#experience").val() == 3) {
            $('tr.hide_cv').show();
        } else {
            $('tr.hide_cv').hide();
        }
   });
   
   /* ,
		onSelect: function(dateText, inst) {
			Date.prototype.toString = function () {return isNaN (this) ? 'NaN' : [this.getDate(), this.getMonth(), this.getFullYear()].join('/')}
			d = new Date(dateText);
			getJsonDate(d.getFullYear(), d.getMonth()+1);
		},
		onChangeMonthYear: function(year, month, inst) {
			//alert(year);
			//alert(month);
			getJsonDate(year, month);
		}
		*/

		// Member detail form		
		if ($('form#memberdetailform input#photo').val() != '')
			$('form#memberdetailform img#member_photo').hide();
		else
			$('form#memberdetailform img#member_photo').show();
		$('form#memberdetailform input#photo').change(function()
		{
			if ($(this).val() != '')
				$('form#memberdetailform img#member_photo').hide();
			else
				$('form#memberdetailform img#member_photo').show();
		});

	tinyMCE.init({
		mode : "exact",
		elements : "v_overview",
		theme : "advanced",
		plugins : "paste",		
		paste_auto_cleanup_on_paste : true,
		paste_strip_class_attributes: "all",
		paste_remove_spans: true,
		paste_remove_styles: true,
		theme_advanced_toolbar_location : "top",
		theme_advanced_buttons1 : "bold,italic,underline,|,justifyleft,justifycenter,justifyright,|,bullist,numlist,|,link,unlink,|,sub,sup,|,undo,redo,|,removeformat",
		theme_advanced_buttons2 : "",
		theme_advanced_buttons3 : ""
	});
	
   	tinyMCE.init({
		mode : "exact",
		elements : "profile_description",
		theme : "advanced",
		plugins : "paste",		
		paste_auto_cleanup_on_paste : true,
		paste_strip_class_attributes: "all",
		paste_remove_spans: true,
		paste_remove_styles: true,
		theme_advanced_toolbar_location : "top",
		theme_advanced_buttons1 : "bold,italic,underline,|,justifyleft,justifycenter,justifyright,|,bullist,numlist,|,link,unlink,|,sub,sup,|,undo,redo,|,removeformat",
		theme_advanced_buttons2 : "",
		theme_advanced_buttons3 : ""
	});
	
	tinyMCE.init({
		mode : "exact",
		elements : "forum_message",
		theme : "advanced",
		plugins : "paste",		
		paste_auto_cleanup_on_paste : true,
		paste_strip_class_attributes: "all",
		paste_remove_spans: true,
		paste_remove_styles: true,
		theme_advanced_toolbar_location : "top",
		theme_advanced_buttons1 : "bold,italic,underline,|,justifyleft,justifycenter,justifyright,|,bullist,numlist,|,link,unlink,|,sub,sup,|,undo,redo,|,removeformat",
		theme_advanced_buttons2 : "",
		theme_advanced_buttons3 : ""
	});
	
}); //Close document.ready

$(window).bind("load", function() {								
	//Slideshows
	$('.loading').each(function () {
		$(this).hide();
	});
	$('#slideshowWrapper').fadeIn('slow');

	
});

/*------- Equal hights ---------------------------------------------------------*/
function equalHeight(group) {
	tallest = 0;
	group.each(function() {
		thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	group.height(tallest);
}
/*------- Toggle animation (open - close) --------------------------------------*/
(function($) {
	$.fn.animPanel = function(options) {
		var opts = $.extend({}, $.fn.animPanel.defaults, options);
		
		return this.each(function() {
			$this = $(this);
			var o = $.meta ? $.extend({}, opts, $this.data()) : opts;				
			$this.toggle(function() {
				$(o.target).css('z-index','9999');
				$(o.target).animate({ top: o.topStart }, 300, "easeOutBounce", function() { 
					$(o.target).find('#trigger').html(o.textOpen);
					
				});						   
			},function() {
				$(o.target).animate({ top: o.topEnd }, 300, "easeOutBounce", function() { 
					$(o.target).find('#trigger').html(o.textClose);
					$(o.target).css('z-index','99');
				});						   	
			})	
			
		});
	};
})(jQuery);
/*------ Easing animations -----------------------------------------------------*/
(function($) {
	$.easing.custom = function (x, t, b, c, d) { 
		var s = 1.70158;  
		if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b; 
		return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b; 
	}
	$.easing.easeOutExpo = function (x, t, b, c, d) {
		return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
	};
	$.easing.easeOutBounce = function (x, t, b, c, d) {
		if ((t/=d) < (1/2.75)) {
			return c*(7.5625*t*t) + b;
		} else if (t < (2/2.75)) {
			return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
		} else if (t < (2.5/2.75)) {
			return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
		} else {
			return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
		}
	};
})(jQuery);
/*----- Custom alert popup -----------------------------------------------------*/
function my_alert(alert_content) {
	$('body').append('<div id="alert">'+alert_content+'<br /> <span class="close">Close</span></div>');
	$("#alert").overlay({ 
		top: 200, 
		expose: { 
			color: '#fff', 
			loadSpeed: 500, 
			opacity: 0.6 
		}, 
		closeOnClick: true,
		api: true 
	}).load();
}

$(function(){
	function getJsonDate(year, month) {
		$.getJSON('index.php?id=376&year='+year+'&month='+month, function(data) {
			var i = 0;
			for (i = 0; i < data.data.length; i++)
			{
				var sub = (data.data[i]['userSubmission']  == 0) ? '#f00' : '#6BBB46';
				$('.ui-datepicker-calendar td a:exactly('+data.data[i]['d']+')').css({color: sub}).attr('href',data.data[i]['link']).parent().attr('onclick','');	
			}
		});
	}
	$.expr[":"].exactly = function(el, i, m) {
		var s = m[3];
		if (!s) return false;
		return eval("/^" + s + "$/i").test($(el).text());
	};
	$('#datepicker').datepicker({
		inline: true,
		onSelect: function(dateText, inst) {
			Date.prototype.toString = function () {return isNaN (this) ? 'NaN' : [this.getDate(), this.getMonth(), this.getFullYear()].join('/')}
			d = new Date(dateText);
			getJsonDate(d.getFullYear(), d.getMonth()+1);
		},
		onChangeMonthYear: function(year, month, inst) {
			//alert(year);
			//alert(month);
			getJsonDate(year, month);
		}
	});
	
	// Post event form date pickers.
	$( "input.select_date" ).datepicker({
		inline: true,
		dateFormat: 'dd-mm-yy' 
	});
	
	$('#twitter_update_list a').each(function() {
		$(this).attr('target','_blank');
	});
	
});

/*------- Validation additonal methods --------------------------------------------------*/
$.validator.addMethod( "checkForDefaultVale", function(value, element) { 
	data = (element.value == "your name") ? false : true; 
	return data;
});
function sumbitForm(formId) {
	 data = $('#'+formId).serialize();
	 formAction = $('#'+formId).attr("action");
	 url = 'assets/snippets/campaignMonitor/proxy.php';
	 final = data + "&action=" + formAction;
     $.ajax({
      type: "POST",    
      url: url,
      data: final,
      success: function(msg){
        $('.newsletter_data').html("<span class='error'>Thank you for subscribing to the Fundraising Ireland newsletter.<br />You'll receive the next edition by email soon.</span>");
		
      }
    });

    return false;
}

$.easing.custom = function (x, t, b, c, d) {
	var s = 0.70158; 
	if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
	return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
}

