// Anything in here will be triggered when the page is ready (so all DIV's are present etc.)

$(document).ready(function(){

	populateElement('#jrSearch', 'e.g. "Banking Operations" Permanent');
	
	// GET SALARY LIST FROM A JOB TYPE SELECTION
    $('select#jobtypejs').change (function(){
        $('#salaryjs').html('<option value=""0"">Loading...</option>');
        $.get('/WebServices/salaryHTML.aspx',{type:this.options[this.selectedIndex].value},function(data) { 
                $('#salaryjs').html(data);
                document.getElementById('salaryjs').value=0;    
            }
        ); 
    });
    
// GET JOB TYPE FROM A SALARY SELECTION
    $('select#salaryjs').change (function(){
            $('#jobtypejs').html('<option value=""0"">Loading...</option>');
            $.get('/WebServices/salaryHTML.aspx',{s:this.options[this.selectedIndex].value},function(data) { 
                    $('#jobtypejs').html(data);
            var jobtypejs = document.getElementById('jobtypejs');
            var i = 0;
                for (i;i<jobtypejs.options.length;i++) {
                    if (jobtypejs.options[i].getAttribute("selected")){
                        jobtypejs.value = jobtypejs.options[i].value;                
                    }
                }
            });
    });
    

// GET SUB SECTORS FROM SECTOR ID
    $('select#sectorjs').change(function(){
        $('select#subsectorjs').html('<option>Loading...</option>');
        $.get('/WebServices/subSectorHTML.aspx',{sector:this.options[this.selectedIndex].value},function(data) { 
                $('select#subsectorjs').html(data);
                $('select#subsector2js').selectedIndex = 0;
            }
        ); 
    });

	$("div.defaultClose").hide(); // close all divs with this class

// MAP OPEN AND CLOSING
    $('span.toggleMap').click (function() {
	    if($('span.toggleMap').html() == "Show"){
	        $("div#mapppFrame").slideDown("slow");
	        $('span.toggleMap').html("Hide");
	    }else{
	        $("div#mapppFrame").slideUp("slow");
	        $('span.toggleMap').html("Show");
	    }
		
		
	});
// SEARCH BAR OPENING AND CLOSING
	if (document.getElementById("searchOpen")) {

		$("div#searchOpen").before('<div id="searchClose"><div id="searchMin" class="searchMin"><p><span class="show">Click here to search for Jobs</span></p></div><div class="info showInfo"><p><span class="show">Show</span></p></div></div>');
		$(".searchContainer").before('<p><span class="hide">Hide</span></p>');
	
		if ($("div#searchOpen").attr('class') == "initClose") {
			$("div#searchOpen").hide();
			$("div#searchClose").show();
		}
	}
	

	$('span.hide').click (function() {
		$("div#searchOpen").slideUp("slow");
		$("div#searchClose").slideDown("slow");
	});

	$('span.show').click (function() {
		$("div#searchClose").slideUp("slow");
		$("div#searchOpen").slideDown("slow");
	});		


// SEARCH HISTORY OPENING AND CLOSING 

	if (document.getElementById("searchHistory")) {

		var $historyHeading = $('#searchHistory');
		var $historyContent = $("#searchHistoryContent");
		var historyDefaultClass = 'yourSearchHistory';
		var $isSearchHistoryClosed = $historyContent.attr('class').search("defaultClose");
	
		if ($isSearchHistoryClosed != -1) { $('span.yourSearchHistory').attr('class','viewSearchHistory').html("Search History"); }
			
		$('span', $historyHeading).click (function() {
			var $whichHistory = $(this).attr('class');
			if ( $whichHistory == historyDefaultClass ) {
				$historyContent.slideUp("slow");
				$(this).attr('class','viewSearchHistory').html("Search History");
			} else {
				$historyContent.slideDown("slow");
				$(this).attr('class',historyDefaultClass).html("Search History");
			};
		});

	}

	// ALERTS
	//if (document.getElementById("alerts")) {
	//	$("div.alert p").append("<span>Dismiss</span>");
	//	$('span', 'div.alert p').click (function() {
	//		$(this).parent().slideUp("fast");
	//	});
	//}
	

	// INTERVIEW OPENING AND CLOSING 
	
	if (document.getElementById("interviewHeader")) {

		var $interviewHeading = $('#interviewHeader');
		var $interviewContent = $('#interviewContent');
		var interviewDefaultClass = 'myInterviews';
		var $isInterviewClosed = $interviewContent.attr('class').search("defaultClose");
		
		if($isInterviewClosed != -1) { $('span.myInterviews').attr('class','viewMyInterviews').html("Upcoming Appointments"); }
		
		$('span', $interviewHeading).click (function() {
			var $whichInterview = $(this).attr('class');
			if ( $whichInterview == interviewDefaultClass ) {
				$interviewContent.slideUp("slow");
				$(this).attr('class','viewMyInterviews').html("Upcoming Appointments");
			} else {
				$interviewContent.slideDown("slow");
				$(this).attr('class',interviewDefaultClass).html("Upcoming Appointments");
			};
		});
	
	}
	
	
	var plus = '/img/iconOpen.gif';
	var minus = '/img/iconClose.gif';
	var $heading = $('#accordian h2');
		
	$("div#collapsables div.box").hide(); // close all boxes
	//$heading.append('<p><span class="showMore"><img src="' + plus + '" alt="show more" /> Show More</span></p>');
		
	$('span', $heading).click (function() {
		var $toggleSrc = $(this).attr('class');
		if ( $toggleSrc == 'hideMore') {
			$(this).parent().next().slideUp("slow");
			$(this).attr('class','showMore').html('<img src="' + plus + '" alt="show more" /> Show More').parents('div.box');
		} else {
			$(this).parent().next().slideDown("slow");
			$(this).attr('class','hideMore').html('<img src="' + minus + '" alt="hide" /> Hide').parents('div.box');
		};
	});

	
	$("div.jobItem").hover(
		function () {
			$(this).addClass("jobItemHover");
			$(this).removeClass("jobItemHoverOut");
		}, 
		function () {
			$(this).addClass("jobItemHoverOut");
			$(this).removeClass("jobItemHover");
		}
	);
	
	$("div.jobItemBasket").hover(
		function () {
			$(this).addClass("jobItemHoverBasket");
			$(this).removeClass("jobItemHoverOut");
		}, 
		function () {
			$(this).addClass("jobItemHoverOut");
			$(this).removeClass("jobItemHoverBasket");
		}
	);

	$("div.browseSector div.sector").hover(
		function () {
			$(this).addClass("sectorHover");
			$(this).removeClass("sectorHoverOut");
		}, 
		function () {
			$(this).addClass("sectorHoverOut");
			$(this).removeClass("sectorHover");
		}
	);


	$("div.eventItem").hover(
		function () {
			$(this).addClass("eventItemHover");
			$(this).removeClass("eventItemHoverOut");
		}, 
		function () {
			$(this).addClass("eventItemHoverOut");
			$(this).removeClass("eventItemHover");
		}
	);
	
	$("div.contClosed").hover(
		function () {
			$(this).addClass("contClosedHover");
			$(this).removeClass("contClosedOut");
		}, 
		function () {
			$(this).addClass("contClosedOut");
			$(this).removeClass("contClosedHover");
		}
	);

	$("div.jobItem").click (function() {
		var theLink = $('h2 a', this).attr('href');
		if (theLink != undefined){	
    		window.location = theLink;
	    }
	});

	$("div.jobItem").click (function() {
		var theLink = $('h3 a', this).attr('href');
		if (theLink != undefined){	
    		window.location = theLink;
	    }
	});
	
	$("div.jobItem").click (function() {
		var theLink = $('h4 a', this).attr('href');
		if (theLink != undefined){	
		    window.location = theLink;
	    }
	});
	
	$("div.jobItem div.jobItemTopBasket").click (function() {
		var theLink = $('h2 a', this).attr('href');
		if (theLink != undefined){	
		    window.location = theLink;
	    }
	});

	$("div#jobListFoot div.jobItem").click (function() {
		var theLink = $('h3 a', this).attr('href');
        if (theLink != undefined){	
        	window.location = theLink;
	    }
	});

	$("div.browseSector div.sector").click (function() {
		var theLink = $('h2 a', this).attr('href');
		if (theLink != undefined){	
    		window.location = theLink;
	    }
	});

	$("div.eventItem").click (function() {
		var theLink = $('h3 a', this).attr('href');
        if (theLink != undefined){	
       		window.location = theLink;
	    }
	});
	
	$("div.sideAd").click (function() {
		var theLink = $('h2 a', this).attr('href');
        if (theLink != undefined){	
       		window.location = theLink;
	    }
	});


	// POPUP BOX
	// use this bit of code to attach the onclick to a button or link
	/* $('#apply').click(function (e) {
		e.preventDefault();
		$('.popupBox').modal();
	}); */

	if (document.getElementById("popups")) {
		$('.popupBox').modal();
	}

	/* add another email in the registration form */
	if (document.getElementById("secondaryEmail")) {
		
		$(".addEmail").click(function (e) {
			e.preventDefault();
			
			var $add = $(this).attr('class').search("removeIcon");
			
			if($add != -1) {
				$(this).removeClass("removeIcon");
				$(this).html("Add another Email Address");
				$("#secondaryEmail").hide();
			} else {
				$(this).addClass("removeIcon");
				$(this).html("Remove Secondary Email Address");
				$("#secondaryEmail").show();
			}
			
    });
	}

	/* add another sector in the registration form */
	if (document.getElementById("secondarySector")) {
		
		$(".addSector").click(function (e) {
			e.preventDefault();
			
			var $addIcon = $(this).attr('class').search("removeIcon");
			
			if($addIcon != -1) {
				$(this).removeClass("removeIcon");
				$(this).html("Add another Sector");
				$("#secondarySector").hide();
			} else {
				$(this).addClass("removeIcon");
				$(this).html("Remove Sector");
				$("#secondarySector").show();
				$("#secondarySubSector").show();
			}
			
    });
	}

	//calInit();

	// SALARY SURVEY SWAP TABLES

	if (document.getElementById("permanentTable")) {
		
		$('#numbersPermanent').click (function() {
			//$("#permanentTable").show();
			$("#permanentTable").removeClass("hideTable");
			$("#permanentTable").addClass("displayTable");
			//$("#temporaryTable").hide();				
			$("#temporaryTable").removeClass("displayTable");
			$("#temporaryTable").addClass("hideTable");
		});

		$('#numbersTemporary').click (function() {
			//$("#permanentTable").hide();
			$("#permanentTable").removeClass("displayTable");
			$("#permanentTable").addClass("hideTable");
			//$("#temporaryTable").show();
			$("#temporaryTable").removeClass("hideTable");
			$("#temporaryTable").addClass("displayTable");
		});
	}

	// hover on interviews list
	$(".yourInterviews .jobItem").hover(
      function () {
        $(this).find(".popUpImg").css({visibility:"visible"});
			}, 
      function () {
        $(this).find(".popUpImg").css({visibility:"hidden"});
      }
    );

    $('select#flocation').change (function(){
        $('form#frmFilter').submit();
    });

    $('select#findustry').change (function(){
        $('form#frmFilter').submit();
    });

});


