$(window)
    .ready(function () { 
        $("iframe").height($(window).height()-45);
        $("iframe").width($(window).width());
        $("#share_bar").width($(window).width());
    })
    .resize(function () { 
        $("iframe").height($(window).height()-45);
        $("iframe").width($(window).width());
        $("#share_bar").width($(window).width());
    });

$(document).ready(function() {
	
    var window_height = $(window).height(),
        story_id = $("meta[name='story_id']").attr('content'),
        story_title = $("meta[name='story_title']").attr('content'),
        story_summary = $("meta[name='story_summary']").attr('content');
    
    // Setup tooltips
    $("#share_bar").ready(function () {
        if(fwix.user_account_id == "" && !$("meta[name='referral']").attr('content')) {
            html =  '<h3>Local news, powered by Fwix.</h3>';
            html += '<p>Fwix is a live, up to the minute news feed of everything happening in your city. </p>';
            fwix.tooltipOpen($("#share_bar"),'welcome',html,12,155,240,null,'up',13);
        }
        $("#tooltip").mouseover(function () {
            if(fwix.cookieGet('share_getbreaking') == false && fwix.user_account_id == "") {
                fwix.cookieSet('share_getbreaking', true, 999999, 0, '/');
            }
        });
    });

    // Faux focus on click
	$('a').click(function() {
        fwix.clearFocus();
	});
	
	// Show the promo bubble if logged out
	if(fwix.logged_in == false) {
	    fwix.shareBar.showBubble();
	}
});


/* SHARE BAR
/ *******************************************************************************************/
fwix.shareBar = {

    /*** fwix.shareBar.register ***/
    showBubble: function(){
        setTimeout(function(){
            $("#share_bubble").show('scale','fast');
        },500);
    },

    /*** fwix.shareBar.register ***/
    hideBubble: function(){
        $("#share_bubble").hide();
        fwix.cookieSet('share_bubble', true, 999999, 0, '/');
    },

    /*** fwix.shareBar.register ***/
    register: function(){
        $.ajax({
            type: "GET",
            url: fwix.site_url+'/ajax/sharebar_register_popover.php',
            dataType: 'json',
            success: function(data){
                if(data.logged_in)
                    window.location.reload()
                else
                    fwix.popoverOpen(data.html, fwix.shareBar.registerSetup);
            }
        });
    },

    /*** fwix.shareBar.registerSetup ***/
    registerSetup: function() { 
        $('.inputs input').keypress(function(e){
            if(e.which == 13){
                fwix.shareBar.registerSubmit();
                return false;
            }
            return;
        });
    },

    /*** fwix.shareBar.registerSubmit ***/
    registerSubmit: function(){
        var inputs = $('#sharebar_register_pop'),
            name = inputs.find('[name="name"]').val(),
            email = inputs.find('[name="email"]').val(),
            password = inputs.find('[name="password"]').val(),
            newsletter = inputs.find('[name="newsletter"]').attr('checked'),
            submit = inputs.find('.submit');

        if(name == '' || name == '' || password == ''){
            fwix.formResponse(submit, 'error', 'Please complete the register form');
            return;
        }

        $.ajax({
            type: "POST",
            url: fwix.site_url+'/ajax/create_user.php',
            data: 'email='+encodeURIComponent(email)+
                  '&name='+encodeURIComponent(name)+
                  '&password='+encodeURIComponent(password)+
                  '&newsletter_signup='+newsletter,
            dataType: 'json',
            success: function(data){
                if(data.error)
                    fwix.formResponse(submit, 'error', data.error);
                else
                    window.location = fwix.site_url+'/register.php';
            }
        });
    },
    
    /*** fwix.shareBar.updateUserResponses ***/
    updateUserResponses: function(data){
        responses = $('.responses');

        for(i in data){
            var udata = data[i];
            var uid = udata.uid;
            var li = responses.find('[uid="'+uid+'"]');
            var answer = li.find('small');

            li.attr('class', udata.answer.toLowerCase());
            answer.html(udata.answer);
        }

        responses.find('li[uid=""]').each(function(){
            var rand_answer = Math.floor(Math.random()*2);
            var new_answer = (rand_answer == 0 ? 'Yes' : 'No');
            var li = $(this);
            var answer = li.find('small');
            li.attr('class', new_answer.toLowerCase());
            answer.html(new_answer);
        });

        fwix.shareBar.showResponses();
    
    },

    /*** fwix.shareBar.surveyAnswer ***/
    surveyAnswer: function(storyid, questionid, answerid){
        poll = $('.poll');
        fwix.survey.updateTracker(questionid, storyid);

        $.ajax({
            type: "GET",
            url: fwix.site_url+'/ajax/question_answer.php',
            data: 'storyid='+storyid+
                  '&geoid='+fwix.geo_id+
                  '&questionid='+questionid+
                  '&answerid='+answerid,
            dataType: 'json',
            success: function(data){
                
                fwix.shareBar.showSelfResponse();
                if(fwix.logged_in == true) {
                    $('.poll .responses li').fadeOut(300);
                    poll.find('.slider *').fadeOut(300);

                    setTimeout(function(){
                        fwix.shareBar.showSelfResponse();
                        poll.find('.slider').remove();

                        if(data.survey_completed){
                            fwix.survey.completeSurvey(poll[0], storyid, questionid);
                        }else{
                            fwix.shareBar.updateUserResponses(data.answerers);
                            fwix.survey.displayNewQuestion(poll[0], storyid, data.survey, true);
                        }
                    }, 300);
                }
                else {
                    fwix.shareBar.hideBubble();
                    setTimeout(function(){
                        fwix.shareBar.register();
                    }, 300);
                }
            }
        });
    },

    /*** fwix.shareBar.showResponses ***/
    showResponses: function(){
        responses = $('.poll .responses li');
        delay = 0;
        for (i=0;i<=responses.length-1;i++) {
            setTimeout('$(\'.poll .responses li:eq('+i+')\').show(\'scale\',{},100);',delay);
            delay += 100;
        }
    },

    /*** fwix.shareBar.showSelfResponse ***/
    showSelfResponse: function(){
        response = $('.poll .responses li.is_self');
        response.show('scale','fast');
    },

    /*** fwix.shareBar.hideSelfResponse ***/
    hideSelfResponse: function(){
        response = $('.poll .responses li.is_self');
        response.hide();
    }
};
