$(document).ready(function() {
	 
	 $('ul.sf-menu').superfish();
	 
	 $('ul.sf-vertical').superfish({
	
			 animation: {height:'show'},   // slide-down effect without fade-in 
			 delay:     1200               // 1.2 second delay on mouseout 
	 });

});



function getTemplateForPlacement(){
	
	var templateUID = $('#templateUID').val();
	var url = '/ajax/ajGW.php?action=renderTemplateCreate&templateUID=' + templateUID;

	$.ajax({
		  url: url,
		  dataType: 'json',
		  success: function(response){
			  
			  if(response.userError) alert(response.userError);
			  else if(response.data){
			
				  alert('Attaching Template to Placement');
				  $('#expatTemplate').html(response.data);
				  
			  }
			  
		  }
	});
	
	var url = '/ajax/ajGW.php?action=getTemplatePrice&templateUID=' + templateUID;
	
	$.ajax({
		  url: url,
		  dataType: 'json',
		  success: function(response){
			  
			  if(response.userError) alert(response.userError);
			  else if(response.data){

				  $('#expatPrice').val(response.data);
				  /*$('.templateTextArea').htmlarea({
					  
					  toolbar: ["html", "|", "forecolor", "|", "bold", "italic", "underline", "|", "h1", "h2", "h3", "|", "link", "unlink"],
					  css: '/styles/jHTMLArea/jHtmlArea.Editor.css'
						  
				  });*/
			  }
			  
		  }
	});
	
}

function searchPlacements(){

	var organisationUID = $('#organisationUID').val();

	var categoryJSON = '[';
	
	if($('#categoryJSON').val()){
	
		$.each($('#categoryJSON').val(), function(index,value){
			categoryJSON += '"' + value + '",';
		});
	}
	if(categoryJSON.length > 1)	categoryJSON = categoryJSON.substr(0, categoryJSON.length - 1);
	categoryJSON += ']';

	var url = '/ajax/ajGW.php?action=getPlacementsForSelection&categoryJSON=' + categoryJSON + '&organisationUID=' + organisationUID;

	$.ajax({
		  url: url,
		  dataType: 'json',
		  success: function(response){
			  
			  if(response.userError) alert(response.userError);
			  else if(response.data){

				  $('#placementResults').html(response.data);
				  
			  }
			  
		  }
	});
	
}

function selectPlacementForModify( uid ){

	var placementUID = uid;
	url = '/ajax/ajGW.php?action=selectPlacementForModify&placementUID=' + placementUID;

	$('#placementResults').load(url, function(){
		
	$("select[multiple]").multiselect({
		noneSelectedText:"Select Categories",
		selectedList:4
	}).multiselectfilter(); });
	
	$('.date-pick').datepicker();
	
	$("#autocompleteModOrg").autocomplete({
		source: "/ajax/ajGW.php?action=autocomplete_getOrganisation",
		minLength: 2,
		select: function( event, ui ){
			$("#organisationModUID").val(ui.item.id);
		}
	});
	

}

function updateWeightList(){

	var catUIDs = [];
	var catNames = [];
	
	$('#categoryJSONUpdate :selected').each(function(i, selected) {
	    catUIDs[i] = $(selected).val();
	    catNames[i] = $(selected).text();
	});
	
	var newHTML = "";
	
	$.each(catUIDs, function(index, value){
	
		newHTML+= '<option value="' + value +'">' + catNames[index] + '</option>';
		
	});	
	
	$('#categoryForWeight').html(newHTML);

}

function getWeightForCategory(){
	
	var currentCategoryUID = $('#categoryForWeight').val();
	var currentPlacementUID = $('#expatPlacementUID').val();
	
	url = '/ajax/ajGW.php?action=getWeightForCategory&placementUID=' + currentPlacementUID + '&categoryUID=' + currentCategoryUID;
		
	$.ajax({
		url : url,
		dataType : 'json',
		success : function(response){
			
			  if(response.userError) alert(response.userError);
			  else if(response.data){

				  $('#weightForCategory').val(response.data);
				  if(response.data > 1){
					 $('#upButton').html("<input type=\"button\" value=\"Move Up\" onClick=\"updatePlacementPosition();\" />");
				  }
				  else{
					  $('#upButton').html("");
				  }
			  }
		}
	
	});
	
}

function updatePlacementPosition(){
	
	var currentCategoryUID = $('#categoryForWeight').val();
	var currentPlacementUID = $('#expatPlacementUID').val();
	var currentTemplateUID = $('#expatTemplateUID').val();
	var currentWeightValue = $('#weightForCategory').val();	

	url = '/ajax/ajGW.php?action=applyWeightForCategoryPlacement&placementUID=' + currentPlacementUID + '&categoryUID=' + currentCategoryUID + '&weight=' + (currentWeightValue - 1) + '&templateUID=' + currentTemplateUID;

	$.ajax({
		url : url,
		dataType : 'json',
		success : function(response){
			
			  if(response.userError) alert(response.userError);
			  else if(response.data){
				  
				  $('#weightForCategory').val(response.data);
				  alert(response.userMessage);
				  if(response.data > 1){
					  $('#upButton').html("<input type=\"button\" value=\"Move Up\" onClick=\"updatePlacementPosition();\" />");
				  }
				  else{
					  $('#upButton').html("");
				  }
				  
			  }

		}
	
	});
	
	
}

function catchAndRedirect( uid , rurl ){
	
	var url = "/ajax/ajGW.php?action=redirectAndCatch&UID=" + uid;
	var loc = 'http://' + rurl;
	$.ajax({
		url : url,
		dataType : 'json',
		success : function(response){
		
			//window.location = 'http://' + rurl;
			window.open(loc,'_blank')
			
		}
	
	});
	
}

function removeLogo(type){
	
	if(confirm('Are you sure you wish to remove the logo from the system?')){

		var uid = $('#stockistUID').val();
		var url = '/ajax/ajGW.php?action=delLogo&uid=' + uid + '&type=' + type;
		
		$.ajax({
			url : url,
			dataType : 'json',
			success : function(response){
		
				if(response.userError) alert(response.userError);
				else if(response.userMessage){
					alert(response.userMessage);
					$('#expatStockistLogo').html("");
				}
			}
		});
	}
}

function attachRegionValue( regionUID ){
	
	$('#HexpatRegionUID').val(regionUID);
	$('#regionSelectorForm').submit();
	
	
}

