jQuery.noConflict();
jQuery(document).ready(function($){
	
	$('a[rel="external"]').click(function() {
		window.open( $(this).attr('href') );
		return false;
	});
	
	$("a[rel^='colorbox']").colorbox();
	
	$('#nav li').hover(function() {
		$(this).addClass('hover');
	}, function() {
		$(this).removeClass('hover');
	});
	
	$("#nav li:has(ul)").hover(function () {
		$(this).children("a").click(function () {
			return false;
		});
	});
	
	$("#nav li:has(ul)").hover(function () {
		$(this).children("a").css('cursor', 'default')
	});
	
	$("#subscribe_email").focus(function() {
		if( $("#subscribe_email").val() == "Your Email Address") $("#subscribe_email").val('');
	}).blur(function() {
		if( $("#subscribe_email").val() == '' ) $("#subscribe_email").val("Your Email Address");
	});
	
	$('#subscribe_form').submit(function() {
		if ( $('#subscribe_email').val() == 'Your Email Address')
		return false;
	});
	
	$("#s").focus(function() {
		if( $("#s").val() == "Search the Site") $("#s").val('');
	}).blur(function() {
		if( $("#s").val() == '' ) $("#s").val("Search the Site");
	});
	
	$('#searchform').submit(function() {
		if ( $('#s').val() == 'Search the Site')
		return false;
	});
	
	$("#nav ul").each(function(i){
		var container = $("#nav");
		var containerPosition = container.position();
		var containerCoords = containerPosition.left + container.width();
	
		var s = $(this);
		var position = s.position();
		var subCoords = $(this).parent().position().left + containerPosition.left + s.width();
	
		if ( subCoords > containerCoords) {
			var newPosition = (containerCoords - subCoords) - 15;
			s.css("left",newPosition);
		};
	});
	
	$('#side_sponsors_image').cycle({
		fx: 'scrollLeft',
		easing: 'easeOutQuart',
		speed: 1000, 
		timeout: 0,
		sync: true,
		pager: '#side_sponsors_nav',
		pagerAnchorBuilder: function(idx, slide) { return '#side_sponsors_nav li:eq(' + idx + ') a'; } 
	});
	
	$.ajax({
		type: 'GET',
		url: 'http://talchamber.com/api/',
		data: 'data=events',
		dataType: 'jsonp',
		beforeSend: function(data) {
		},
		success: function(data) {
			var headerEvents = '<ul id="header-events-list">';
			$.each(data.events, function(i, item) {
				headerEvents = headerEvents + '<li class="clearfix"><div class="header-event-title"><a href="' + item.event_permalink + '">' + item.event_name + '</a></div>';
				headerEvents = headerEvents + '<div class="header-event-notes">' + item.event_notes + '</div>';
				headerEvents = headerEvents + '<div class="header-event-info">';
				headerEvents = headerEvents + item.event_date + '<br/>';
				headerEvents = headerEvents + item.event_location;
				headerEvents = headerEvents + '</div></li>' + "\n";
				
			});
			headerEvents = headerEvents + '</ul>';
			headerEvents = headerEvents + '<div class="header-events-item clearfix"><a class="header-events-more" href="/events/">Learn More &raquo;</a>';
			
			var headerEventNav = '';
			headerEventNav = headerEventNav + '<ul id="header-events-nav">';
			var i = 1;
			while ( i <= ( data.total + 1 ) ) {
				headerEventNav = headerEventNav + '<li><a href="#">' + i + '</a> </li>';
				i++;
			}
 			headerEventNav = headerEventNav + '</ul></div>';
			
			var headerEventsOutput = headerEvents + "\n" + headerEventNav;
			
			$('#header-events').html(headerEventsOutput).removeClass('loading');
			$('#header-events-list li:first').show();
			$('#header-events-list').cycle({ 
				fx: 'scrollLeft',
				easing: 'easeOutQuart', 
				speed: 2000, 
				timeout: 8000,
				sync: true,
				pager: '#header-events-nav',
				pauseOnPagerHover: true,
				pause: true,
				pagerAnchorBuilder: function(idx, slide) { return '#header-events-nav li:eq(' + idx + ') a'; },
 				after: function(currSlideElement, nextSlideElement, options, forwardFlag) {
					var learnMoreLink = $('.header-event-title a',this).attr('href');
					$('.header-events-more').attr('href', learnMoreLink);
				}
			});
			Cufon.refresh;
			Cufon.refresh();
		},
		complete: function(data) {
		}
	});

	$.ajax({
		type: 'GET',
		url: 'http://talchamber.com/api/',
		data: 'data=quote',
		dataType: 'jsonp',
		success: function(data) {
			$('#footer_callout2 div').html(data.html);
		},
		complete: function(data) {
		}
	});

	$('.refresh').click(function(event) {
		event.preventDefault();
		
		$.ajax({
			type: 'GET',
			url: 'http://talchamber.com/api/',
			data: 'data=randomheader',
			dataType: 'jsonp',
			success: function(data) {
				$('#random_picture div').html(data.html);
				$('#random_picture div a').fadeIn('slow');
			},
			complete: function(data) {
			}
		}); 
	});
	
	$('.toggle_link').click(function(event) {
		$(this).parent().next('.expanded_text').toggle();
		
		event.preventDefault();
	});
	
});
