var isIE = false;
var projectindex = 0;
var activeHandler = false;

jQuery(document).ready(function () {

    isIE = jQuery.browser.msie;

    jQuery("#homepage-wrapper").data("currentlyMoving", false);
    //set height of next and previous nav for projects
    setProjectPagingNav();

    //set first project
    getSuccessStoryText(projects[projectindex]);
    getArticleText(articles[projectindex]);

    jQuery('#project_navigation_next').detectFlicks({
        axis: 'x',
        threshold: 60,
        flickEvent:function(d) {
            hideHomepageContent();
            projectindex++;
            if (projectindex > projects.length - 1) {
                projectindex = 0;
            }
            jQuery('#bgstretcher').bgStretcher.play();
            getSuccessStoryText(projects[projectindex]);
            getArticleText(articles[projectindex]);
            showHomepageContent();
            e.preventDefault();

        }
    });

    jQuery('#project_navigation_previous').detectFlicks({
        axis: 'x',
        threshold: 60,
        flickEvent:function(d) {
            hideHomepageContent();
            projectindex--;
            if (projectindex < 0) {
                projectindex = projects.length - 1;
            }
            jQuery('#bgstretcher').bgStretcher.rewind();
            getSuccessStoryText(projects[projectindex]);
            getArticleText(articles[projectindex]);
            showHomepageContent();
            e.preventDefault();
         }
    });


    jQuery('#project_navigation_previous').hover(
        	function () {
		    jQuery('.project_previous').show();
		},
		function () {
		    jQuery('.project_previous').hide();
		});

    jQuery('#project_navigation_next').hover(
		function () {
		    jQuery('.project_next').show();
		},
		function () {
		    jQuery('.project_next').hide();
		});

    jQuery('.project_previous').click(function (e) {
        jQuery('#project_navigation_previous').hide();
        previousClick();
        e.preventDefault();
        jQuery('#project_navigation_previous').show();
    });

    jQuery('.project_next').click(function (e) {
	jQuery('#project_navigation_next').hide();
        nextClick();
        e.preventDefault();
        jQuery('#project_navigation_next').show();
    });

});

function nextClick()
{
    if ((jQuery("#homepage-wrapper").data("currentlyMoving") == false)) {
        jQuery("#homepage-wrapper").data("currentlyMoving", true);
        hideHomepageContent();
        projectindex++;
        if (projectindex > projects.length - 1) {
            projectindex = 0;
        }
        jQuery('#bgstretcher').bgStretcher.play();
        getSuccessStoryText(projects[projectindex]);
        getArticleText(articles[projectindex]);
        showHomepageContent();        
    } 
}

function previousClick()
{
    if ((jQuery("#homepage-wrapper").data("currentlyMoving") == false)) {
        jQuery("#homepage-wrapper").data("currentlyMoving", true);
        hideHomepageContent();
        projectindex--;
        if (projectindex < 0) {
            projectindex = projects.length - 1;
        }
        jQuery('#bgstretcher').bgStretcher.rewind();
        getSuccessStoryText(projects[projectindex]);
        getArticleText(articles[projectindex]);
	showHomepageContent();
    }
}


function setProjectPagingNav() 
{
    var navWidth = jQuery(window).width();
    var navHeight = jQuery(window).height();

    // previous and next css
    var navProjectTop = (navHeight - jQuery('#project_navigation .project_previous').height()) / 2;
    var navProjectWidth = (navWidth - 160) / 3;

    jQuery('#project_navigation_next .project_next').css("marginTop", navProjectTop + "px");
    jQuery('#project_navigation_previous .project_previous').css("marginTop", navProjectTop + "px");
    
    if (navProjectWidth < 120) {
        jQuery('#project_navigation_previous').width(120);
        jQuery('#project_navigation_next').width(120);
    }
    else {
        jQuery('#project_navigation_previous').width(navProjectWidth);
        jQuery('#project_navigation_next').width(navProjectWidth);
    }

    if(isIE) {
        jQuery('#project_navigation_previous').height(navHeight);
    }

}

function getSuccessStoryText(projectObject) {
    
    var title = projectObject.title;
    var excerpt = projectObject.excerpt;
    var caseLink = projectObject.url;
    var fact = projectObject.fact;
    var factText = projectObject.fact_text;
    var logoColor = projectObject.logo;
        
    jQuery('#logo a').attr('class', logoColor);

    var link = (projectObject.url=="")? "" : '<div style="padding-top:10px;"><a href="'+caseLink+'" >View case study</a></div>';

    jQuery('#casestudy_data h2').html(title);
    jQuery('#casestudy_data p:eq(0)').html(excerpt+link);
    jQuery('#casestudy_data p:eq(1)').html('<div class="fact">'+fact+'</div><div style="margin-top:5px;"><span class="facttext">'+factText+'</span></div>');
    
}

function getArticleText(articleObject) {

    //Article
    var articleTitle = articleObject.title;
    var articleExcerpt = articleObject.excerpt;
    var articleHref = articleObject.url;
    var articleLink = (articleObject.url == "") ? "" : '<div style="padding-top:10px;"><a href="' + articleHref + '" >Read the full Story</a></div>';
    
    jQuery('#image_data h1').html(articleTitle);
    jQuery('#image_data p:eq(0)').html(articleExcerpt + articleLink);

    //Tweet
    var tweetText = articleObject.tweet;
    if (tweetText == "") {
        jQuery('#tweet_data').hide();
        //jQuery('#tweet_data').css("display", "block");
        jQuery('#tweet_data').css("visibility", "hidden");
        jQuery('#tweet_data').css("float", "right");
    }
    else {
        jQuery('#tweet_data').show();
        //jQuery('#tweet_data').css("display", "visible");
        jQuery('#tweet_data').css("visibility", "visible");
        jQuery('#tweet_data').css("float", "left");
        jQuery('#tweet_data p').html(tweetText);
    }

    //Tags
    var tagText = articleObject.tags;
    jQuery('#tag_data p').html(tagText);    
}

function hideHomepageContent() {
    jQuery('#casestudy_data h2').hide();
    jQuery('#casestudy_data p').hide();
    jQuery('#image_data h1').hide();
    jQuery('#image_data p').hide();
    jQuery('#tweet_data div').hide();
    jQuery('#tweet_data p').hide();
    jQuery('#tag_data div').hide();
    jQuery('#tag_data p').hide();
}

function showHomepageContent() {
    jQuery('#casestudy_data h2').fadeIn(1000);
    jQuery('#casestudy_data p').fadeIn(1000);
    jQuery('#image_data h1').fadeIn(1000);
    jQuery('#image_data p').fadeIn(1000);
    jQuery('#tweet_data div').fadeIn(1000);
    jQuery('#tweet_data p').fadeIn(1000);
    jQuery('#tag_data div').fadeIn(1000);
    jQuery('#tag_data p').fadeIn(1000, function() {
        jQuery("#homepage-wrapper").data("currentlyMoving", false);
    });
}