//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//slider
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
jQuery.fn.accessNews = function(settings) {
    settings = jQuery.extend({
        newsSpeed: "normal"
    }, settings);
    return this.each(function(i) {
        aNewsSlider.itemWidth = parseInt(jQuery(".item:eq(" + i + ")",".slider").css("width")) + parseInt(jQuery(".item:eq(" + i + ")",".slider").css("margin-right"));
        aNewsSlider.init(settings,this);
        jQuery(".view_all > a", this).click(function() {
            aNewsSlider.vAll(settings,this);
            return false;
        });
    });
};
var aNewsSlider = {
    itemWidth: 0,
    init: function(s,p) {
        jQuery(".messaging",p).css("display","none");
        itemLength = jQuery(".item",p).length;
        if (jQuery(".view_all",p).width() == null) {
            //jQuery(".items",p).prepend("<p class='view_all'>" + s.newsHeadline + " [ " + itemLength + " total ] &nbsp;-&nbsp; <a href='#'>View All</a></p>");
        }
        newsContainerWidth = itemLength * aNewsSlider.itemWidth;
        jQuery(".container",p).css("width",newsContainerWidth + "px");
        jQuery(".next",p).css("display","block");
        animating = false;
        jQuery(".next",p).click(function() {
            if (animating == false) {
                animating = true;
                animateLeft = parseInt(jQuery(".container",p).css("left")) - (aNewsSlider.itemWidth * 2);
                if (animateLeft + parseInt(jQuery(".container",p).css("width")) > 0) {
                    jQuery(".prev",p).css("display","block");
                    jQuery(".container",p).animate({left: animateLeft}, s.newsSpeed, function() {
                        jQuery(this).css("left",animateLeft);
                        if (parseInt(jQuery(".container",p).css("left")) + parseInt(jQuery(".container",p).css("width")) <= aNewsSlider.itemWidth * 2) {
                            jQuery(".next",p).css("display","none");
                        }
                        animating = false;
                    });
                } else {
                    animating = false;
                }
            }
            return false;
        });
        jQuery(".prev",p).click(function() {
            if (animating == false) {
                animating = true;
                animateLeft = parseInt(jQuery(".container",p).css("left")) + (aNewsSlider.itemWidth * 2);
                if ((animateLeft + parseInt(jQuery(".container",p).css("width"))) <= parseInt(jQuery(".container",p).css("width"))) {
                    jQuery(".next",p).css("display","block");
                    jQuery(".container",p).animate({left: animateLeft}, s.newsSpeed, function() {
                        jQuery(this).css("left",animateLeft);
                        if (parseInt(jQuery(".container",p).css("left")) == 0) {
                            jQuery(".prev",p).css("display","none");
                        }
                        animating = false;
                    });
                } else {
                    animating = false;
                }
            }
            return false;
        });
    },
    vAll: function(s,p) {
        var o = p;
        while (p) {
            p = p.parentNode;
            if (jQuery(p).attr("class") != undefined && jQuery(p).attr("class").indexOf("slider") != -1) {
                break;
            }
        }
        if (jQuery(o).text().indexOf("View All") != -1) {
            jQuery(".next",p).css("display","none");
            jQuery(".prev",p).css("display","none");
            jQuery(o).text("");
            jQuery(".container",p).css("left","0px").css("width",aNewsSlider.itemWidth * 2 + "px");
        } else {
            jQuery(o).text("View All");
            aNewsSlider.init(s,p);
        }
    }
};
$(function() {
    $(".slider").accessNews({
        newsSpeed:"slow"
    });
});

/******************************
 *
 *	Activity-list
 *
 ********/


$(function(){

	$('.activity-list ul').hide(0);
	$('.activity-list li a.cat-name').css('cursor', 'pointer');

	$('.activity-list li a.cat-name').click(function()
	{

		var
			el = this,
			node = $(this).next();

		$('.activity-list li a.cat-name').each(function(){
			$(this).removeClass('expanded');
		});

		$('.activity-list ul').each(function(){
			if( $(this).is(':visible') && $(this).get(0) != $(node).get(0) )
				$(this).slideUp('slow');
		});

		$(node).is(':hidden') ?
		$(node).slideDown('slow', function(){ $(el).addClass('expanded') }) :
		$(node).slideUp('slow', function(){ $(el).removeClass('expanded') }) ;

		return false;
	});
});

/******************************
 *
 *	packages-list
 *
 ********

$(function(){

	$('#package-list ul').hide(0);
	$('#package-list li a').css('cursor', 'pointer');

	$('#package-list li a').click(function()
	{

		var
			el = this,
			node = $(this).next();

		$('#package-list li a').each(function(){
			$(this).removeClass('expanded');
		});

		$('#package-list ul').each(function(){
			if( $(this).is(':visible') && $(this).get(0) != $(node).get(0) )
				$(this).slideUp('slow');
		});

		$(node).is(':hidden') ?
		$(node).slideDown('slow', function(){ $(el).addClass('expanded') }) :
		$(node).slideUp('slow', function(){ $(el).removeClass('expanded') }) ;
		 return false;
	});
} );
*/

