$(document).ready(function() {

    activeTab();
    hoverMenu();
    pullImages();

    $(function() {
        setInterval("slidePic()", 3000);
		setInterval("bannerRotation('.set1')", 3000);
		setInterval("bannerRotation('.set2')", 5000);
		
    });

    sectionPic();
    imgLinks();
    langActive();
    centerImage();


});

if (!Array.indexOf) {
    Array.prototype.indexOf = function(obj) {
        for (var i = 0; i < this.length; i++) {
            if (this[i] == obj) {
                return i;
            }
        }
        return -1;
    }
}

function bannerRotation(a) {

	var $start = $(''+a+' a.start');
	var $next = $start.next().length ? $start.next() : $(''+a+' a:first');

	$start.removeClass('start');
	$next.addClass('start');

}

function pullImages() {

    if ($('.slide').html() != '') {
        $('.slide').html('');
    }

    nums = new Array();
    do {
        rndNum = randomRange(1, 5);
        if (nums.indexOf(rndNum) == -1) {
            nums.push(rndNum);
        }
    }
    while (nums.length < 3)

    for (i = 0; i <= 2; i++) {

        if ($('.adults_slide').length > 0) {
            $('.slide').append('<img src="/img/adultos/cursos-idiomas-extranjero_' + nums[i] + '.jpg" />');
        }
        else
        if ($('.jovenes_slide').length > 0) {
            $('.slide').append('<img src="/img/jovenes/cursos-idiomas-extranjero_' + nums[i] + '.jpg" />');
        }
        else
        if ($('.becas_slide').length > 0) {
            $('.slide').append('<img src="/img/becas-mec/becas-mec_' + nums[i] + '.jpg" />');
        }

        else {
            $('.slide').append('<img src="/img/jovenes/cursos-idiomas-extranjero_' + nums[i] + '.jpg" />');
        }
    }

    $('.slide img:first').addClass('start');

}

function hoverMenu() {

    $("#main_menu h2").each(function() {
        $(this).hover(function() {
            $(this).addClass('hover');
            $(this).animate({
                'marginLeft': '5px'
            }, {
                duration: 'fast',
                easing: 'easeOutSine'
            });
        }, function() {
            $(this).removeClass('hover');
            $(this).animate({
                marginLeft: '0'
            }, {
                duration: 'fast',
                easing: 'easeOutSine'
            });
        })
    });
}

function randomRange(minVal, maxVal, floatVal) {
    var randVal = minVal + (Math.random() * (maxVal - minVal));
    return typeof floatVal == 'undefined' ? Math.round(randVal) : randVal.toFixed(floatVal);
}

function slidePic() {

    var $start = $('.slide img.start');
    var $next = $start.next().length ? $start.next() : $('.slide img:first');

    $start.addClass('last');
    $start.removeClass('start');

    $next.css({
        opacity: 0.0
    }).addClass('start').animate({
                                     opacity: 1.0
                                 }, 700, function() {
        $start.removeClass('start last');
    })

}

function sectionPic() {

    $('#linksnav a').each(function() {

        $(this).hover(function() {

            if ($(this).hasClass('des1')) {
                $('.slide').append('<img src="/img/adultos.jpg" class="extra" />');
            }
            else
            if ($(this).hasClass('des2')) {
                $('.slide').append('<img src="/img/jovenes.jpg" class="extra" />');
            }
            else
            if ($(this).hasClass('des3')) {
                $('.slide').append('<img src="/img/becasstudents.jpg" class="extra" />');
            }
            else
            if ($(this).hasClass('des4')) {
                $('.slide').append('<img src="/img/camps.jpg" class="extra" />');
            }
            if ($(this).hasClass('des5')) {
                $('.slide').append('<img src="/img/oferta.jpg" class="extra" />');
            }

        }, function() {
            $('img.extra').remove();
        });
    });

}

function imgLinks() {
    $('.blockfront').each(function() {
        var uLink = $('a.ulink', this).attr('href');
        if (!$(this).hasClass('blockoffer')) {
            $(this).click(function() {
                window.location = uLink;
            });
        }


        $(this).hover(function() {
            $(this).css('border', '1px solid #65A6F7');
            $(this).css('border-bottom', '3px solid #65A6F7');
        }, function() {
            $(this).css('border', '1px solid #C7E7FF');
            $(this).css('border-bottom', '3px solid #C7E7FF');
        });

    });
}

//Old functions start from here

function modedImage() {
// modded random image script
//random image
    var imgTotal = 6;
    var rndNum = Math.round(Math.random() * (imgTotal - 1)) + 1;

    var imgTotal_3 = 3;
    var rndNum_3 = Math.round(Math.random() * (imgTotal_3 - 1)) + 1;

    var imgTotal_4 = 2;
    var rndNum_4 = Math.round(Math.random() * (imgTotal_4 - 1)) + 1;


    $("div.oferta1").css("background", "url(/img/oferta1_promo.jpg) no-repeat center");
    $("div.oferta2").css("background", "url(/img/oferta2_promo.jpg) no-repeat center");
    $("div.oferta3").css("background", "url(/img/oferta3_" + rndNum_3 + ".jpg) no-repeat center");
    $("div.oferta4").css("background", "url(/img/brochure_" + rndNum_3 + ".jpg) no-repeat center");

}

function offertasBgChange() {
    /* Backgrounds change on mouseover */

    $("#ofertas div.bgfix").each(// For each block, run this code.
            function(intIndex) {

                // Bind the event.
                $(this).bind("mouseover", function() {
                    $(this).addClass('mover');
                });

                $(this).bind("mouseout", function() {
                    $(this).removeClass('mover');
                });


            });


    /* Backgrounds change on mouseover without the bottom part */
    $("#ofertas div.bgfix-nobot").each(// For each block, run this code.
            function(intIndex) {

                // Bind the event.
                $(this).bind("mouseover", function() {
                    $(this).addClass('mover-nobot');
                });

                $(this).bind("mouseout", function() {
                    $(this).removeClass('mover-nobot');
                });


            });
}

function activeTab() {

    if ($('div.t1_active').length > 0) {
        $('li.t1').addClass('active');

    }
    else
    if ($('div.t2_active').length > 0) {
        $('li.t2').addClass('active');

    }
    else
    if ($('div.t3_active').length > 0) {
        $('li.t3').addClass('active');

    }
    else
    if ($('div.t4_active').length > 0) {
        $('li.t4').addClass('active');

    }
    else
    if ($('div.t5_active').length > 0) {
        $('li.t5').addClass('active');

    }
    else
    if ($('div.t6_active').length > 0) {
        $('li.t6').addClass('active');

    }
    else
    if ($('div.t7_active').length > 0) {
        $('li.t7').addClass('active');

    }
    else
    if ($('div.t8_active').length > 0) {
        $('li.t8').addClass('active');

    }
    else
    if ($('div.t9_active').length > 0) {
        $('li.t9').addClass('active');

    }

}

function langActive() {

    if ($('html').attr('lang') == 'es') {
        $('#topnav a.es').css({
            opacity: '0.5'
        });
    }
    else
    if ($('html').attr('lang') == 'en') {
        $('#topnav a.us').css({
            opacity: '0.5'
        });
    }
}

function centerImage() {

    $('#midle img').each(function() {
        if ($(this).attr('height') == '110' & $(this).attr('width') == '390') {
            $(this).wrap('<center>');
        }
    });

}

