function show_upgrades(show_id){

    $("div[id^=ug_]").hide();
    $(show_id).show();

}

function confirmDelete(a){
    if(confirm(a)){
        return true;
    }else{
        return false;
    }
}

function enableButton(a){
    $(a).attr('disabled', false);
}

function toggleCallTime(){
    if($("#ContactType_1").is(':checked')){
        $("#CallTimeContainer").show().highlight()
    }else{
        $("#CallTimeContainer").hide()
    }
}

function submitCompact(){

    $("#search_loader").show();

    $("#searchBox").load(
        $("#courseSearchForm").attr('action'),
        $("#courseSearchForm").serialize(),
        function(){
            $("#search_loader").hide();
        }
    );

}

function submitFull(){

    $("#searchBoxLoadingSmall").show();

    $('#bookBox').load(
        $('#courseBookForm').attr('action'),
        $('#courseBookForm').serialize(),
        function(){
            $("#searchBoxLoadingSmall").hide();
        }
    );
        
}

function getSchoolsPriceBrochures(year, lang){
	$('#schools_container').load('/ajax.php?p=get_schools_price_brochures&year='+year+'&lang='+lang);
}

function showBrochuresDownloadLinks(){
	$('#bdl').load('/ajax.php?p=get_brochures_download_links&year='+$('#year').val()+'&school_id='+$('#school_id').val());
}

function getSchoolsByCourseType(CourseTypeID){
	$('#sctdest').load('/ajax.php?p=get_schools_by_course_type&CourseTypeID='+CourseTypeID);
}

function switchTranslationTo(lang){
	//Get all the editors
	var editors = $('div.fck'); 

  	//Hide them all
	editors.each(function(i,e){
            $(e).hide();
        });

	//show the selected one
	$('#edit_'+lang).show();
	
	$('#edit_'+lang).effect('highlight');
	
	//update the switcher links
	//Get all the switcher links
	var sw = $('a.switcher'); 

  	//Hide them all
	sw.each(function(e){
		$(e).removeClass("txt-red txt-big");
	});
	$('#switch_'+lang).addClass('txt-red txt-big');
}

function openSchoolCourses(id){
	$('div.pcourses').each(function(i,e){
            $(e).hide();
        });
	$(id).toggle();
}

function spCloseCourses(SchoolID){
	
	var check_school = false;

	$('#input.cb_'+SchoolID).each(function(i,e){
		if($(e).is(':checked')){
			check_school = true;
		}
	});
	
	if(check_school == true){
		$('#participating_schools['+SchoolID+']').attr('checked', true);
	}else{
		$('#participating_schools['+SchoolID+']').attr('checked', false);
	}
	
	$('#sc_'+SchoolID).fadeOut();
}


function checkCourses(SchoolID){
	
	if($('#participating_schools['+SchoolID+']').is(':checked')){
		//check all the courses
		$('input.cb_'+SchoolID).each(function(i,e){
			if(!$(e).is(':checked')){
                            $(e).attr('checked', true);
			}
		});
	}else{
		//uncheck all the courses
		$('input.cb_'+SchoolID).each(function(i,e){
			if($(e).is(':checked')){
                            $(e).attr('checked', false);
			}
		});
	}
}

function checkSchools(){
	$('input.spschools').each(function(i,e){

		if($(e).is(':checked')){
			//uncheck!
			$(e).attr('checked', false);
		}else{
			$(e).attr('checked', true);
		}		
		
		e.id.gsub(/\d+/, function(m){
			//check the school's courses
			checkCourses(m[0]);
		});
	});
}

function setToday(){
	var d = new Date();
	
	//a bit more work with the month
	var _m = d.getMonth();
	
	var _m = parseInt(_m) + 1;

	if(_m < 10){
		_m = '0'+_m;
	}
	
	$('#s_day').val() = d.getDate();
	$('#s_month').val() = _m;
	$('#s_year').val() = d.getFullYear();
}

function checkUniqueCode(code){

	if(code == ''){
		alert('Type the special offer code, please!');
		$('#OfferCode').focus();
	}else{
            $.ajax({

                url: '/ajax.php?p=spucode&c='+code,
                type: 'GET',
                success: function(data){
                    alert(data);
                },
                error: function(data){
                    alert('Error!\nThe code can not be verified!');
                }

            });

	}
}

