
$(document).ready(function () {

    /* Slideshow */
    if ($('.slideshow').length) {

        $('.slideshow').cycle({
            fx: 'fade',
            prev: '.slideshowPrev',
            next: '.slideshowNext',
            pager: '.slideshowNav',
            slideExpr: 'span',
            timeout: 10000,
            pause: 1
        });
    }

    /* Modals */
    if ($('.CallForEntryModalTrigger').length) {
        $('.CallForEntryModalTrigger').colorbox({ transition: "fade", inline: true, width: '575px', height: '300px', href: "#modalFormConditional" });
        $('#myself').colorbox({ href: '/MeetTheBeautifulMinds/EntryForm.aspx?id=NominatingSelf', scrolling: false, transition: "fade", width: '588px', height: '800px', opacity: '0.85', overlayClose: 'false', iframe: 'true' })
        $('#someoneElse').colorbox({ href: '/MeetTheBeautifulMinds/EntryForm.aspx', scrolling: false, transition: "fade", width: '588px', height: '1005px', opacity: '0.85', overlayClose: 'false', iframe: 'true' })
    }
});


function closeIt() {
 parent.$('#stframe').colorbox.close();
}

function clear_form_elements(ele) {

 $(ele).find(':input').each(function () {
  switch (this.type) {
   case 'password':
   case 'select-multiple':
   case 'select-one':
   case 'text':
   case 'textarea':
    $(this).val('');
    break;
   case 'checkbox':
   case 'radio':
    this.checked = false;
  }
 });

}

function ajax(isWebService, url, type, params, success)
{
    if (isWebService)
    {
        $.ajax(
        {
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            url: url,
            data: JSON.stringify(params),
            type: type,
            success: function(response)
            {
                success(response.d);
            }
        });
    }
    else
    {
        $.ajax(
        {
            url: url,
            data: params,
            type: type,
            success: success
        });
    }
}
/* MESSAGING */
function displayMessage(message, leftOffset, topOffset, e, eShadow)
{
   //dynamic
    e.css('top', topOffset + 'px');
    e.css('left', leftOffset + 'px');
    e.html(message);
    e.show();
    if(eShadow)
    {
        eShadow.css('top', (topOffset + 5) + 'px');
        eShadow.css('left', (leftOffset + 5) + 'px');
        eShadow.html(message);
        eShadow.show();
    }
}

function clearMessage(timeout, e, eShadow)
{
    setTimeout(function()
    {
    	e.hide();

    	e.html('');
    	if(eShadow)
    	{
    	    eShadow.hide();
    	    eShadow.html('');
    	}
    }, timeout * 1000);
}

function list(message, delim)
{
	var error = '';
	var arrError = trim(message).split(delim);
	var i = 0;
	for(i = 0; i < arrError.length; i++)
	{
		if(arrError[i].toString().length > 0)
		{
			error += '<li />' + arrError[i];
		}
	}
	error = '<ul class=\"error\">' + error + '</ul>';
	return error;
}

/* POSITIONING */
function getElemOffset(e, offset, property)
{
    var currentOffset = 0;
    if (e.offsetParent)
    {
        do
        {
        	if(property == 'left')
        	{
            	currentOffset += e.offsetLeft;
            }
            else
            {
           		currentOffset += e.offsetTop;
            }
        }
        while (e = e.offsetParent);

        currentOffset += offset;

        return currentOffset ;
    }
}


function getPointerOffset(e, option)
{
    var x = 0;
    var y = 0;
    var IE = document.all ? true : false;
    if (IE)
    {
        x = Event.clientX + document.body.scrollLeft;
        y = Event.clientY + document.body.scrollTop;
    }
    else
    {
        x = e.pageX;
        y = e.pageY;
    }
    if (x < 0)
    {
        x = 0;
    }
    if (y < 0)
    {
        y = 0;
    }
    if (option == 'x')
    {
        return x;
    }
    else if(option == 'y')
    {
        return y;
    }
	else
	{
		return [x,y];
	}
}

/* URL UTILS */
function getDomain()
{
    var domain = '';
    var url = location.href;
    var arr = url.split('/');
    var i = 0;
    for (i = 0; i < 3; i++)
    {
        domain += arr[i].toString() + '/';
    }

    return domain;
}

function getQueryString(url, name)
{
	var value = '0';

	var qs = "";
	try
	{
	    qs = url.split("?")[1].toString();
	}
	catch (e)
	    {
	
	}
	
	if (qs != "")
	{
	    var arrQS = qs.split('&');
	    for (var i = 0; i < arrQS.length; i++)
	    {
	        var arr = arrQS[i].toString().split('=');
	        if (arr[0].toString() == name)
	        {
	            value = arr[1].toString();
	            break;
	        }
	    }
	}
	
	return value;
}

/* WINDOW */
function redirect(url, bNewWindow)
{
    if (bNewWindow)
    {
        window.open(url);
    }
    else
    {
        location.href = url;
    }
}
function overlay(e, bDisplay, leftOffset, topOffset)
{    
    e.css('top', topOffset + 'px');
    e.css('left', leftOffset + 'px');
   	if(bDisplay)
   	{
   		e.show();
   	}    
   	else
   	{
   		e.hide();
   	}
}

function popup(url, width, height, bGetDomain)
{
    var left = (window.screen.width / 2) - (width / 2) - 10;
    var top = (window.screen.height / 2) - (height / 2) - 10;
    
    var features = "alwaysRaised=1,dependent=1,directories=0,menubar=0,resizable=0,scrollbars=0,status=0,titlebar=0,toolbar=0,z-lock=1,width=" + width + ",height=" + height + ",left=" + left + ",top=" + top;
    
    var URL = ((bGetDomain) ? getDomain() : "") + url;
    window.open(URL, '', features);
  
}


/* MISC */
function setDefaultButton(elem, button)
{
        elem.keypress(
            function (e)
            {                
                if (e.keyCode == '13')
                {
                    button.click();
                    e.preventDefault();
                }
            }
        );
	
}

function trim(str)
{
    var s = '';
    if (str != '')
    {
        s = str.replace(/^(\s)*/, '');
        s = s.replace(/(\s)*$/, '');
    }
    return s;
}

function quizOverlay() {
    _gaq.push(['_trackEvent', 'Beautiful Mind Quiz', 'Click', 'Take the Quiz']);
    $.fn.colorbox({ href: '/HowHealthyIsYourBrain/Quiz.aspx', width: '860px', height: '727px', opacity: '0.85', overlayClose: 'false', iframe: 'true', scrolling: false });
}
