_site_language_regexp = /jp\.jpg/;

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(){

    $("#course-search-loader").show();

    $('#bookBox').load(
        $('#courseBookForm').attr('action'),
        $('#courseBookForm').serialize(),
        function(){
            $("#course-search-loader").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);
                $(allTestimonials[index]).show();

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

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

function fixTopMenu(){

    //new value
    var free = 850;

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

    if(free > 0){

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


        $('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;

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

        });

        if(new_width > 850){
            var substr = Math.abs(850 - new_width);

            $('a.level1:last').css("padding-left", (parseInt($('a.level1:last').css("padding-left")) - substr)+"px");
        }
}

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 weather(id){

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

    if($('#main_teasers').children().length > 0){

        $('.infoBoxTitle:first').addClass('hover');

        var _slider_changes = 0;

        $('#main_teasers').nivoSlider({
		effect: 'fade',
                pauseTime: 4000,
                directionNav: false,
		controlNav: false,
		keyboardNav: false,
                pauseOnHover: false,
                onChange: function(){
                    var t = $('.infoBoxTitle')[$('#main_teasers').data('nivo:vars').currentSlide];

                    $('.infoBoxTitle').removeClass('hover');
                    $(t).addClass('hover');

                    _slider_changes++;

                    if(_slider_changes > 0 && $('#main_teasers').data('nivo:vars').currentSlide == 0){
                        $('#main_teasers').data('nivoslider').stop();
                    }
                }
	});

	$('#slider_titles div').mouseover(function(){
		var ids = $(this).attr('id').split('-');
		$('#main_teasers').data('nivoslider').openSlide(ids[1]);
	});

	$('#slider_titles div').mouseout(function(){
            if(_slider_changes < 4){
		$('#main_teasers').data('nivoslider').start();
            }
	});
    }


    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();
            }
        });
    }

    fixTopMenu();
    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');
    }

    _testimonialsLoop = setTimeout(function(){
        $('div.tEntry:first').fadeOut(3000);
        toggleTestimonials(1);
    }, 20000);

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


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

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

    $('ul.sf-menu li ul').each(function(i,e){
        $(e).children('li').width($(e).parent().width()-2);
    });

});


