var slides=new Array();

window.addEvent('domready', function() {
	initializeEverything();
});

function initializeEverything() {
    Shadowbox.init({
        displayNav: false,
        enableKeys: false,
        overlayOpacity: 0.2,
        resizeDuration: 0,
        initialHeight: 200,
        initialWidth: 300,
        players:  ['html'],
        player: "html",
        language: "en"
    });

    if ($('CallToActionSlideShow')) {
        mySlideShow = new SlideShow('CallToActionSlideShow',{
            delay: 1000,
            transition: 'fade',
            duration: 175,
            autoplay: true
        });
        $('CallToActionSlideShow').setStyle('display', 'inline');
    }

    if ($('y_scroll') && parseInt($('y_scroll').value) > 0) {
        window.scroll(0, parseInt($('y_scroll').value));
    }

    if ($('SearchForm')) {
	    $('SearchForm').addEvent('submit', function(e){
		    if ($('SearchInput').value=="" || $('SearchInput').value=="Trouver un Cours ou Professeur" || $('SearchInput').value=="Find a Course or Professor") {
			    e.stop();
		    }
	    });
    }

    if ($('SearchInput')) {
        $('SearchInput').addEvent('click', function(){
            /*this.set('tween', {transition: Fx.Transitions.Quint.easeOut, duration: '400'});
            this.tween('width', 172);*/
            if (this.value=="Trouver un Cours ou Professeur" || this.value=="Find a Course or Professor") {
                this.oldValue = this.value;
                this.value="";
            }
        });

        $('SearchInput').addEvent('blur', function(){
            /*this.set('tween', {transition: Fx.Transitions.Quint.easeOut, duration: '400'});
            this.tween('width', 102);*/
            if(this.value=="") {if (this.oldValue) this.value=this.oldValue;}
        });
    }

    if ($('q')) {
        $('q').addEvent('click', function(){
            if (this.value=="Mots clés de recherche" || this.value=="Search keywords") {
                this.oldValue = this.value;
                this.value="";
            }
        });

        $('q').addEvent('blur', function(){
            if(this.value=="") {if (this.oldValue) this.value=this.oldValue;}
        });
    }

    if ($('LoginForm'))    {
        $('LoginForm').addEvent('submit', function(e){
            if ($('EmailInput').value=="" || $('EmailInput').value=="Votre adresse courriel" || $('EmailInput').value=="Your e-mail address") {
                e.stop();
            }
        });

        $('EmailInput').addEvent('click', function(){
            loginFieldsClicked();
        });

        $('EmailInput').addEvent('blur', function(){
            loginFieldsBlurred();
        });

        $('PasswordInput').addEvent('click', function(){
            loginFieldsClicked();
        });

        $('PasswordInput').addEvent('blur', function(){
            loginFieldsBlurred();
        });
    }

    if ($('PasswordResetForm')) {
        $('member_email').addEvent('click', function(){
            if (this.value=="Votre adresse courriel" || this.value=="Your e-mail address") {
                this.oldValue = this.value;
                this.value="";
            }
        });

        $('member_email').addEvent('blur', function(){
            if(this.value=="") {if (this.oldValue) this.value=this.oldValue;}
        });
    }

    var clickable_divs;

    //clickable_divs = $$('.TableHeader').append($$('.TableNavigation'));
    clickable_divs = $$('.TableNavigation');

    clickable_divs.each(function(element) {
        makeClickable(element);
    });

    var divs_to_shrink;

    divs_to_shrink = $$('.Shrink2');
	divs_to_shrink.each(function(element) {
		shrinkTo(element, 26);
	});

    divs_to_shrink = $$('.Shrink3');
	divs_to_shrink.each(function(element) {
		shrinkTo(element, 39);
	});

	divs_to_shrink = $$('.Shrink4');
	divs_to_shrink.each(function(element) {
		shrinkTo(element, 52);
	});

    divs_to_shrink = $$('.Shrink5');
    divs_to_shrink.each(function(element) {
        shrinkTo(element, 65);
    });

    divs_to_shrink = $$('.Shrink6');
    divs_to_shrink.each(function(element) {
        shrinkTo(element, 78);
    });

    divs_to_shrink = $$('.Shrink7');
    divs_to_shrink.each(function(element) {
        shrinkTo(element, 91);
    });

    divs_to_shrink = $$('.Shrink8');
    divs_to_shrink.each(function(element) {
        shrinkTo(element, 104);
    });

    divs_to_shrink = $$('.Shrink9');
    divs_to_shrink.each(function(element) {
        shrinkTo(element, 117);
    });

    divs_to_shrink = $$('.Shrink1').append($$('.RatingBreakdownCourseTeacher'));
    divs_to_shrink.each(function(element) {
        shrinkWithDots(element);
    });

    var divs_to_slide;

    divs_to_slide = $$('.SlideAway');
    divs_to_slide.each(function(element) {
        slideAway(element);
    });

    divs_to_slide = $$('.SlideKeep');
    divs_to_slide.each(function(element) {
        slideKeep(element);
    });
}


