// JavaScript Document
var current = "";
var carouselGlobal;
/*
function mycarousel_initCallback(carousel) {
	jQuery('#moveEventLeftBt').bind('click', function() {
        carousel.prev();
        return false;
    });

    jQuery('#moveEventRightBt').bind('click', function() {
        carousel.next();
        return false;
    });
	
	jQuery('#moveEventLeftBt').bind('mouseover', function() {
        jQuery(this).attr("src",'images/buttons/blue-arrow-left.png');
        return false;
    });
	
	jQuery('#moveEventRightBt').bind('mouseover', function() {
        jQuery(this).attr("src",'images/buttons/blue-arrow-right.png');
        return false;
    });
	
	jQuery('#moveEventLeftBt').bind('mouseout', function() {
        jQuery(this).attr("src",'images/buttons/gray-arrow-left.png');
        return false;
    });

    jQuery('#moveEventRightBt').bind('mouseout', function() {
        jQuery(this).attr("src",'images/buttons/gray-arrow-right.png');
        return false;
    });
};
*/

function home_initCallback(carousel)
{
	carouselGlobal = carousel;
	$('#moveItemLeftBt').bind('click', function() {
        carousel.prev();
        return false;
    });

    $('#moveItemRightBt').bind('click', function() {
        carousel.next();
        return false;
    });
	
	//$('div.featuredThumbnailItem').bind('mouseover', function() {
        
     //   return false;
    //});

}

function changeLanguage(l) {
	//window.location = '/language/'+l;
	alert("Dan is-sit eletroniku għadu qed jigi tradott għall-Malti u se jkun disponibbli fil-ġejjieni qrib.");
}

function cleanMenu(x,e) {
	if(x == 1) {
		$(".activeLink").children().css('color','#757575');
		$(".activeLink").attr('class','');
	}
	else {
		$(e).parent().parent().children().children().attr('class','');
		$(e).parent().parent().children().children().children().css('color','#757575');
	}
}

function openBranch(id,child) {
	if(id!=0) {
		$(document).ready(function() { $("#subItem"+id+" ul:first").slideDown('normal'); });
		$("#subItem"+id).children().attr('class','activeLink');
		$("#subItem"+id).children().children().css("color","#E46E33");
		$("#subSubItem"+child).children().children().css("color","#E46E33");
		
	}
}

function getSubsMenu(p,c,t) {
	if(p != null && p > 0 && c != null && c.length > 1 && current != c) {
		$.ajax({
			   type: "POST",
			   url: "ajax/getSubs.php",
			   data: "p="+p+"&t="+t,
			   success: function(msg){
				   	current = c;
					$("#middleColumn").html(msg);
			   },
				error:function (xhr, ajaxOptions, thrownError){
					alert("Error - Could not retrieve content.");
				}
			 });
	}
	
}

function initSlideMenu(id,s) {
	
  $('#'+id+' ul').hide();
  
  if(s) {
  	$('#'+id+' ul:first').show();
  }
  
  $('#'+id+' li p').click(
    function() {
		  var check = $(this).children().attr('name');
		  if($("#subItem"+check).children().attr('class') != "activeLink") {
				cleanMenu(1,null);
		  }
		  else {
				cleanMenu(2,this); 
		  }
		
		  $(this).children().css("color","#E46E33");
		  $(this).attr('class', 'activeLink');
		  
		  if(id == "resourcesContentMenu" && $(this).parent().children('ul').is(':visible'))
		  {
			  $(this).parent().children('ul:visible').slideUp('normal');
			  $(this).children().css('color','#757575');
			  $(this).attr('class', '');
		  }
		  
		  if(id == "resourcesContentMenu") {
		  	fleXenv.updateScrollBars();
		  }
		  
		  var checkElement = $(this).next();
		  if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
			return false;
			}
			
		  if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
			
			$("#"+$(this).parent().parent().parent().attr('id')+" li").children('ul').each(function() {
				$(this).slideUp('normal');
			});
			
			checkElement.slideDown('normal');
			return false;
			}
      }
    );
 }
 
function onProductChange(selected,e){    
    var output = "";
	
    if(selected != 0){
		$('#courseDetails >option').remove();
		$('#courseDetails').append(
			$('<option></option>').val("-1").html("Loading available locations..")
		);
		
		$.ajax({
			   type: "POST",
			   url: "../ajax/getLocations.php",
			   data: "cid="+selected+"&e="+e,
			   success: function(msg){

				 eval(msg);
				 $('#courseDetails >option').remove();
				 
				 $.each(elements, function(val, text) {
					$('#courseDetails').append(
						$('<option></option>').val(val).html(text)
					);
				});
				
				return false;
			   },
			   error: function() {
					alert("Failed retrieving data.");   
					return false;
			   }
		});
	}
}

$(document).ready(function() {

	Cufon.replace('.title');
	Cufon.replace('.newsTitle');
	Cufon.replace('h2');
	Cufon.replace('#sectionTitle');
	
	if ($('#resourcesContentMenu').length) {
		initSlideMenu('resourcesContentMenu',0);
	}
	
	if ($('#sectionSubMenuContent').length) {
		initSlideMenu('sectionSubMenuContent',0);
	}
	
	if($('#addChild').length) {
		$('#addChild').bind('click',function() {
			addParticipant();
		});	
	}
	

	$("#featuredItems").jcarousel({
        scroll: 1,
		wrap: "circular",
		auto: 0,
		animation:0,
		start: 1,
        initCallback: home_initCallback,
		itemLoadCallback : {onBeforeAnimation: function() {Cufon.replace('.title');}} ,
        buttonNextHTML: null,
        buttonPrevHTML: null,
		visible: 1
    });
	

	
	$('#sectionSubMenu ul li p').live('mouseover mouseout', function(event) {
		if (event.type == 'mouseover') {
			$(this).children().css('color','#E46E33');
		
		} else {
			if($(this).attr('class') != 'activeLink') {
				$(this).children().css('color','#757575');
			}
		}

        return false;
    });

	
	$('div.sectionItem').live('mouseover', function() {
		$(this).children(".sectionImage").children(".sectionSubsTitle").css('background-image','url(images/orange-tab-subs.png)');
		$(this).children(".sectionImage").children(".sectionSubsTitle").css('color','white');
        return false;
    });
	
	$('div.sectionItem').live('mouseout', function() {
		$(this).children(".sectionImage").children(".sectionSubsTitle").css('background-image','url(images/gray-tab-subs.png)');
		$(this).children(".sectionImage").children(".sectionSubsTitle").css('color','white');
        return false;
    });
});

var counterParticipants = 0;
function addParticipant() {
	var className = 'nameFields';
	if($('.'+className).length && counterParticipants <8) {
		var obj = $('.'+className);
		var returned = $('<div class="'+className+'">'+obj.html()+'</div>').insertBefore('.newField');
		
		$(returned).children('input').val('');
		$(returned).children('select').val('');
		
		counterParticipants++;
	}
}

