filter = 0;
genres = new Array();

$(document).ready(function(){
	
/*** Search filter functionality ***/

	// Collapse and show filters
	$('#sidebar-search-filters > li > span').bind('click', function(){
		$(this).next().toggle();
		if( $(this).parent().hasClass('collapsed') ) {
			$(this).parent().removeClass('collapsed');
			$.ajax({
			  type: 'POST',
			  url: "http://www.rasa.nl/programma/monthopen",
			  data: {'name':$(this).text() }
			});
		} else {
			$(this).parent().addClass('collapsed');
                        $.ajax({
                          type: 'POST',
                          url: "http://www.rasa.nl/programma/monthopen",
                          data: {'name':$(this).text() }
                        });

		}
		return false;
	});
	
	// Make filters clickable
	$('#sidebar-search-filters > li > ul > li').bind('click', function(){
	    if($(this).parent().attr('id') == 'genre-filter'){
	       window.open('/programma/genre/'+$(this).attr('id'), '_self');
	    }
		if($(this).parent().attr('id') == 'continent-filter'){
           window.open('/programma/werelddeel/'+$(this).attr('id'), '_self');
        }
        if($(this).parent().attr('id') == 'year-filter'){
           window.open('/programma/jaar/'+$(this).attr('id'), '_self');
        }
        if($(this).parent().attr('id') == 'month-filter'){
           window.open('/programma/maand/'+$(this).attr('id'), '_self');
        }
		return false;
	});
	
	// reset button
	$('#sidebar-search-filters > li.reset > span').unbind('click').bind('click', function(){
		window.open('/programma/wissen/', '_self');
		return false;
	});
	
/*** Slideshow on homepage ***/
	$('#sidebar-showcase-pictures').cycle({
		speed: 500,
		timeout: 5000,
	    next:  '#sidebar-showcase-info .next',
	    prev:  '#sidebar-showcase-info .prev',
	    pause: true,
	    before: function(curr,next,opts){
	    	var this_ = this;
			$('#sidebar-showcase-info .title').html( $(this_).attr('title') );
			$('#sidebar-showcase-info .date').html( $(this_).attr('date') );
			
			$('#sidebar-showcase-info .title,#sidebar-showcase-info .date').unbind('click');
			$('#sidebar-showcase-info .title,#sidebar-showcase-info .date').click(function(event) {
                event.preventDefault();
                event.stopPropagation();
                if($(this_).attr('url-target') == 'blank'){
                    window.open($(this_).attr('url'), '_blank');
                }else{
                    window.open($(this_).attr('url'), '_self');
                }
            });

			$('#sidebar-showcase-info .pager').html( (opts.nextSlide+1) + '/' + opts.slideCount );
	    },
	    after: function(curr,next,opts){
			$('#sidebar-showcase-info .pager').html( (opts.currSlide+1) + '/' + opts.slideCount );	    	
	    }
	});
	
/*** Slideshow on detail page ***/
	$('.detail-slideshow-images').cycle({
		speed: 500,
		timeout: 5000,
	    next:  '.detail-slideshow-info .next',
	    prev:  '.detail-slideshow-info .prev',
	    pause: true,
	    before: function(curr,next,opts){
	    	var this_ = this;
			$('.detail-slideshow-info .title').html( $(this_).attr('alt') );
			$('.detail-slideshow-info .pager').html( (opts.nextSlide+1) + '/' + opts.slideCount );
	    },
	    after: function(curr,next,opts){
			$('.detail-slideshow-info .pager').html( (opts.currSlide+1) + '/' + opts.slideCount );	    	
	    }
	});
	
/*** Newspage toggle ***/
	$('.newspage-button, .newspage h1').bind('click', function(){
		if( $(this).parent().hasClass('expanded') ) {
			$(this).parent().removeClass('expanded');
			$(this).parent().find('.newspage-content').show();
		} else {
			$('.newspage.expanded').find('.newspage-content').slideToggle().parent().removeClass('expanded');
			$(this).parent().addClass('expanded');
			$(this).parent().find('.newspage-content').hide();
		}
		
		$(this).parent().find('.newspage-content').slideToggle(300,function(){
			if ( $.browser.msie ) {
				if( parseInt($.browser.version, 10) <= 8 ) {
					$('.newsitems-navigation li a').each(function(){
						PIE.detach(this);
						PIE.attach(this);
					});
				}
			}
		});

		return false;
	});
	
/*** Newspage add twitter button ***/
    twitterDataUrl = $('.newsitem-social>li:first-child>#dataurl').html();  
    twitterDataText = $('.newsitem-social>li:first-child>#datatext').html();
	$('.newsitem-social>li:first-child').html('<a href="http://twitter.com/share" class="twitter-share-button" data-count="none" data-text="'+twitterDataText+'" data-url="'+twitterDataUrl+'">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>');

/*** Popup ***/
	$('.popup-close').bind('click', function(){
		$('#popup').hide();
		return false;
	});
	
	$('#popup').bind('click', function(event){
        $('#popup').hide();
        return false;
    });
    
    $('#popup-inner').bind('click', function(event){
        return false;
    });
	
/*** Detail page more ***/
	$('.detail-left-more').click(function(){
		$('.detail-more').slideToggle(300);
		return false;
	});
	
/*** Placeholder ***/
	$('input').placeholder();
	
	$(".at-ticket-button").click(function(e){
		var url = $(this).attr('href');
		$.colorbox({width:"970px", height:"570px", iframe:true, href:url});
		return false;
	});

    $('#mailform').submit(function(event){
        event.preventDefault();
        
        $form = $( this );
        $error = false;
        
        $senderMail = $form.find( 'input[name="sendermail"]' );
        if($senderMail.val() == ''){
            $senderMail.addClass('error');
            $error = true;
        }else{
            $senderMail.removeClass('error');
        }
        $senderName = $form.find( 'input[name="sendername"]' );
        if($senderName.val() == ''){
            $senderName.addClass('error');
            $error = true;
        }else{
            $senderName.removeClass('error');
        }
        $email = $form.find( 'input[name="email"]' );
        if($email.val() == ''){
            $email.addClass('error');
            $error = true;
        }else{
            $email.removeClass('error');
        }
        $name = $form.find( 'input[name="name"]' );
        if($name.val() == ''){
            $name.addClass('error');
            $error = true;
        }else{
            $name.removeClass('error');
        }

        if($error == false) {
            $html = '<a class="popup-close" href="#">Close</a>';
            $html += 'Hallo '+$name.val()+',<br/>';
            $html += 'Ga je mee?<br/>';
            $html += '<br/>';
            $html += $('.event-date big').html()+' '+$('.event-date small').html()+' '+$('#event-time').html()+'<br/>';
            $html += $('#event-title').html()+'<br/>';
            $html += $('#event-subtitle').html()+'<br/>';
            $html += $('#event-subgenre').html()+'<br/>';
            $html += '<a href="'+include_path+'programma/event/'+$form.find( 'input[name="urlrewrite"]' ).val()+'/" target="_blank">'+$('#event-title').html()+'</a><br/>';
            $html += '<br/>';
            $html += $form.find( 'textarea[name="content"]' ).val().replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1' + '<br/>' + '$2')+'<br/>';
            $html += '<br/>';
            $html += 'Kijk op <a href="http://www.rasa.nl/" target="_blank" >www.rasa.nl</a> voor het volledige programma.<br/>';
            $html += 'Op de hoogte blijven van nieuws en programma van RASA? Meld u hier aan voor de RASA nieuwsbrief.<br/>';
            $html += '<br/>';
            $html += 'Hartelijke groet,<br/>';
            $html += $senderName.val()+'<br/><br/>';
            $html += '<label>&nbsp;</label><input type="submit" onclick="sendTellAFriend();" value="Versturen" class="button button-narrow"><br/>';
            $('#popup-response').html($html);
            $('#popup-block').hide();
            $('#popup-response').show();
            
            $('.popup-close').bind('click', function(){
                $senderMail.val('');
                $senderName.val('');
                $email.val('');
                $name.val('');
                $form.find( 'textarea[name="content"]' ).val('');
                $('#popup-response').hide();
                $('#popup-block').show();
                $('#popup').hide();
                return false;
            });
        }
    });
	
});

