$(document).ready(function(){
    $('.ajaxbox').fancybox({
	autoDimensions: false,
	autoScale:	false,
	width:		700,
	height:		700,
	padding:	0,
	transitionIn:	'elastic',
	transitionOut:	'elastic',
	speedIn:	400, 
	speedOut: 	200,
	easingIn:	'easeInExpo',
	titleShow: 	false
    });
    $('a[rel=calendar]').click(function() {
	//$.fancybox.showActivity();
	//take current link
	var link = $(this).attr('href');
	//current date
	date = $(this).parent().attr('rel');
	day = date.split(' ');
	day = day[0];
	//collect all the links
	var data = new Array();
	//current
	var current = new String();
	$('a[rel=calendar]').each(function(index){
	    data[index] = {
		//'title': $(this).parent().attr('rel'),
		'href': MyAjax['ajaxurl'] + '?action=cibr_page_load&url=' + $(this).attr('href')
	    };
	    //find current
	    if (link == $(this).attr('href') && day == $(this).parent().siblings('.daynum').text()) 
		current = index;
	    
	});
	$.fancybox( data, {
	    autoDimensions: 	false,
	    autoScale:		false,
	    width:		500,
	    height:		500,
	    padding:		0,
	    transitionIn:	'elastic',
	    transitionOut:	'elastic',
	    speedIn:		400, 
	    speedOut: 		200,
	    easingIn:		'easeInExpo',
	    titlePosition:	'over',
	    index: 		current
	});
	return false;
    });
    $('.home-tab').click(function(){
	var tab = $(this).attr('id');
	var box = tab + '-box';
	$(this).removeClass('inactive').siblings().addClass('inactive');
	$('#' + box).fadeIn().siblings().css('display', 'none');
	if (tab == 'read-tweets' && !$(this).hasClass('tweets-loaded')) {
	    getTwitters('read-tweets-box', { 
		id: 'CIBR_News',
		count: 3,
		enableLinks: true,
		newwindow: true,
		clearContents: true,
		timeout: 10,
		onTimeoutCancel: true,
		template: '<h3 class="newsitem-title">\%text%\ <a href="http://twitter.com/%user_screen_name%/status/%id_str%">%time%</a></h3>'
	    });
	    $(this).addClass('tweets-loaded');
	}
    });    
    //load faq with ajax
    $('.faq-link').click(function(){
        //clicked element
        element = $(this).parents('.faq-item').attr('id');
        //clicked title
        title = $(this).text();
        //get link
        link = $(this).attr('href');
        //check to see if it exists already
        if($('#box-' + element).text()) {
            ajaxSetAll();
        } else {
            //send ajax request and load data
            $.ajax({
                url:        MyAjax.ajaxurl,
                data:       'action=cibr_faq_load&url=' + link,
                error:      function(){
                    window.location = link;
                },
                success:    function(data) {
                    //load the data
                    $('#' + element).append(data);
                    ajaxSetAll();
                }
            });
        }
        return false;
    });
});

function ajaxSetAll() {
    //check if we are clicking an already open box
    if( $('#' + element).hasClass('faq-active')) {
        //just close it if it already exists
        $('#' + element).removeClass('faq-active').children('.faq-content').fadeOut('fast');
    } else {
        $('.faq-content').fadeOut('fast');
        $('.faq-item').removeClass('faq-active');
        //switch box on
        $('#' + element).addClass('faq-active');
        $('#box-' + element ).fadeIn('fast');
        document.title = title;
    }
    //silly ie6 problem
    $('.main-bottom, .wrapper-bot').css('left', '0');
}

//function calendarTitle(title, currentArray, currentIndex, currentOpts) {
//    return '<div class="calendar-date">' + $(this).toSource() + '</div>';
//}
