var mozilla = !document.all && self.innerWidth;
var msie = !mozilla;
var opera = window.opera;
var docSize = null;

function mcanimeInit()
{
	if ($('faqs'))
	{
		faqs.init();
	}
    
    initInputs();
	
	if (screen.width < 1024)
	{
		document.getElementsByTagName('body')[0].style.scroll = 'scroll';
	}
    
    var adCont = $('openx_bgad');
    var adAs = adCont.getElementsByTagName('a')[0];
    
    if(adAs)
    {
        var adImg = adAs.getElementsByTagName('img')[0];
        
        var adUrl = adImg.parentNode.href;
        var adDiv = $('openx_wrap');
        adDiv.style.backgroundImage = 'url('+adImg.src+')';
        adDiv.style.backgroundColor = '#000';
        adDiv.style.cursor = 'pointer';
        adDiv.onclick = function(e){
            if(!e) e = window.event;
            
            if(e.target)
                var targ = e.target;
            else if(e.srcElement)
                var targ = e.srcElement;
            
            if($(targ).id == 'openx_wrap')
                window.open(adUrl);
        };
        
        adCont.parentNode.removeChild(adCont);
    }
}

//-----------------------------------------------------------------------------

var faqs =
{
	current: null,
	opacity: 0,
	
	init: function ()
	{
		var a = $('faqs').getElementsByTagName('a');
		var aLen = a.length;
		
		for (var i = 0; i < aLen; i++)
		{
			if (a[i].name)
			{
				if (mozilla || opera) addEventSimple(a[i], "click", faqs.mode);
				else a[i].onclick = faqs.mode;
			}
		}
	},
	
	mode: function ()
	{
		window.s_h('faq-' + this.name);
	}
}

//-----------------------------------------------------------------------------

function addInfo()
{
	var buttons = getElementsByClassName('content', 'a', 'sinfo');
	var bLen = buttons.length;
	var s = 'show';
	var r = 'hide';
	
	if (bLen > 0)
	{
		for (var i = 0; i < bLen; i++)
		{
			if (buttons[i].className.indexOf(r) >= 0)
			{
				s = 'hide';
				r = 'show';
			}
			
			buttons[i].className = buttons[i].className.replace(s, r);
		}
	}
	
	return false;
}

//-----------------------------------------------------------------------------

function synText(obj, e)
{
	var key = e.which ? e.which : (window.event ? window.event.keyCode : 0);
	
	if (key == 13)
		return false;
	
	if (obj.value.search(/[\n\xA1!\xBF?]/) >= 0)
	{
		obj.value = obj.value.replace(/\n/g, ' ');
		obj.value = obj.value.replace(/[\xA1!\xBF?]/g, '');
	}
	
	if (obj.value.length > 1000)
		obj.value = obj.value.substr(0, 1000);
	
	return true;
}

//-----------------------------------------------------------------------------

function validateSDDForm()
{
	$('series_id_error').className = $('series_id_error').className.replace('show', 'hide');
	
	if ($('series_id').value == '' || $('series_id').value == 0)
	{
		$('series_id_error').className = $('series_id_error').className.replace('hide', 'show');
		return false;
	}
	
	return true;
}

//-----------------------------------------------------------------------------

function loader()
{
	// Si se utiliza Mozilla o Opera
	if (mozilla || opera)
	{
		// Utilizamos evento callback
		document.addEventListener("DOMContentLoaded", mcanimeInit, false);
	}
	// Si se utiliza Internet Explorer entonces usamos este excelente hack de Matthias Miller
	else if (msie)
	{
		// S�lo funciona cuando se utiliza document.write();
		document.write("<scr" + "ipt id=__ie_init defer=true " + "src=//:><\/script>");
	
		// Utilizamos el hack del defer
		var script = document.getElementById("__ie_init");
		
		// el script no existe si se ejecuta la funci�n din�micamente
		if (script)
		{
			script.onreadystatechange = function()
			{
				if (this.readyState != "complete") return;
				mcanimeInit();
			};
		}
	
		// Lo eliminamos de la memoria
		script = null;
	}

	// En caso de que no se cumpla lo anterior, utilizamos window.onload
	if (!(mozilla || opera || msie))
		addEventSimple(window, "load", mcanimeInit);
}