function makeClickable(element) {
    element.addEvent('mouseenter', function () {
        element.set('oldColor', element.getStyle('background-color'));
        element.setStyle('background-color', '#63606B');
    });

    element.addEvent('mouseleave', function () {
        element.setStyle('background-color', element.get('oldColor'));
    });

    element.addEvent('click', function() {
        link = element.getElement('a');
        linkOnclick = link.getAttributeNode('onclick').nodeValue.replace("return false;", "");
        eval(linkOnclick);
    });
}


function shrinkTo(element, new_height) {
	element.set('original_height', (element.getSize().y));
	if (new_height < (element.get('original_height') - 10))
	{
		element.set('shrink_height', new_height);
		element.setStyle('height', new_height);
		element.setStyle('overflow-y', 'hidden');
		element.set('shrunk', '1');
		$('ReadMore'+element.get('id')).setStyle('display', 'inline');
	}
}

function shrinkWithDots(element) {
    words = element.get('html').split(' ');
    while (element.getSize().y > 16) {
        words.pop();
        element.set('html', words.join(' ')+"...");
    };
}

function slideAway(element) {
    element_id = element.get('id');
    slides[element_id] = new Fx.Slide(element_id);
    slides[element_id].hide();
}

function slideKeep(element) {
    element_id = element.get('id');
    slides[element_id] = new Fx.Slide(element_id);
    slides[element_id].show();
}

function slideHide(element_id) {
    slides[element_id].slideOut();
}

function slideShow(element_id) {
    slides[element_id].slideIn();
}

function toggleOverflow(the_element) {

    if (the_element.get('shrunk') == '1') {
    	the_element.set('tween', {transition: Fx.Transitions.Quart.easeOut, duration: (300+(the_element.get('original_height')-the_element.get('shrink_height'))*2)+''});
    	the_element.tween('height', (the_element.get('original_height'))+"px");
        the_element.set('shrunk', '0');
        the_read_more_link_element = $('ReadMore'+the_element.get('id'));
        //alert(the_element.get('id')+'ReadMore');
        the_read_more_link_element.set('oldhtml', the_read_more_link_element.get('html'));
        the_read_more_link_element.set('html', the_read_more_link_element.get('html').replace("Read more", "Show less").replace("Lire plus", "Voir moins").replace("View all", "Show less").replace("Voir tous", "Voir moins"));

        //alert(the_read_more_link_element.get('html'));
    } else {
    	the_element.set('tween', {transition: Fx.Transitions.Quart.easeOut, duration: (150+(the_element.get('original_height')-the_element.get('shrink_height')))+''});
		the_element.tween('height', (the_element.get('shrink_height'))+"px");
        the_element.set('shrunk', '1');
        oldhtml = the_read_more_link_element.get('oldhtml');
        the_read_more_link_element.set('html', oldhtml);
    }
}

function loginFieldsClicked() {
	if (($('EmailInput').value=="Votre adresse courriel" || $('EmailInput').value=="Your e-mail address") && ($('PasswordInput').value=="12345678")) {
		$('EmailInput').oldValue = $('EmailInput').value;
		$('EmailInput').value="";
		$('PasswordInput').oldValue = $('PasswordInput').value;
		$('PasswordInput').value="";
	}
}


function loginFieldsBlurred() {
	if($('EmailInput').value=="" && $('PasswordInput').value=="") {
		if ($('EmailInput').oldValue) $('EmailInput').value=$('EmailInput').oldValue;
		if ($('PasswordInput').oldValue) $('PasswordInput').value=$('PasswordInput').oldValue;
	}
}


function submitAjax (element, target_div_id){
    var target_div = $(target_div_id);

    if (target_div)
    {
        element.set('action', encodeURI(element.get('action')));

        //Empty the div and show the spinning indicator.
        //target_div.empty().addClass('ajax-loading');

        //Set the options of the form's Request handler.
        //("this" refers to the $('myForm') element).
        element.set('send', {onComplete: function(response) {
            //target_div.removeClass('ajax-loading');
            target_div.set('html', response);
        }});
        //Send the form.
        element.send();
    }
}

function focusDescription(element) {
    if (element.value=="Spécifiez la nature de l'erreur" || element.value=="Specify the nature of the error") {
        element.oldValue = element.value;
        element.value="";
    }
}

function blurDescription(element) {
    if(element.value=="") {if (element.oldValue) element.value=element.oldValue;}
}

function focusComments(element) {
    if (element.value.indexOf("Describe your experience with this ") != -1 || element.value.indexOf("Décrivez votre éxperience avec ce ") != -1) {
        element.oldValue = element.value;
        element.value="";
        element.style.color = '#000000';
    }
}

