$(document).ready(function() {

    var url = location.href;
    if (url.indexOf("?") < 0){ //in case it's the first time
        $.facebox({ajax: 'banner.jsp'});
    }

    $(".www").mouseover(function() {
        var id = $(this).attr("id");
        $(this).attr("src", "images/" + id + "up.jpg");
    });

    $(".www").mouseout(function() {
        var id = $(this).attr("id");
        $(this).attr("src", "images/" + id + ".jpg");
    });

    $(".www").livequery("click", function() {
        var page = $(this).attr("title");
        if (page != null && page != undefined){
          document.location = "index.jsp?page="+page;
        }
    });
    
    $(".common").livequery("click", function(){
         var id = $(this).attr("id");
         var area = $(this).attr("alt");
         if (id != null && id != undefined && id.indexOf(".jsp") < 0 && id.indexOf("http://") < 0 && id.indexOf("https://") < 0 && area != null && area != undefined ){
             if (id == "shop") $(".textmain").load("shop.jsp"); else {
                 document.location = area+".jsp?page="+id;
             }
         }
         if (id.indexOf(".jsp") >= 0 || id.indexOf("http://") >= 0 || id.indexOf("https://") >= 0) document.location = id;         
    });
   
    $("#testimonials").load("randomtestimonial.jsp", function(){
         $("#testimonials").fadeIn("slow");
    });
    $("#testimonials").everyTime(10000, function(){
        $("#testimonials").fadeOut("slow", function(){
            $("#testimonials").load("randomtestimonial.jsp", function(){
              $("#testimonials").fadeIn("slow");
            });
        });
    });
    
    $(".webbuttons").livequery("click", function(){
        var title = $(this).attr("title");
        if (title == "subscribe"){
            $(".textmain").load("subscribe.html");
        } else if (title == "book now"){
            $(".textmain").load("shop.jsp");
        } else if (title == "free"){
            $(".textmain").load("free.html");
        }
    });
    
    $(".ttbutt").livequery("click", function(){
        var course = $(this).attr("title");
        var body = $(this).attr("alt");
        $(".textmain").load("timetable.jsp",{"course":course,"body":body},function(postback){
            
        });
    });
    
    $("#continueshopping").livequery("click",function(data){
        var str = $("#shopform").serialize();
        $.post("getform",{"products":str,"origin":"belfast"},function(data){
            if (data.indexOf("ERROR") < 0){
                $(".textmain").html("<br/><br/><H3>Thankyou for your purchase. You will be asked to complete your payment in the next step</h3><br/>"+data);
            } else {
                alert("Sorry an error occured while processing your request. Please try again later");
            }
        })                    
    });
    
    $("#subscribeButton").livequery("click", function(data){
       var name = $("#name").val();
       var email = $("#email").val();
       var where = $("#where").val();          
       var course = $("#course").val();
          $.post("subscribe",{"name":name,"email":email,"where":where,"course":course}, function(data){
           if (data.toString().indexOf("ERROR") < 0){
              $(".textmain").html("<br><br>Thanks for subscribing to our newsletter. <br> If you wish to unsubscribe feel free to send an email to <a href='mailto:root@richardclarkeacademy.com'>root@richardclarkeacademy.com</a><br><br>");
           } else {
              $(".textmain").html("<br><br>Sorry but an error has occured while processing your request. <br> Please try again later or contact the administrator on <a href='mailto:root@richardclarkeacademy.com'>root@richardclarkeacademy.com</a><br><br>");
           }
       });            
    });
    
});
