jQuery(document).ready(function()
{
	$("#rennie").click(function(){
		$("#header-description").html('<strong>September 16th:</strong>Join us for a night of laughter in support of the arts<div id="get-tickets-now">' + $('#get-tickets-now').html());
		$('#get-tickets-now #item_name').attr('value', $('#header-description strong').text() + ' Join us for a night of laughter in support of the arts');
		$('#get-tickets-now #os0').attr('value', 'Weekend Ticket');		
	});
	
	$("#jn_thursday_show").click(function(){
		$("#header-description").html('<strong>September 9th:</strong>The Joe Narvaez Thursday Show!<div id="get-tickets-now">' + $('#get-tickets-now').html());
		$('#get-tickets-now #item_name').attr('value', $('#header-description strong').text() + ' The Joe Narvaez Thursday Show.');
		$('#get-tickets-now #os0').attr('value', 'Weekend Ticket');		
	});
	
	$('.input-box').focus(function(){
				$(this).val('');		
	});
	$('.input-box').blur(function(){
			if($(this).val() == '')	
				$(this).val('Email address here');		
	});
	$('#email-sign-up').click(function(){
		var data_string = $('#email-sign-up-form').serialize();
		
		$.ajax({
		    type:       "POST",
		    url:        "http://www.standupny.com/php/email.php",
		    data:       data_string,
		    success:    function() {
			
				alert('Thank you for signing up for our newsletter!');

		    }//end success function


		}) //end ajax call

		return false;
	});
	
	$('#win-tickets-submit').click(function(){
		var data_string = $('#win-free-tickets-form').serialize();
		
		$.ajax({
		    type:       "POST",
		    url:        "http://www.standupny.com/php/email2.php",
		    data:       data_string,
		    success:    function() {
			
				alert('Thank you for entering the free ticket contest, good luck!');

		    }//end success function


		}) //end ajax call

		return false;
	});
	/********************/	
	var inTransition = 0;
	var interval_id;
	var current_slide = 0;
	var currentTime = new Date();
				//var currentTime = new Date("October 13, 2009 11:13:00");
				var day = currentTime.getDate();
				var dow = currentTime.getDay();
				var month = currentTime.getMonth();
				var day_tracker = 1;
				var banner_html;
				
				var months = new Array();
				var events = new Array();
				var titles = new Array();
				var descriptions = new Array();
				
				months[5] = 31;
				months[6] = 32;
				months[7] = 32;
				months[8] = 31;
				months[9] = 32;
				var first_day = get_first_day_of_month(day, dow);
				var dow_tracker = first_day;
				var week = $('<tr></tr>');
				
				week.append('<td class="padding" colspan="'+(first_day)+'"></td>')
				
				while(day_tracker < 35)
				{
					if(day_tracker < months[month])
					{
						
							week.append('<td id="day'+day_tracker+'">'+day_tracker+'</td>');
						
					}
					else
						if(day_tracker > 34)
							week.append('<td class="padding last"></td>');
						else
							week.append('<td class="padding"></td>');
						
					day_tracker++;
					dow_tracker++;
					if(dow_tracker > 6)
					{ 
						$('.month').append(week);
						dow_tracker = 0;
						week = $('<tr></tr>');
					}
					
				}
				$('#show-selector').append('<div style="text-align:center;color:white;"></div>');
				$('.padding').click(function(){
					$('#c_month').html($('#next_month').html());
					})
				first_day = get_first_day_of_month(4, 0);
				dow_tracker = first_day;
				var day_tracker = 1;
				week = $('<tr></tr>');
				
				week.append('<td class="padding" colspan="'+(first_day)+'"></td>')
				
				while(day_tracker < 35)
				{
					if(day_tracker < months[8])
					{
						
							week.append('<td id="nday'+day_tracker+'">'+day_tracker+'</td>');
						
					}
					else
						week.append('<td class="padding"></td>');
						
					day_tracker++;
					dow_tracker++;
					if(dow_tracker > 6)
					{
						$('.month2').append(week);
						dow_tracker = 0;
						week = $('<tr></tr>');
					}
					
				}
					
				//$('.month').find('.last').text('-->');
				
			
			
			function get_first_day_of_month(day, dow)
			{
				while(day != 1)
				{
					day --;
					if(dow > 0)
						dow--;
					else
						dow = 6;
				}
				
				return dow;
			}
	
	load_comics();
	
    // Add the drop shadow to the logo
//    $("#logo img").dropShadow({bottom: 3, left: 5, right: 5, top: 0, opacity: 0.8, blur: 3});

    // On Hover for "Upcoming Shows" button
    $('#upcoming-shows').hover(function()
    {
        // Ensure that the hover function only occurs when the menu is not actively moving
        if (inTransition == 0)
        {
            // Set the state to "in transition"
            $(this).inTransition = 1;
            // Stop all transitioning
            $('#show-selector,').stop(true, true);
            $('#show-selector').css('zIndex', '3000');
            $('#show-selector', this).slideDown(250, function()
            {
                inTransition = 0;
            });
        }
    }, 
    function()
    {
        obj = this;
        if (inTransition == 0)
        {
            inTransition = 1;
            $('#show-selector', this).slideUp(250, function()
            { 
                inTransition = 0;
            });
        }
		//$('#banner').html(banner_html);
		//loadBanners();
    });
	


  

  

    $("#logo img, .sign-up img").dropShadow({left: -2, top: 0, color: 'black', opacity: 0.8, blur: 3});

    $('.columnize').columnize({ columns: 2, width: 220 });
    $('.columnize-3').columnize({ columns: 3 });
    $('.columnize-4').columnize({ columns: 4, width: 259, height: 858 });

// Displays comedian hover caption  
    $('.columnize-4 .column li').hover(function()
    {
        
		$(this).css({
            color: '#FFFFFF',
            //cursor: 'pointer',
            textShadow: '#FFFFFF 0px 0px 5px'
        });

        var xleft = $(this).offset().left;
        var xright = $(this).offset().right;
        var y = $(this).offset().top;

        if ($(this).parent().hasClass('last'))
        {
            $('#talent-caption').css({
                top: (y + 20) + 'px',
                right: (xleft + 10) + 'px'
            });
        }
        else
        {
            $('#talent-caption').css({
                top: (y + 20) + 'px',
                left: (xleft + 10) + 'px'
            });
        }
		var image_html = '<img style="margin-top:53px;margin-left:48px;width:162px;height:203px;border:1px solid black;" src="http://www.standupny.com/images/talent_images/'+$(this).find('.image').text()+'" />';
		var bio_html = '<div style="position:relative;left:250px;top:-170px;width:210px;height:300px;clear:none;font-size:10px;margin:0;"><p>'+$(this).find('.bio').text().substring(0,530)+'...</p></div>';
		var name_html = '<div style="position:relative;left:250px;top:-500px;margin:0;clear:none;"><h3>'+$(this).find('.name').text()+'</h3></div>';
		$('#talent-caption').html(image_html+bio_html+name_html);
        $('#talent-caption').show();
    }, function()
    {
        $(this).css({
            color: '#333333',
            //cursor: 'default',
            textShadow: 'none'
        });
        $('#talent-caption').hide();
    });

    $('.event-menu-private img').mousemove(function(e)
    {
        var mouseX = e.pageX - findPos(this)[0];

        if (mouseX > 140 && mouseX < 296)
        {
            $(this).attr('src', '/images/events-menu-private-over.png');
            $(this).css('cursor', 'pointer');
        }
        else
        {
            $(this).attr('src', '/images/events-menu-private.png');
            $(this).css('cursor', 'default');
        }
    });

    $('.event-menu-private img').hover(function()
    { }, function()
    {
        $(this).attr('src', '/images/events-menu-private.png');
        $(this).css('cursor', 'default');
    });

    $('.event-menu-private img').click(function(e)
    {
        var mouseX = e.pageX - findPos(this)[0];

        if (mouseX > 140 && mouseX < 296)
        {
            document.location = '/events/groups/';
        }
    });

    $('.event-menu-group img').mousemove(function(e)
    {
        var mouseX = e.pageX - findPos(this)[0];

        if (mouseX < 147)
        {
            $(this).attr('src', '/images/events-menu-group-over.png');
            $(this).css('cursor', 'pointer');
        }
        else
        {
            $(this).attr('src', '/images/events-menu-group.png');
            $(this).css('cursor', 'default');
        }
    });

    $('.event-menu-group img').hover(function()
    { }, function()
    {
        $(this).attr('src', '/images/events-menu-group.png');
        $(this).css('cursor', 'default');
    });

    $('.event-menu-group img').click(function(e)
    {
        var mouseX = e.pageX - findPos(this)[0];

        if (mouseX < 147)
        {
            document.location = '/events/';
        }
    });

    $('.talent-menu-all-talent img').mousemove(function(e)
    {
        var mouseX = e.pageX - findPos(this)[0];

        if (mouseX > 113 && mouseX < 247)
        {
            $(this).attr('src', '/images/talent-menu-all-talent-over-open-mic.png');
            $(this).css('cursor', 'pointer');
        }
        else if (mouseX > 247 && mouseX < 384)
        {
            $(this).attr('src', '/images/talent-menu-all-talent-over-comedy-class.png');
            $(this).css('cursor', 'pointer');
        }
        else if (mouseX > 384 && mouseX < 518)
        {
            $(this).attr('src', '/images/talent-menu-all-talent-over-new-talent.png');
            $(this).css('cursor', 'pointer');
        }
        else
        {
            $(this).attr('src', '/images/talent-menu-all-talent.png');
            $(this).css('cursor', 'default');
        }
    });

    $('.talent-menu-all-talent img').hover(function()
    { }, function()
    {
        $(this).attr('src', '/images/talent-menu-all-talent.png');
        $(this).css('cursor', 'default');
    });

    $('.talent-menu-all-talent img').click(function(e)
    {
        var mouseX = e.pageX - findPos(this)[0];

        if (mouseX > 113 && mouseX < 247)
        {
            document.location = '/talent/open-mic/';
        }
        else if (mouseX > 247 && mouseX < 384)
        {
            document.location = '/talent/comedy-class/';
        }
        else if (mouseX > 384 && mouseX < 518)
        {
            document.location = '/talent/new-talent/';
        }
    });

    $('.talent-menu-open-mic img').mousemove(function(e)
    {
        var mouseX = e.pageX - findPos(this)[0];

        if (mouseX < 113)
        {
            $(this).attr('src', '/images/talent-menu-open-mic-over-all-talent.png');
            $(this).css('cursor', 'pointer');
        }
        else if (mouseX > 247 && mouseX < 384)
        {
            $(this).attr('src', '/images/talent-menu-open-mic-over-comedy-class.png');
            $(this).css('cursor', 'pointer');
        }
        else if (mouseX > 384 && mouseX < 518)
        {
            $(this).attr('src', '/images/talent-menu-open-mic-over-new-talent.png');
            $(this).css('cursor', 'pointer');
        }
        else
        {
            $(this).attr('src', '/images/talent-menu-open-mic.png');
            $(this).css('cursor', 'default');
        }
    });

    $('.talent-menu-open-mic img').hover(function()
    { }, function()
    {
        $(this).attr('src', '/images/talent-menu-open-mic.png');
        $(this).css('cursor', 'default');
    });

    $('.talent-menu-open-mic img').click(function(e)
    {
        var mouseX = e.pageX - findPos(this)[0];

        if (mouseX < 113)
        {
            document.location = '/talent/';
        }
        else if (mouseX > 247 && mouseX < 384)
        {
            document.location = '/talent/comedy-class/';
        }
        else if (mouseX > 384 && mouseX < 518)
        {
            document.location = '/talent/new-talent/';
        }
    });

    $('.talent-menu-comedy-class img').mousemove(function(e)
    {
        var mouseX = e.pageX - findPos(this)[0];

        if (mouseX < 113)
        {
            $(this).attr('src', '/images/talent-menu-comedy-class-over-all-talent.png');
            $(this).css('cursor', 'pointer');
        }
        else if (mouseX > 113 && mouseX < 247)
        {
            $(this).attr('src', '/images/talent-menu-comedy-class-over-open-mic.png');
            $(this).css('cursor', 'pointer');
        }
        else if (mouseX > 384 && mouseX < 518)
        {
            $(this).attr('src', '/images/talent-menu-comedy-class-over-new-talent.png');
            $(this).css('cursor', 'pointer');
        }
        else
        {
            $(this).attr('src', '/images/talent-menu-comedy-class.png');
            $(this).css('cursor', 'default');
        }
    });

    $('.talent-menu-comedy-class img').hover(function()
    { }, function()
    {
        $(this).attr('src', '/images/talent-menu-comedy-class.png');
        $(this).css('cursor', 'default');
    });

    $('.talent-menu-comedy-class img').click(function(e)
    {
        var mouseX = e.pageX - findPos(this)[0];

        if (mouseX < 113)
        {
            document.location = '/talent/';
        }
        else if (mouseX > 113 && mouseX < 247)
        {
            document.location = '/talent/open-mic/';
        }
        else if (mouseX > 384 && mouseX < 518)
        {
            document.location = '/talent/new-talent/';
        }
    });

    $('.talent-menu-new-talent img').mousemove(function(e)
    {
        var mouseX = e.pageX - findPos(this)[0];

        if (mouseX < 113)
        {
            $(this).attr('src', '/images/talent-menu-new-talent-over-all-talent.png');
            $(this).css('cursor', 'pointer');
        }
        else if (mouseX > 113 && mouseX < 247)
        {
            $(this).attr('src', '/images/talent-menu-new-talent-over-open-mic.png');
            $(this).css('cursor', 'pointer');
        }
        else if (mouseX > 247 && mouseX < 384)
        {
            $(this).attr('src', '/images/talent-menu-new-talent-over-comedy-class.png');
            $(this).css('cursor', 'pointer');
        }
        else
        {
            $(this).attr('src', '/images/talent-menu-new-talent.png');
            $(this).css('cursor', 'default');
        }
    });

    $('.talent-menu-new-talent img').hover(function()
    { }, function()
    {
        $(this).attr('src', '/images/talent-menu-new-talent.png');
        $(this).css('cursor', 'default');
    });

    $('.talent-menu-new-talent img').click(function(e)
    {
        var mouseX = e.pageX - findPos(this)[0];

        if (mouseX < 113)
        {
            document.location = '/talent/';
        }
        else if (mouseX > 113 && mouseX < 247)
        {
            document.location = '/talent/open-mic/';
        }
        else if (mouseX > 247 && mouseX < 384)
        {
            document.location = '/talent/comedy-class/';
        }
    });

    var d = new Date();
   // loadCalendar(d.getMonth(), d.getYear());

    $('.charity-link').click(function()
    {
        document.location = 'http://blogs.wsj.com/speakeasy/2010/05/22/a-jewish-singleton-looks-for-drama-in-dating/';
    });

    loadBanners();
	
	  var _gaq = _gaq || [];
	  _gaq.push(['_setAccount', 'UA-17593356-2']);
	  _gaq.push(['_trackPageview']);

	  (function() {
		var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
		ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
		var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
	  })();


});
 