function blurComments(element) {
    if(element.value=="") {
        if (element.oldValue) element.value=element.oldValue;
        element.style.color = '#7F8183';
    }
}

function showShadowBox (href, width, height) {
    var req = new Request({
        method: 'get',
        url: href,
        data: { 'do' : '1' },
        onComplete: function(response) { showShadowBoxWithContent(response, width, height); }
    }).send();
}

function showWideShadowBox (href) {
    showShadowBox(href, 600, 350);
}

function showReportShadowBox (href, type) {
    if (type == "rating") {
        showShadowBox(href, 250, 150);
    } else {
        showShadowBox(href, 384, 175);
    }
}

function showStudentsShadowBox (href) {
    showShadowBox(href, 500, 400);
}


function showShadowBoxWithContent(content, the_width, the_height) {
    Shadowbox.open({
        content:    content,
        player:     "html",
        title:      "",
        width:      the_width,
        height:     the_height
    });
}

function showLoginInterface() {
	$('LoginBox').setStyle('top', '-200px');
	$('LoginBox').setStyle('display', 'block');
	$('LoginBox').set('tween', {transition: Fx.Transitions.Quart.easeOut, duration: '500'});
	$('LoginBox').tween('top', 0);
}

function hideLoginInterface() {
    $('LoginBox').set('tween', {transition: Fx.Transitions.Quart.easeOut, duration: '500'});
    $('LoginBox').tween('top', - +$('LoginBox').getSize().y);
}

function showInfoBox() {
    var fx = new Fx.Morph('InfoBox', {
        duration: 500,
        transition: Fx.Transitions.Quart.easeOut
    });

    fx.start({
    }).chain(function(){
        this.start.delay(500, this, {
            'top': 0
        });
    });
}

function hideInfoBox() {
    document.cookie='hideyourbox=please;path=/;';

    var fx = new Fx.Morph('InfoBox', {
        duration: 500,
        transition: Fx.Transitions.Quart.easeOut
    });

    fx.start({
        'top': - (7+$('InfoBox').getSize().y)
    }).chain(function(){
        $('InfoBoxContainerContainer').setStyle('display', 'none');
    });
}


var isSchoolFacultyFormShown = false;
function toggleDisplaySchoolFacultyForm() {
    if (isSchoolFacultyFormShown) {
        hideSchoolFacultyForm();
        isSchoolFacultyFormShown = false;
    } else {
        showSchoolFacultyForm();
        isSchoolFacultyFormShown = true;
    }

}

function showSchoolFacultyForm() {
    var fx = new Fx.Morph('SchoolFacultyFormContainer', {
        duration: 500,
        transition: Fx.Transitions.Quart.easeOut
    });

    fx.start({
        'height': 115
    });
}

function hideSchoolFacultyForm() {
    var fx = new Fx.Morph('SchoolFacultyFormContainer', {
        duration: 500,
        transition: Fx.Transitions.Quart.easeOut
    });

    fx.start({
        'height': 0
    });
}

function clickRow(element) {
    element = $(element);
    window.location = element.getElement('a').get('href');
}

function highlightRow(element) {
    element = $(element);
    element.setStyle('background', '#EFEFEF');
    element.getElement('a').setStyle('text-decoration', 'underline');
}

function lowlightRow(element) {
    element = $(element);
    element.setStyle('background', '#FFFFFF');
    element.getElement('a').setStyle('text-decoration', 'none');
}

function getScrollY() {
  var scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
  }
  return scrOfY;
}

function facebookLogin() {
    if (getCookie('facebook') != 'login') {
        setCookie('facebook', 'login', null, '/');
    }

    FB.getLoginStatus(function(response) {
        if (response.status != "connected") {
            FB.login(null, {perms:'email, user_birthday'});
        } else {
            window.location = window.location;
        }
    });
}

function setCookie(CookieName, CookieVal, CookieExp, CookiePath, CookieDomain, CookieSecure) {
    var CookieText = escape(CookieName) + '=' + escape(CookieVal); //escape() : Encodes the String
    CookieText += (CookieExp ? '; EXPIRES=' + CookieExp.toGMTString() : '');
    CookieText += (CookiePath ? '; PATH=' + CookiePath : '');
    CookieText += (CookieDomain ? '; DOMAIN=' + CookieDomain : '');
    CookieText += (CookieSecure ? '; SECURE' : '');

    document.cookie = CookieText;
}

function getCookie(cookie_name) {
    var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );

    if ( results )
        return ( unescape ( results[2] ) );
    else
        return null;
}

function findPosX(obj)
{
    var curleft = 0;
    if(obj.offsetParent)
        while(1)
        {
            curleft += obj.offsetLeft;
            if(!obj.offsetParent)
                break;
            obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
}

function findPosY(obj)
{
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
            curtop += obj.offsetTop;
            if(!obj.offsetParent)
                break;
            obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
}