//we will store the previous opened panel here
var prev_slide_panel = '';


function venSlidePanels(current){

        if( (prev_slide_panel != current) && (prev_slide_panel != '') ){
                $(prev_slide_panel).hide();
        }

        //toggle the current one
        $(current).slideToggle();
        prev_slide_panel = current;

}

function toggleTestimonials(index){

    allTestimonials = $('div.tEntry');


    if($(allTestimonials).length > 0){

        if(index < (allTestimonials.length - 1)){

            if(allTestimonials[index] != 'undefined'){

                $(allTestimonials[index]).fadeIn(10000);

                setTimeout(function(){
                    $(allTestimonials[index]).fadeOut(3000);
                    toggleTestimonials(index+1);
                }, 12000);
            }
        }
    }
}

function toggleLocalTestimonials(index, wait_time){

    allLocalTestimonials = $('div.testimonial_data');


    if($(allLocalTestimonials).length > 0){

        if(index < (allLocalTestimonials.length - 1)){

            if(allLocalTestimonials[index] != 'undefined'){

                $(allLocalTestimonials[index]).fadeIn(10000);

                setTimeout(function(){
                    $(allLocalTestimonials[index]).fadeOut(3000);
                    toggleLocalTestimonials(index+1, 12000);
                }, wait_time);
            }
        }
    }
}


function fixSliderControls(){
    if(($("a.infoBoxTitle").length > 0) && ($('#lbControlls') != undefined)){
        $('#lbControlls').prev().css('margin-top', '55px');
    }
}

function fixMenu(menu_id, width){

    //new value
    var free = width;

    //the menu is too long, make the paddings smaller
    $('#'+menu_id+' a.level1').each(function(i, e){
        free -= parseInt($(e).css('width'));
    });

    if(free > 0){

        var p = parseInt(Math.round(free/$('#'+menu_id+' a.level1').length/2));

        if(menu_id == 'sub_navigation'){
            
            p = p-2;

            $('#'+menu_id + ' a.level1').each(function(i,e){
                    $(e).css("padding-left", (p-1)+"px");
                    $(e).css("padding-right", (p-1)+"px");

                free -= p*2;
            });

        }else{
            $('#'+menu_id + ' a.level1').each(function(i,e){
                    $(e).css("padding-left", p+"px");
                    $(e).css("padding-right", p+"px");

                free -= p*2;
            });
        }

    }

    var new_width = 0;

    $('#'+menu_id + ' a.level1').each(function(i,e){
            new_width += $(e).width() + parseInt($(e).css("padding-left")) + parseInt($(e).css("padding-right"));

    });

    //var substr = Math.abs(new_width - width);
    //$('#'+menu_id + ' a.level1:last').css("padding-left", (parseInt($('#'+menu_id + ' a.level1:last').css("padding-left")) - substr)+"px");
    $('#menu a.level1:last').css("margin-left", '-4px');
    $('#sub_navigation a.level1:last').css("margin-left", '-6px');



}

function showContent(element, print){

	n = window.open();
	n.document.write($(element).html());
	n.document.close();

	if(print == 1){
		n.print();
	}
}

function switchLanguagePane(code){
    $('div.language_group').each(function(i,e){
        $(e).hide();
    });
    $(code).show();
}

function setupFooterLinks(){
    $('div.linksBlock ul.wt:first').show();

    $('div.linksBlock h4').each(function(index, element){
        $(element).click(function(event){

            $('div.linksBlock ul').each(function(i,e){
                $(e).hide();
            });
            $('div.linksBlock ul.wt:first').show();

            $(element).next('ul').fadeIn(1000);

        });
    });
}

function toggleFlags(){

    _block = 0;

    $('#languageFlagsLinksInner a').each(function(i,e){
            if($(e).css('display') == 'block'){
                _block += 1;
            }
    });

    if(_block == 1){

        $('#languageFlagsLinksInner a').each(function(i,e){
                $(e).fadeIn();
        });

    }else{

        $('#languageFlagsLinksInner a').each(function(i,e){

            var f = $(e).children().first();

            if(!_site_language_regexp.test($(f).attr('src'))){
                $(e).fadeOut();
            }
        });

    }

}

function openLightbox(){
    $('a[rel*=lightbox]:first').click();
}