$(function() {
	$('#package-list ul').hide(0);
	$('#package-list li a.package-category').css('cursor', 'pointer');

	$('#package-list li a.package-category').click(function()
	{

		var
			el = this,
			node = $(this).next();

		$('#package-list li a.package-category').each(function(){
			$(this).removeClass('expanded');
		});

		$('#package-list ul').each(function(){
			if( $(this).is(':visible') && $(this).get(0) != $(node).get(0) )
				$(this).slideUp('slow');
		});

		$(node).is(':hidden') ?
		$(node).slideDown('slow', function(){ $(el).addClass('expanded') }) :
		$(node).slideUp('slow', function(){ $(el).removeClass('expanded') }) ;

		return false;
	});
});

$(function() {
	$('ul#package-list li ul li a').livequery('click',function( e ){
		e.preventDefault();
  		$('ul#package-list li ul li a').removeClass('selected');
  		$(this).addClass('selected');
		$("#package-intro").hide();


		var price = $(this).children('span').text();

		var venue = $(this).attr('href').split('/forVenue/')[1].split('/')[0];

		$.ajax({
			type:'GET',
			url:$(this).attr('href'),
			data:'_xsl=/xsl/json.xsl',
			dataType:'json',
			success:function(a,b) {
				pData = a.root.packages_details.content.package;
				var sTime = pData.session_start.split(":");
				var fTime = pData.session_end.split(":");

				display_s_hour = sTime[0];
				display_f_hour = fTime[0];

				var sSuffix = "am";
				var fSuffix = "am";

				if(parseInt(sTime[0]) > 12) {
					var sSuffix = "pm";
				}

				if(parseInt(fTime[0]) > 12) {
					var fSuffix = "pm";
				}

				if(parseInt(sTime[0]) > 12) {
					display_s_hour = parseInt(sTime[0]) - 12;
				}

				if(parseInt(fTime[0]) > 12) {
					display_f_hour = parseInt(fTime[0]) - 12;
				}

				disp_time = display_s_hour+":"+sTime[1]+" "+sSuffix+" - "+display_f_hour+":"+fTime[1]+" "+fSuffix;

				$("div#package-header h3").empty().append(pData.title+"<br />"+pData.session_name+", "+disp_time);
				$("div#package-header p").empty().append(price);

				$("div#package-details > p").empty().append(pData.description);

				var href = $("div#package-header a").attr('href');
				tmp = href.split('forPackage=');
				$("div#package-header a").attr('href',tmp[0] + 'forPackage='+pData.id+'&zone='+pData.zones_id+'&venue='+venue);
			}
		});

		//scroll up to top
		return false;
	});
});

/******************************
 *
 *	Short cut to package list
 *
 ********

$(function() {
	$("a[@href='#venue-packages']").click(function() {
		$("a[@href='#tabs-container-2']").trigger('click');
		return false;
	});
});

/******************************
 *
 *	Send message
 *
 ********/

$(function() {

	$("form#contactVenue").submit(function() {

		var msgBody = $("form#contactVenue textarea[name='message_body']").val();

		var alertMsg = "";

  	  	if(document.contactVenue.contact_name.value == "") {
		 	alertMsg = alertMsg + "Please provide your name\n";
  		}

  		if(document.contactVenue.contact_email.value == "") {
		 	alertMsg = alertMsg + "Please provide your email address\n";
  		}

  		if(msgBody == "") {
		 	alertMsg = alertMsg + "Please do not send an empty message\n";
  		}

  		if(alertMsg != "") {
  			alert(alertMsg);
  			return false;
  		}

		var d = new Date();
		//var recipient =

		var SPTitle = $("#venue-details h3 a").text();


		//var queryString = "create=true&sender=&recipient="+recipient+"&title="+(document.contactVenue.message_body.value)+"&content="+msgBody+"&state=n/a&messagetype=Enquiry&senton="+(d.toGMTString())+"&lang=en";

		$.ajax({
			type:'POST',
			url:'/Messages/SendEmail/',
			data:$(this).serialize(),
			complete:function(a,b) {
				if(b == 'success') {
					$("form#contactVenue").parent().prepend("<p>Your message has been sent</p>");
					//console.log(a);
				} else {
					alert("Failed to Send");
				}
			}

		});
		return false;
	})

});