function loadBanners()
{
    $.get('/data2/banners/', function(d)
    {
        $('#banner').empty();
        $(d).find('banner').each(function()
        {
            var $banner = $('<img src="' + $(this).find('img').text() + '" />');
            if ($(this).find('link').text() != '')
            {
                $banner.attr('linkTo', $(this).find('link').text());
            }
            $('#banner').append($banner);
        });
        $('#banner img:first').addClass('active');
		banner_html = $('#banner').html();
		interval_id = setInterval('slideSwitch()', 5000);
		return banner_html;
    });
	
}

function slideSwitch()
{
    var $active = $('#banner .active');

    if ($active.length == 0)
        $active = $('#banner img:last');

    var $next = $active.next().length ? $active.next() : $('#banner img:first');

    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function()
        {
            $active.removeClass('active last-active');
        });
}

function findPos(obj)
{
    var posX = obj.offsetLeft;
    var posY = obj.offsetTop;

    while(obj.offsetParent)
    {
        if (obj == document.getElementsByTagName('body')[0])
        {
            break;
        }
        else
        {
            posX = posX + obj.offsetParent.offsetLeft;
            posY = posY + obj.offsetParent.offsetTop;
            obj = obj.offsetParent;
        }
    }
    var posArray = [posX, posY];
    return posArray;
}
 

