Shadowbox.init();
$(function(){
    $('#newsletter_email').labelify({
        text: 'label'
        , labelledClass: 'labelled'
    });
    $('#newsletter_form label').hide();

    $('#google_cse_input').labelify({
        labelledClass: 'labelled'
    });

	if($('#book_now').length > 0 ){
	    $('#book_now a').click(function() {

	        $reserve_index = $(this).index();

	        Shadowbox.open({
	            width: 630
				, content: '/template/travelocity.php?tab=' + $reserve_index
				, player: 'iframe'
				, scrolling: 'no'
	        });
	        return false;
	    });
	}

    /* Add Vendor */
    $('#vendor_listing a.add').click(function(e){
        e.preventDefault();
        var vendorId = $(this).attr('data-id');
        Shadowbox.open({
            content: '/template/itinerary/add.php?vendor_id='+vendorId
            , height: 400
            , player: "iframe"
            , width: 500
        });
    });

    $('#add_link').click(function(e){
        e.preventDefault();
        var vendorId = $(this).attr('data-id');
        Shadowbox.open({
            content: '/template/itinerary/add.php?vendor_id='+vendorId
            , height: 400
            , player: "iframe"
            , width: 500
        });
    });

    /* Remove Vendor */
    $('#vendor_listing a.remove').click(function(e){
        e.preventDefault();
        var vendorId = $(this).attr('data-id');
        var itemId = $(this).attr('data-item-id');
        Shadowbox.open({
            content: '/template/itinerary/remove.php?vendor_id='+vendorId+'&item_id='+itemId
            , height: 250
            , player: "iframe"
            , width: 500
        });
    });

    /* Delete Itinerary */
    $('#itinerary-preview div.actions a.remove').click(function(e){
        e.preventDefault();
        var itemId = $(this).attr('data-id');
        Shadowbox.open({
            content: '/template/itinerary/delete.php?item_id='+itemId
            , height: 250
            , player: "iframe"
            , width: 500
        });
    });

    /* Edit Itinerary */
    $('#itinerary-editor a.rename').click(function(e){
        e.preventDefault();
        var itemId = $(this).attr('data-id');
        Shadowbox.open({
            content: '/template/itinerary/rename.php?item_id='+itemId
            , height: 500
            , player: "iframe"
            , width: 500
        });
    });

    $('#itinerary-blurb a.edit-description').click(function(e){
        e.preventDefault();
        var itemId = $(this).attr('data-id');
        Shadowbox.open({
            content: '/template/itinerary/rename.php?item_id='+itemId
            , height: 500
            , player: "iframe"
            , width: 500
        });
    });

    /* Create new Itinerary */
    $('#my-itineraries li.start a').click(function(e){
        e.preventDefault();
        Shadowbox.open({
            content: '/template/itinerary/create.php'
            , height: 500
            , player: "iframe"
            , width: 500
        });
    });

    /* Copy Itinerary */
    $('#itinerary-preview div.actions a.add').click(function(e){
        e.preventDefault();
        var from_id = $(this).attr('data-id');
        Shadowbox.open({
            content: '/template/itinerary/copy.php?from_id='+from_id
            , height: 350
            , player: "iframe"
            , width: 500
        });
    });

    $('#itinerary-listings a.add').click(function(e){
        e.preventDefault();
        var from_id = $(this).attr('data-id');
        Shadowbox.open({
            content: '/template/itinerary/copy.php?from_id='+from_id
            , height: 350
            , player: "iframe"
            , width: 500
        });
    });

    $('#feat_itin_slides a.add').click(function(e){
        e.preventDefault();
        var from_id = $(this).attr('data-id');
        Shadowbox.open({
            content: '/template/itinerary/copy.php?from_id='+from_id
            , height: 350
            , player: "iframe"
            , width: 500
        });
    });

    $('#print_link').click(function(e){
        e.preventDefault();

        $('#vendor_tabs div.tab_box').show();
        window.print();
        $('#vendor_tabs div.tab_box').hide();
        $('#vendor_tabs div.tabbed_links a.active').each(function(){
            $('#'+$(this).attr('rel')+'_box').show();
        });
    });

    $('#events_tabs').dsTab({activeSelector:'li'});
    InitSideEvent();
});