$(function() {

  	$("form#ContactVenueBook").submit(function() {

  		var alertMsg = "";

  		if(document.ContactVenueBook.Cname.value == "") {
		 	alertMsg = alertMsg + "Please provide your name\n";
  		}

  		if(document.ContactVenueBook.Ctel.value == "" && ContactVenueBook.Cemail) {
  			alertMsg = alertMsg + "Please provide a way for the venue owner to contact you (email or telephone)\n";
  		}

  		if(document.ContactVenueBook.Cdate.value == "") {
			alertMsg = alertMsg + "Please provide the date on which you wish to visit this venue\n";
  		}

  		if(alertMsg != "") {
  			alert(alertMsg);
  			return false;
  		}

  		var d = new Date();

  		var SPTitle = $("#venue-details h3 a").text();

  		var brand_unid = $("meta[name='brand_unid']").attr('content');

  		var message = "";

  		message = message + "&Name="+document.ContactVenueBook.Cname.value;
  		message = message + "&Telephone="+document.ContactVenueBook.Ctel.value;
  		message = message + "&Email="+document.ContactVenueBook.Cemail.value;
  		message = message + "&Participents="+document.ContactVenueBook.Cnum.value;
  		message = message + "&Date="+document.ContactVenueBook.Cdate.value;

  		var notes = $("form#ContactVenueBook textarea").text();

  		message = message + "&Notes="+notes

  		var queryString = "create=true&sender=&recipient="+brand_unid+"&title="+SPTitle+"&content="+message+" From your "+SPTitle+" SitePitch - "+(document.contactVenue.message_body.value)+"&state=n/a&messagetype=Booking&senton="+(d.toGMTString())+"&lang=en";

  		$.ajax({
  			type:'POST',
  			url:'/Messages/SendEmail/',
  			data:queryString,
  			complete:function(a,b) {
  				if(b == 'success') {
  					$("form#contactVenue").parent().prepend("<p>Your message has been sent</p>");
  					$("form#ContactVenueBook").parent().fadeOut('slow');
  				} else {
  					alert("Failed to Send");
  				}
  			}

  		});
  		return false;
  	})

 });

/******************************
 *
 *	last minute details
 *
 ********/

$(function() {
$('#book-online').hide(0);
	$('.book-online').click(function(){
		$('#book-online').fadeIn('slow');
		return false;
	});
});

$(function() {
$('#book-online .close').livequery('click',function() {
		$('#book-online').hide();
	});
});

/******************************
 *
 *	last minute details
 *
 ********/

$(function() {
$('#jbox').hide(0);
	$('.jbox-link').click(function(){
		$('#jbox').empty().load($(this).attr('href')).fadeIn('slow');
		return false;
	});
});

$(function() {
$('#jbox .close').livequery('click',function() {
		$('#jbox').hide();
	});
});

/******************************
 *
 *	clear-form-input
 *
 ********/

function clearText(thefield){
if (thefield.defaultValue==thefield.value)
thefield.value = ""
}

/******************************
 *
 *	innerfade
 *
 ********/

$(function(){
	$('#venue-gallery').innerfade({ speed: 'slow', timeout: 6000, type: 'sequence', containerheight: '200px' });
});

/******************************
 *
 *	jq-innerfade
 *
 ********/


$.fn.innerfade = function(options) {
return this.each(function(){

var settings = {
speed: 'normal',
timeout: 2000,
type: 'sequence',
containerheight: 'auto'
}

if(options)
$.extend(settings, options);

var elements = $(this).children().get();

if (elements.length > 1) {

$(this).parent().css('position', 'relative');

$(this).css('height', settings.containerheight);

for ( var i = 0; i < elements.length; i++ ) {
$(elements[i]).css('z-index', elements.length - i).css('position', 'absolute');
$(elements[i]).hide();
}

if ( settings.type == 'sequence' ) {
setTimeout(function(){
$.innerfade.next(elements, settings, 1, 0);
}, settings.timeout);
$(elements[0]).show();
}
else if ( settings.type == 'random' ) {
setTimeout(function(){
do { current = Math.floor ( Math.random ( ) * ( elements.length ) ); } while ( current == 0 )
$.innerfade.next(elements, settings, current, 0);
}, settings.timeout);
$(elements[0]).show();
}
else {
alert('type must either be \'sequence\' or \'random\'');
}

}

});
};

$.innerfade = function() {}
$.innerfade.next = function (elements, settings, current, last) {

$(elements[last]).fadeOut(settings.speed);
$(elements[current]).fadeIn(settings.speed);

if ( settings.type == 'sequence' ) {
if ( ( current + 1 ) < elements.length ) {
current = current + 1;
last = current - 1;
}
else {
current = 0;
last = elements.length - 1;
}
}
else if ( settings.type == 'random' ) {
last = current;
while (current == last ) {
current = Math.floor ( Math.random ( ) * ( elements.length ) );
}
}
else {
alert('type must either be \'sequence\' or \'random\'');
}
setTimeout((function(){$.innerfade.next(elements, settings, current, last);}), settings.timeout);
};
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//micro tabs//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$(function(){
$('#tab-menu').tabs(3);
$('#venue-menu').tabs();
$('#signup-menu').tabs();
//$('#sitepitch').tabs();
} );

$(function(){

	//$("#newsreel ul").liScroll();
	$("#myTable").tablesorter();

	$('#last-min-deals-cta>a, #prices-from-cta>a, p a[href^="#tabs-container"]').livequery('click', function(e){
		e.preventDefault();
		var
			tc = $(this).parents('.tabbed:first'),
			tb = tc.find('a[href="'+$(this).attr('href')+'"]').attr('id');

		tc.triggerTab(tb);
		return false;
	});

});