function slide_control()
{
	if($('#slides').attr('alt') > 1)
	{
		if($('.slide0').attr('alt') == 0)
		{
			$('.slide0').animate({'marginLeft': -1016}, 1500, "easeInOutQuad");
			$('#hdslide0').fadeOut(80, function(){$('#hdslide1').fadeIn(800);});
			
			//$('#hdslide0').animate({'marginLeft': -1016}, 1500, "easeInOutQuad");
			$('.slide0').attr('alt',1);
		}
		else
		{
			$('.slide0').animate({'marginLeft': 0}, 1500, "easeInOutQuad");
			$('#hdslide1').fadeOut(80, function(){$('#hdslide0').fadeIn(750);});
			
			//$('#hdslide0').animate({'marginLeft': 0}, 1500, "easeInOutQuad");
			$('.slide0').attr('alt',0);
		}
	}
	else
	{
		$('.slide0').animate({'marginLeft': 0}, 1500, "easeInOutQuad");	
	}
}

function load_comics()
{
    var d = new Date();
    var month = d.getMonth();
	var year = d.getYear();	
	var cur_day = d.getDate();
	//var day = d.getDate();
	$.get('/data/?month=' + month + '&year=' + year, function(d)
    {			 
	
		$(d).find('entry').each(function()
			{
				var $item = $('<div class="day"></div>');
				var day_of_week = $(this).find('dayofweek').text();
				var month = $(this).find('month').text();
				var day = $(this).find('day').text();
				var time = $(this).find('time').text();
				var event_div = $('<div class="events"></div>');
				var event_div_ul = $('<ul></ul>');
				var event_div_li = $('<li></li>');
				var html = '';
				var $comedians = new Array;
				var count = 0;
				var image_html = $(this).find('image').text();
				
				
			if(month == 'Sep' && day >= cur_day)
			{
				
					$('#day'+$(this).find('day').text()).addClass('date_has_event');
					$('#day'+$(this).find('day').text()).click(
						function(){
					  if (day_of_week == 'Fri' || day_of_week == 'Sat')
						{
							$('#get-tickets-now #os0').attr('value', 'Weekend Ticket');							
						}
						else
						{
							$('#get-tickets-now #os0').attr('value', 'Weekday Ticket');
						}

						
					
					$('#header-description').html('<div id="dyndesc"></div><div id="get-tickets-now">' + $('#get-tickets-now').html());
					$('#get-tickets-now #item_name').attr('value', $('#header-description strong').text() + $('#header-description .comedians').text());	
					
					clearInterval(interval_id);
					$.get('/data4?day='+day, function(data) {
					  $("#banner").html("<div style=\"margin-top:50px;overflow:hidden;\">"+data+"</div>");
							
					});
										
					$.get('/get_hd?day='+day, function(data) {
					  $('#dyndesc').html(data);				
					});
					$('#hdslide0').fadeIn(750);
					interval_id = window.setInterval("slide_control()", 6000);
					//$('#banner').html('<div style="margin-top:50px;overflow:hidden;"><div style="background-image:url(/images/banner_add.jpg);width:262px;height:284px;float:left;clear:none;text-align:center;"></div>'+html+'</div>');
				});
			
			
			}
			
			if(month == 'Oct')
			{
				
					$('#nday'+$(this).find('day').text()).addClass('date_has_event');
					$('#nday'+$(this).find('day').text()).click(
						function(){
					  if (day_of_week == 'Fri' || day_of_week == 'Sat')
						{
							$('#get-tickets-now #os0').attr('value', 'Weekend Ticket');							
						}
						else
						{
							$('#get-tickets-now #os0').attr('value', 'Weekday Ticket');
						}

						
					
					$('#header-description').html('<div id="dyndesc"></div><div id="get-tickets-now">' + $('#get-tickets-now').html());
					$('#get-tickets-now #item_name').attr('value', $('#header-description strong').text() + $('#header-description .comedians').text());	
					
					clearInterval(interval_id);
					$.get('/data5?day='+day, function(data) {
					  $("#banner").html("<div style=\"margin-top:50px;overflow:hidden;\">"+data+"</div>");
							
					});
										
					$.get('/get_hd2?day='+day, function(data) {
					  $('#dyndesc').html(data);				
					});
					$('#hdslide0').fadeIn(750);
					interval_id = window.setInterval("slide_control()", 6000);
					//$('#banner').html('<div style="margin-top:50px;overflow:hidden;"><div style="background-image:url(/images/banner_add.jpg);width:262px;height:284px;float:left;clear:none;text-align:center;"></div>'+html+'</div>');
				});
			
			
			}
		
		}); 
	}) 
	
}