function accordionHover(divID) {
	if (document.getElementById) {
		theDiv = document.getElementById(divID);
		theDiv.style.backgroundColor = '#dbd5bb';
	}	
}

function accordionOff(divID) {
	if (document.getElementById) {
		theDiv = document.getElementById(divID);
		theDiv.style.backgroundColor = '#f4f0df';
	}	
}

function formFocus(field,theFocus) {
	//field.focus();
	//field.select();
	if(theFocus == 'hasFocus') {
		field.style.backgroundColor = '#fcdd5b';
		field.style.color = '#312c2f';
		field.style.textDecoration = 'none'; 
	} else {
		field.style.backgroundColor = '#ffffff';
		field.style.color = '#d75303';
		field.style.textDecoration = 'underline'; 
	}
}


function populateElement(selector, defvalue) {
    if($.trim($(selector).val()) == "") {
        $(selector).val(defvalue);
    }
  
    $(selector).focus(function() {
        if($(selector).val() == defvalue) {
            $(selector).val("");
        }
    });
    
    $(selector).blur(function() {
        if($.trim($(selector).val()) == "") {
            $(selector).val(defvalue);
        }
    });
 }
 
function findObj(n, d) {
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function swapImage() {
  var i,j=0,x,a=swapImage.arguments; document.sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=findObj(a[i]))!=null){document.sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

// IE background flicker fix
try {
  document.execCommand("BackgroundImageCache", true, true);
} catch(e) {}

// Campaign local alert
function acknowledgeCampaign(accountCampaignID, divName) {
  $.get('/WebServices/acknowledgeCampaign.aspx',{accountcampaignid:accountCampaignID},function(data) { 
    if (data == '0') {
      document.getElementById(divName).style.display = "none";
    }
  }
  );
}
