$(document).ready(function(){
    
    $('#toolTipBox').append("");
    
    $("#id_help").click(function() {
        $("#fdbk_tab").click();
        return false;
    });

     $('.classDashWrap').hover( function () {
        $(this).find('h3').toggleClass("h3Hover");
    });

    $('.addTool').hover(
        function () {
            $(this).addClass("addToolHover");
        }, function () {
            $(this).removeClass("addToolHover");
    });

     $('.deleteTool').hover(
        function () {
            $(this).addClass("deleteToolHover");
        }, function () {
            $(this).removeClass("deleteToolHover");
    });
        
    $('.inputWithDefault').each(function() {
        if ($(this).val() == '') {
            $(this).val($(this).attr('default'));
        };
    });

    $('.inputWithDefault').live('focus', function() {
        if ($(this).val() == '' || $(this).val() == $(this).attr('default')) {
            $(this).val('');
        }
        $(this).css({'color' : '#333', 'background-color' : '#fff'});
    });

    $('.inputWithDefault').live('blur', function() {
        if ($(this).val() == '' || $(this).val() == $(this).attr('default')) {
            $(this).val($(this).attr('default'));
            $(this).css({'color' : '#999', 'background-color' : '#eee'});
        }
    });
    
    $('.hasHover').live('mouseover', function(){
        var hoverObject = $(this);
        showToolTip(hoverObject);

        $('.toolTips').mouseenter(function() {
            showToolTip(hoverObject);
        });

        $('.toolTips').mouseleave(function() {
            hideToolTip();
        });
        $(this).mouseleave(function() {
            hideToolTip();
        });
    });
});

var showToolTip = function(object) {
    var offset = $(object).offset(),
     tipp = $(object).attr('tip'),
     objWidth = $(object).outerWidth(),
     objCenter = objWidth/2,
     offleft = offset.left,
     docWidth = $(document).width(),
     pLeft =  (docWidth-990)/2,
     properLeft;
     
    var hoverObject = $(object);
    var htools = $('#toolTipBox .toolTips');
    var filler = $('#toolTipBox .hoverFill');
    htools.find('p').html(tipp);
    var tippHeight = $(htools).height();
    var tippWidth = ($(htools).outerWidth())/2;
    var offtop = (offset.top - tippHeight)-12;
    if($(htools).outerWidth() > objWidth){
        filler.css({'width' : $(htools).outerWidth(), 'height' : $(object).outerHeight()+14, 'bottom' : -($(object).outerHeight()-1), 'left' : '0px', 'margin-left' : '0px'});
    } else {
        filler.css({'width' : objWidth, 'height' : $(object).outerHeight()+14, 'bottom' : -($(object).outerHeight()-1), 'left' : '50%', 'margin-left' : -(objWidth/2)});
    }
    properLeft = offleft + objCenter - tippWidth;

    $(htools).fadeIn("slow");
    $(htools).css({'top' : offtop, 'left' : properLeft});
}

var hideToolTip = function() {
    var htools = $('#toolTipBox .toolTips');
    $(htools).fadeOut("fast");
}

var clearFormDefaults = function(form) {
    $(form).find('.inputWithDefault').each(function() {
        if ($(this).val() == $(this).attr('default')) {
            $(this).val('');
        }
    });
};

function goToPage() {
    try {
        mpq.track("User GoToPage Click");
    } catch(err) {
    }
    
    var baseValId = (arguments[0]) ? arguments[0] : '#gotoPage_base';
    var pageValId = (arguments[1]) ? arguments[1] : '#gotoPage';
    var redirectURL = $(baseValId).val()+$(pageValId).val() + "/";
    window.location.href = redirectURL;
};

function acknowledgeContribute(redirectURL) {
    $('#contentHelpBanner').hide();
    $.get('/account/acknowledge-contribute/');
};

var showTour = function(tourType) {
    if (tourType == 'subject') {
        $('#id_tour_subject').html('<img src="/media/images/tour/subject.png" />');
        $('#id_tour').show();
        tourSeen(tourType);
    } else if (tourType == 'subject_anon') {
        $('#id_tour_subject').html('<img src="/media/images/tour/subjectanon.png" />');
        $('#id_tour').show();    
    } else if (tourType == 'answers_only') {
        $('#id_tour_answers_only_jump').html('<img src="/media/images/tour/answers_only_jump.png" />');
        $('#id_tour_answers_only_just').html('<img src="/media/images/tour/answers_only_just.png" />');
        $('#id_tour').show();
        tourSeen(tourType);
    } else if (tourType == 'answers_only_anon') {
        $('#id_tour_answers_only_jump').html('<img src="/media/images/tour/answers_only_jump.png" />');
        $('#id_tour_answers_only_just_anon').html('<img src="/media/images/tour/answers_only_just.png" />');
        $('#id_tour').show();        
    } else if (tourType == 'exercise_detail') {
        $('#id_tour_ex_chicklet').html('<img src="/media/images/tour/exercise_detail_chicklet.png" />');
        $('#id_tour_ex_rate').html('<img src="/media/images/tour/exercise_detail_rate.png" />');   
        $('#id_tour_ex_send').html('<img src="/media/images/tour/exercise_detail_send.png" />');
        $('#id_tour_ex_comment').html('<img src="/media/images/tour/exercise_detail_comment.png" />');        
        $('#id_tour').show();
        tourSeen(tourType);
    } else if (tourType == 'logged_in_home') {
        $('#id_tour_badges').html('<img src="/media/images/tour/badges.png" />');
//        $('#id_tour_gold').html('<img src="/media/images/tour/goldnav.png" />');
        $('#id_tour').show();
        tourSeen(tourType);
    };
}

var tourSeen = function(tourType) {
    $.post('/account/tour-seen/', {tourtype: tourType});
}


$('html').ajaxSend(function(event, xhr, settings) {
    function getCookie(name) {
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
    if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
        // Only send the token to relative URLs i.e. locally.
        if (xhr.setRequestHeader !== undefined){
            xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
        }
    }
});


$.fn.clearForm = function() {
    return this.each(function() {
        var type = this.type, tag = this.tagName.toLowerCase();
        if (tag == 'form')
            return $(':input',this).clearForm();
        if (type == 'text' || type == 'password' || tag == 'textarea')
            this.value = '';
        else if (type == 'checkbox' || type == 'radio')
            this.checked = false;
        else if (tag == 'select')
            this.selectedIndex = -1;
    });
};


var set_rating = function(stars, rating) {
    this_star = $(stars).find('.hStar[rate="'+rating+'"]');
    $(stars).find('.hStar').removeClass('hStarlight');
    this_star.addClass("hStarSelected");
    this_star.prev('.hStar').addClass("hStarSelected");
    this_star.prev('.hStar').prev('.hStar').addClass("hStarSelected");
    this_star.prev('.hStar').prev('.hStar').prev('.hStar').addClass("hStarSelected");
    this_star.prev('.hStar').prev('.hStar').prev('.hStar').prev('.hStar').addClass("hStarSelected");
}