//-----------------------------------------------------------------------------

function s_h(ref)
{
	var refClass = $(ref).className;
	
	if (refClass.indexOf('show') >= 0)
	{
		$(ref).className = refClass.replace('show', 'hide');
	}
	else
	{
		$(ref).className = refClass.replace('hide', 'show');
	}
}

//-----------------------------------------------------------------------------

var currentFeed = 'all';
function getNewsFeeds(type)
{
    if (type != currentFeed)
    {
        $('tabs_'+currentFeed).className = '';
        $('tabs_'+type).className = 'sel';
        
        if (!$('feeds_'+type).getAttributeNode('loaded'))
        {
            $('feeds_'+currentFeed).className = 'hide';
            $('feeds_'+type).className = '';
            
            var ajax = new XHConn();
            ajax.connect('/feeds/ajax/'+type, 'GET', '', function(oXML){
                $('feeds_'+type+'_cont').innerHTML = oXML.responseText;
                var sib = $('feeds_'+type+'_cont').nextSibling;
                if (sib.nodeType != 1)
                    sib = sib.nextSibling;
                sib.className = 'feeds_pages';
                var tmp = document.createAttribute('loaded');
                tmp.nodeValue = 'true';
                $('feeds_'+type).setAttributeNode(tmp);
            });
        }
        else
        {
            $('feeds_'+currentFeed).className = 'hide';
            $('feeds_'+type).className = '';
        }
        
        currentFeed = type;
    }
    
    return false;
}

//-----------------------------------------------------------------------------

function closeSubscriptionNotice()
{
    var date = new Date();
    date.setTime(date.getTime()+(24*60*60*1000));
    
    $('subscriptionNotice').className = 'hide';
    document.cookie = 'subscriptionNotice=1;expires=' + date.toGMTString(); + ';path=/;domain=mcanime12.com';
}

function closeKronosNotice()
{
    $('kronos').className = 'hide';
    document.cookie = 'kronosNotice=1;path=/;domain=mcanime.net';
}

//-----------------------------------------------------------------------------

function computeSizes()
{
    var sizes = Array();
    
    if (typeof(window.innerWidth) == 'number')
    {
        //Non-IE
        sizes['width'] = window.innerWidth;
        sizes['height'] = window.innerHeight;
    }
    else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
    {
        //IE 6+ in 'standards compliant mode'
        sizes['width'] = document.documentElement.clientWidth;
        sizes['height'] = document.documentElement.clientHeight;
    }
    else if (document.body && (document.body.clientWidth || document.body.clientHeight))
    {
        //IE 4 compatible
        sizes['width'] = document.body.clientWidth;
        sizes['height'] = document.body.clientHeight;
    }
    
    return sizes;
}

docSize = computeSizes();

//-----------------------------------------------------------------------------

/**
 * Function initInputs - Makes it so that identified inputs have a focus and blur option for removing and restoring default text.
 */
function initInputs()
{
    // Get the list of all inputs with the defined class
    var ins = getElementsByClassName(document.getElementsByTagName('body')[0], 'input', 'srcht');
    // Calculate the length (for performance)
    var insLength = ins.length;
    
    // Loop the inputs
    for (var i = 0; i < insLength; i++)
    {
        // Add the :focus event and function
        addEventSimple(ins[i], 'focus', function(){
            // If the value is == to its title then clear the input so the user can begin typing
            if (this.value == this.title)
                this.value = '';
        });
        // Add the :blur event and function
        addEventSimple(ins[i], 'blur', function(){
            // Match anything from a space, tab, etc. If true set the default text
            if (this.value.match(/\s*/))
                this.value = this.title;
        });
    }
}

//-----------------------------------------------------------------------------

function $(element)
{
	if (typeof(element) == "string")
		element = document.getElementById(element);
	
	return 	element;
}

loader();