function InitTwitter() {
    $('#partner_tweets div.tweet_list').each(function(){
        var j = 0;
        $(this).children('div.tweet').each(function(i,el){
            var el = $(this);
            if (i > 2) {
                el.hide();
                el.addClass('after_event');
            } else {
                el.addClass('visible_event');
            }
            ++j;
        });
        if(j>3){
            var back = $('<a class="btn prev" href="">Back</a>');
            var next = $('<a class="btn next active" href="">Next</a>');
            next.prependTo($(this).next());
            back.prependTo($(this).next());
            $(this).next().delegate('a.next', 'click', function(e){
                e.preventDefault();
                var el = $(this);

                if (el.hasClass('active')) {
                    var events = $('#partner_tweets').children('div.tweet_list');
                    events.children('div.visible_event').first().hide().removeClass('visible_event').addClass('before_event');
                    events.children('div.after_event').first().show().removeClass('after_event').addClass('visible_event');
                    if (!events.children('div.after_event').length) {
                        el.removeClass('active');
                    }
                    el.prev().addClass('active');
                }
            });
            $(this).next().delegate('a.prev', 'click', function(e){
                e.preventDefault();
                var el = $(this);

                if (el.hasClass('active')) {
                    var events = $('#partner_tweets').children('div.tweet_list');
                    events.children('div.visible_event').last().hide().removeClass('visible_event').addClass('after_event');
                    events.children('div.before_event').last().show().removeClass('before_event').addClass('visible_event');
                    if (!events.children('div.before_event').length) {
                        el.removeClass('active');
                    }
                    el.next().addClass('active');
                }
            });
        }
    });
}

function InitSideEvent(){
    $('div.event_list').each(function(){
        var j = 0;
        $(this).children('div.side_event').each(function(i,el){
            var el = $(this);
            if (i > 2) {
                el.hide();
                el.addClass('after_event');
            } else {
                el.addClass('visible_event');
            }
            ++j;
        });
        if(j>3){
            var back = $('<a class="btn prev" href="">Back</a>');
            var next = $('<a class="btn next active" href="">Next</a>');
            back.appendTo($(this).next());
            next.appendTo($(this).next());
            $(this).next().delegate('a.next', 'click', function(e){
                e.preventDefault();
                var el = $(this);

                if (el.hasClass('active')) {
                    var events = el.closest('div.tab_box').children('div.event_list');
                    events.children('div.visible_event').first().hide().removeClass('visible_event').addClass('before_event');
                    events.children('div.after_event').first().show().removeClass('after_event').addClass('visible_event');
                    if (!events.children('div.after_event').length) {
                        el.removeClass('active');
                    }
                    el.prev().addClass('active');
                }
            });
            $(this).next().delegate('a.prev', 'click', function(e){
                e.preventDefault();
                var el = $(this);

                if (el.hasClass('active')) {
                    var events = el.closest('div.tab_box').children('div.event_list');
                    events.children('div.visible_event').last().hide().removeClass('visible_event').addClass('after_event');
                    events.children('div.before_event').last().show().removeClass('before_event').addClass('visible_event');
                    if (!events.children('div.before_event').length) {
                        el.removeClass('active');
                    }
                    el.next().addClass('active');
                }
            });
        }
    });
}

function LoadPartnerTwitter(data) {
    var tweetCont = $('<div class="tweet_list"></div>');
    $.each(data, function(i, item) {
        var dateObj = new Date();
        var time = Date.parse(item.created_at);
        var timeDiff = dateObj.getTime() - time;
        var timeString = '';
        if (timeDiff > 86400000) {
            timeString = Math.round(timeDiff / 1000 / 60 / 60 / 24) + ' days ago';
        } else if (timeDiff > 3600000) {
            timeString = Math.round(timeDiff / 1000 / 60 / 60) + ' hours ago';
        } else {
            timeString = Math.ceil(timeDiff / 1000 / 60) + ' minutes ago';
        }
        var text = item.text.replace(/http:\/\/([^ ]+)/g, '<a href="http://$1" target="_blank">http://$1</a>');
        text = text.replace(/@([^ ]+)/g, '@<a href="http://twitter.com/$1" target="_blank">$1</a>');
        text = text.replace(/#([^ ]+)/g, '<a href="http://twitter.com/#search?q=%23$1" target="_blank">#$1</a>');

        $('<div class="tweet"><p class="text"><a class="self" href="http://twitter.com/'+item.user.screen_name+'" target="_blank">'+item.user.screen_name+'</a><span> </span>'+(item.retweeted ? 'RT ' : '')+text+'</p><p class="meta">'+timeString+' via '+item.source+'</p></div>').appendTo(tweetCont);
    });
    tweetCont.appendTo('#partner_tweets');
    $('#partner_tweets div.tweet_info').prepend('<div class="tweet_image"><img alt="" src="'+data[0].user.profile_image_url+'" /></div>');
    $('#partner_tweets').append('<div class="follow_us"><a href="http://twitter.com/'+data[0].user.screen_name+'" class="follow" target="_blank">Follow Us &raquo;</a></div>');
    InitTwitter();
}

function TwitterJson(){
    var twitterName = $('#partner_tweets').attr('data-name');
    if (twitterName) {
        $.getJSON('/action/pull_twitter.php?t='+twitterName, LoadPartnerTwitter);
    }
}