function sendTellAFriend() {
    sendermail1 = $form.find( 'input[name="sendermail"]' ).val();
    sendername1 = $form.find( 'input[name="sendername"]' ).val();
    email1 = $form.find( 'input[name="email"]' ).val();
    name1 = $form.find( 'input[name="name"]' ).val();
    content1 = $form.find( 'textarea[name="content"]' ).val();
    urlrewrite1 = $form.find( 'input[name="urlrewrite"]' ).val();
    
    $.post(include_path+'programma/verstuur-mail/', { sendermail: sendermail1, sendername:sendername1, email:email1, name:name1, content:content1, urlrewrite:urlrewrite1 }, function(data){
        data = $.parseJSON(data);
        $('#popup-response').html(data);
        $('.popup-close').bind('click', function(){
            $senderMail.val('');
            $senderName.val('');
            $email.val('');
            $name.val('');
            $form.find( 'textarea[name="content"]' ).val('');
            $('#popup-response').hide();
            $('#popup-block').show();
            $('#popup').hide();
            return false;
        });
    });
}

function addslashes(str) {
    str=str.replace(/\\/g,'\\\\');
    str=str.replace(/\'/g,'\\\'');
    str=str.replace(/\"/g,'\\"');
    str=str.replace(/\0/g,'\\0');
    return str;
}

function showPopup() {
	$('#popup').appendTo('body').show();
}



