var Tabs = {
    initialize: function () {
        var tabs = $('tabs');
        if (!tabs) {
            return;
        }
        Tabs.tabs = tabs.select('li');

        var tabsContent = $('tabscontent') || $$('.tabscontent')[0];
        if (!tabsContent) {
            throw('Tabs initialization failed: missing #tabscontent element');
            return;
        }
        
        Tabs.tabsContent = tabsContent.childElements();
        Tabs.tabsContent.invoke('hide');
        Tabs.activeTab = null;
        Tabs.numTabs = 0;

        Tabs.tabs.each(function(li) {
            li.tabId = Tabs.numTabs;
            Tabs.numTabs++;

            if (li.id == 'tabHeaderActive') {
                Tabs.activeTab = li.tabId;
                Tabs.tabsContent[li.tabId].show();
            }

            li.down('a').observe('click', function(tab) {
                tab.element().up().id = 'tabHeaderActive';
                Tabs.toggle(tab.element().up().tabId + 1);
            });
            
        });

        /* activate first tab in list if none marked as active */
        if (Tabs.activeTab == null) {
            var act = tabs.readAttribute('active');
            Tabs.activeTab = (act)?act-1:0;
            Tabs.tabs[Tabs.activeTab].id = 'tabHeaderActive';
            Tabs.tabsContent[Tabs.activeTab].show();
        }
    },

    /**
     * Toggle tab number to open
     * @param int tabId : tab # starting at (1)
     */
    toggle: function (tabNum) {
        var tabId = tabNum - 1;
        Tabs.tabsContent[this.activeTab].hide();
        Tabs.tabs[this.activeTab].id = '';
        Tabs.tabs[tabId].id = 'tabHeaderActive';
        Tabs.activeTab = tabId;
        Tabs.tabsContent[tabId].show();
    }

};
document.observe('dom:loaded', function() {Tabs.initialize()});
/* End Functions used by tabbed pages */

Overlay = (function() {
    var overlay = null;
    return {
        create:  function create() {
            overlay = new Element('div');
            overlay.setStyle({backgroundColor:'black', zIndex:200, position:'absolute', top:'0px'});
            overlay.setOpacity(0.8);

            Overlay.setSize();
            $(document.body).insert(overlay);
            
            Event.observe(window, 'resize', function(e){Overlay.setSize()});
            Event.observe(window, 'scroll', function(e){Overlay.setSize()});
        },

        setSize: function() {
            var dim = document.viewport.getDimensions();
            var off = document.viewport.getScrollOffsets();
          //  overlay.setStyle({width: dim.width+off.left+'px', height:dim.height+off.top+'px'})
        },

        remove: function() {
            overlay.remove();
            overlay = null;
        },

        getElement: function() {
            return(overlay);
        }
    }
})();




function set_black(li)
{
	li.style.backgroundColor = "#FFF";
	
	links=li.getElementsByClassName('add_tribe');
	for (var i = 0; i < links.length; i++)
		{
			links[i].style.display='block';
		}
}

function set_white(li)
{
	li.style.backgroundColor = "#FFF";
	
	links=li.getElementsByClassName('add_tribe');
	for (var i = 0; i < links.length; i++)
		{
			links[i].style.display='none';
		}
}

function state1_on(id)
{
	if(id.value=='email')
	{
		id.value='';
	}
}

function state1_off(id)
{
	if(id.value=='')
	{
		id.value='email';
	}
}

function state2_on(id)
{
	if(id.value=='password')
	{
		id.value='';
	}
}

function state2_off(id)
{
	if(id.value=='')
	{
		id.value='password';
	}
}

function state_input_on(id)
{
	if(id.value=='Type here')
	{
		id.value='';
	}
}

function state_input_off(id)
{
	if(id.value=='')
	{
		id.value='Type here';
	}
}

function preview_message() {
    if ($('preview'))
        return;

    Overlay.create();

    var p = new Element('div', {'id':'preview', 'class':'alert'}).setStyle({visibility: 'visible'});
    p.update("Please log in or sign up to access this functionality.<br><br><b><a>close</a></b>");
    p.down('a').onclick = function() {p.remove(); Overlay.remove()}.bind(this);
    $(document.body).insert(p);
}

function disableLinks (section) {
	$$('#block-5 a, #block-8 a').each(function(el) {
	    el.onclick = preview_message;
	    el.writeAttribute('href', "javascript:void(0)");
	    el.setStyle({cursor: 'pointer'});
	});
}

/* 
 MOVED FROM PORTAL_INIT.JS
this is called by any JW player loaded, each has it's own ID
 * currently there are only two: video and music
 */

function playerReady(thePlayer) {

	/* initialize audio player (TribalMP3) */
	if (thePlayer.id == "mp3pl") {
		mp3player = new MP3Player(thePlayer.id);
		$('mp3pl').setStyle({visibility: 'hidden'});
	}

}

/**
ians code for cropper functions **/

function showCropper(cropperDiv){
	$(cropperDiv).setStyle({display: 'inline'});
}

function closeCropper(cropperDiv){
	$(cropperDiv).setStyle({display: 'none'});
}

function saveCropper(cropperID){
	if(confirm("Are you sure you wish to crop the image to these dimensions?")==true){
		
		var cropCoords = eval("cropCoords"+cropperID);
		var fileName = eval("cropFile"+cropperID);
		var mode = eval("cropMode"+cropperID);
		var width = eval("cropWidth"+cropperID);
		var height = eval("cropHeight"+cropperID);
		new Ajax.Request('?func=PageCropper', {
			method: 'post',
			postBody: 'cropID=1&x1='+cropCoords.x1+'&x2='+cropCoords.x2+'&y1='+cropCoords.y1+"&y2="+cropCoords.y2+"&file="+fileName+"&mode="+mode+"&width="+width+"&height="+height,
			onSuccess: function(r) {
				closeCropper('cropper'+cropperID);
				$("testImage"+cropperID).src=r.responseText;
				$("thumbCropper"+cropperID).src=r.responseText;
				alert('image cropped');
			}.bind(this)
		});
		
	}
}

/** end ians code **/
