/* jQuery elementReady plugin
 * Version 0.6
 * Copyright (C) 2007-08 Bennett McElwee.
 * Licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License (http://creativecommons.org/licenses/by-sa/3.0/)
 * Permissions beyond the scope of this license may be available at http://www.thunderguy.com/semicolon/.
 */
(function($) {
var interval = null;
var checklist = [];

$.elementReady = function(id, fn) {
    checklist.push({id: id, fn: fn});
    if (!interval) {
        interval = setInterval(check, $.elementReady.interval_ms);
    }
    return this;
};

// Plugin settings
$.elementReady.interval_ms = 23; // polling interval in ms

// Private function
function check() {
    var docReady = $.isReady; // check doc ready first; thus ensure that check is made at least once _after_ doc is ready
    for (var i = checklist.length - 1; 0 <= i; --i) {
        var el = document.getElementById(checklist[i].id);
        if (el) {
            var fn = checklist[i].fn; // first remove from checklist, then call function
            checklist[i] = checklist[checklist.length - 1];
            checklist.pop();
            fn.apply(el, [$]);
        }
    }
    if (docReady) {
        clearInterval(interval);
        interval = null;
    }
};

})(jQuery);

/**
 * This assumes jQuery is loaded
 */

function initTabs()
{
	var sets = document.getElementsByTagName("ul");
	for (var i = 0; i < sets.length; i++)
	{
		if (sets[i].className.indexOf("tabset") != -1)
		{
			var tabs = [];
			var links = sets[i].getElementsByTagName("a");
			for (var j = 0; j < links.length; j++)
			{
				if (links[j].className.indexOf("tab") != -1)
				{
					tabs.push(links[j]);
					links[j].tabs = tabs;
					var c = document.getElementById(links[j].href.substr(links[j].href.indexOf("#") + 1));

					//reset all tabs on start
					if (c) if (links[j].className.indexOf("active") != -1) c.style.display = "block";
					else c.style.display = "none";

					links[j].onclick = function ()
					{
						var c = document.getElementById(this.href.substr(this.href.indexOf("#") + 1));
						if (c)
						{
							//reset all tabs before change
							for (var i = 0; i < this.tabs.length; i++)
							{
								document.getElementById(this.tabs[i].href.substr(this.tabs[i].href.indexOf("#") + 1)).style.display = "none";
								this.tabs[i].className = this.tabs[i].className.replace("active", "");
							}
							this.className += " active";
							c.style.display = "block";
							return false;
						}
					}
				}
			}
		}
	}
}

function xmlNodeIndex(o) {
    return $(o).parent().contents().index(o);
}

function precedingTextNode(o) {
    i = xmlNodeIndex(o)
    return $(o).parent().contents()[i - 1];
}

function lastWord(n) {
    t = $(p).text().trim().split(/\s+/);
    return t[t.length - 1];
}

function precedingWord(o) {
    return lastWord(precedingTextNode(o));
}


function doRateRandomProd(params) {
    var url = $('#ta-rand-production form').attr('action');
    if (url) {
        $.post(url, params, function(data) {
                if (data.empty == 0) {
                    $('#ta-rand-production').hide();
                    return;
                }
                var x = $('#ta-rand-production');
                x.fadeOut(400, function() {
                    x.find('img').attr('height', data.imgh).attr('width', data.imgw).attr('src', data.img);
                    x.find('form').attr('action', data.action);
                    x.find('p>a').text(data.title).attr('href', data.url);
                    $('.random-rating').rating('drain');
                }).fadeIn();
                return false;
            },
            'json'
        );
    }
}

$(function() {
    initTabs();
    $(".default-text").focus(function(srcc)
    {
        if ($(this).val() == $(this)[0].title)
        {
            $(this).removeClass("default-text-active");
            $(this).val("");
        }
    });
    
    $(".default-text").blur(function()
    {
        if ($(this).val() == "")
        {
            $(this).addClass("default-text-active");
            $(this).val($(this)[0].title);
        }
    });
    
    $(".default-text").blur();

    $('.ta-avatar-mine:first .ta-avatar-default').qtip({
        content: {
            title: {
                text: 'This is you!',
                button: 'close'
            },
            text: "Nobody looks good in orange.  <a href=\"/profile\">Go to your profile</a> to update your picture."
        },
        show: {
            ready: true
        },
        position: {
            corner: {
                target: 'rightMiddle',
                tooltip: 'leftTop'
            }
        },
        style: {
            name: 'dark',
            tip: 'leftTop',
            width: 300,
            border: {
                radius: 4
            }
        }
    });


    $('form.ta-score label').hide();
    $('form.ta-score input[type="submit"]').hide();
    $('form.ta-score').each(function(i) {
        var t = $(this);
        var url = t.attr('action');

        var up = $('<a href="#">Yes</a>').click(function() {
            $.post(url, {});
            t.replaceWith('<span>Thanks for rating this review!</span>');
            return false;
        });
        var down = $('<a href="#">No</a>').click(function() {
            $.post(url, {});
            t.replaceWith('<span>Thanks for rating this review!</span>');
            return false;
        });

        t.children('.ta-score-up').first().replaceWith(up);
        t.children('.ta-score-down').first().replaceWith(down);
    });

});