function scrollPlaylist(target, duration){
    $('#playlist').scrollTo(target, duration);
}

function stopScrolling(){
    $('#playlist').stop();
}

function lastTwitt(){

    $.ajax({
        url: '/ajax.php?resource=lastTwitt',
        type: 'GET',
        beforeSend: function(){
            $('#lastTwitt').append('<img src="/images/ajax-loader.gif" width="20" style="margin: 10px 0 10px 120px;" />');
        },
        success: function(data){
            $('#lastTwitt').html(data);
        }
    });

}

function MM_goToURL(){
	var i,
	args=MM_goToURL.arguments;
	document.MM_returnValue=false;

	for(i=0;i<(args.length-1);i+=2){
		eval(args[i]+".location='"+args[i+1]+"'");
	}
}

function onlineResourcesMainSubmit(){
    
}

function weather(id){

    $.ajax({
        url: '/sejours-linguistiques/ajax.php?resource=weather',
        type: 'GET',
        data: {id: id},
        beforeSend: function(){
            $('#school_weather').append('<img src="/images/ajax-loader-white.gif" style="margin: 10px 0 20px 100px;" />');
        },
        success: function(data){
            $('#school_weather').html(data);
        }
    });

}

function setupOnlineTest(){

    $('div.online_test_question').hide();
    $('div.questions_navigation:first').hide();
}

function initOnlineTest(){

    filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

    if($('#test_student_name').val() == ''){
        alert('Type your name, please!');
        $('#test_student_name').focus();
        return false;
    }else if(!filter.test($('#test_student_email').val())){
        alert('Type your email address, please!');
        $('#test_student_email').focus();
        return false;
    }else{

        $('div.online_test_meta').hide();

        $('div.online_test_question').hide();

        var first_question = $('div.online_test_question:first');

        $(first_question).show().addClass('current_question');

        $('div.questions_navigation:first').show();

        return true;
    }
}

var qind = 1;

function onlineTestAction(caller){

    var current_active = $("div[class*=current_question]:first");

    if(qind < $('div.online_test_question').length){

        var nx = $(current_active).next();

        $(current_active).removeClass('current_question').hide('scale');

        $(nx).show().addClass('current_question');

        qind++;

        $('#question_index').html(qind);

    }else{

        $.ajax({
            url: $('#onlinetestform').attr('action'),
            data: $('#onlinetestform').serialize(),
            type: 'POST',
            beforeSend: function(){
                $('div.online_test:first').html('<div style="text-align: center;"><h1>Processing your test results...</h1><img src="/images/ajax-squares.gif" width="43" height="11" alt="loading" /></div>');
            },
            success: function(result){
                window.location.href = result;
            }
        });
    }

}

$(function(){
    $('#booking_tabs').tabs();

    fixMenu('menu', 890);
    fixMenu('sub_navigation', 980);
    fixSliderControls();
    setupFooterLinks();
    //lastTwitt();

    if($('a[rel*=lightbox]').length > 0){
        $('a[rel*=lightbox]').lightBox({
            fixedNavigation: true
        });
    }

    if($('a[rel*=external]').length > 0){
        $('a[rel*=external]').attr('target', '_blank');
    }

    $('#menu ul.sf-menu li ul').hover(function(){
        $(this).parent().children('a:first').css('color', '#ffffff');
    });


    $('#menu ul.sf-menu li ul').mouseout(function(){
        $('#menu ul.sf-menu li a.level1').css('color', '#0050a2');
    });

    $('#menu a.level2').hover(function(){
        $(this).parent().parent().parent().children('a.level1').css('color', '#fff');
    });

    $('#menu .sf-menu li ul li').each(function(i,e){
        var new_width = $(e).parent().parent().width() - 7;

        if($(e).width() < new_width){
            $(e).width(new_width);
        }
    });

    $('#sub_navigation .sf-menu li ul li').each(function(i,e){
        var new_width = $(e).parent().parent().width() - 7;

        if($(e).width() < new_width){
            $(e).width(new_width);
        }
    });

    if($('#languageFlagsLinksInner a').length > 0){


        $('#languageFlagsLinksInner a').each(function(i,e){

            $(e).css('display', 'block');

            var f = $(e).children().first();

            if(!_site_language_regexp.test($(f).attr('src'))){
                $(e).hide();
            }
        });
    }

});


