// JavaScript Document


function insertVideo(divId, width, height, xml, addControlsHeight) {
	addControlsHeight = (addControlsHeight == null) ? true : addControlsHeight;
	height = (addControlsHeight) ? height + 30 : height;
	var flashVars = {"dataURL":xml}
	var flashParams = {"wmode":"transparent"};
	var flashAttributes = {"name": divId, "id": divId};
	swfobject.embedSWF("/flash/video/ZapointVideoPlayer.swf", divId, width, height, "10.0.0", "/flash/expressInstall.swf", flashVars, flashParams, flashAttributes);
}


function hideElements(class_name) {
	elements = $$(class_name);
	elements.each(function(el){
		el.hide();
	});
}

function showElements(class_name) {
	elements = $$(class_name);
	elements.each(function(el){
		el.show();
	});
}


// preload mouseover images at runtime
function preloadImg() {
  if (document.images) { 
    if(!document.p) document.p=new Array();
    var i,j=document.p.length; 
    for(i=0; i<preloadImg.arguments.length; i++) {
      if (preloadImg.arguments[i].indexOf("#")!=0) { 
        document.p[j]=new Image; 
        document.p[j++].src=preloadImg.arguments[i];
      }
    }
  }
}

//open new browser window
function openwindow(url,name,prop) {
  window.open(url,name,prop);
}

function Popup(id) {
  var dur = 0.5;
  var containerId = 'opacity_background';
  var pageSize = getPageSize();
  var pageScroll = getPageScroll();

  Popup.prototype.showPopup = function() {
	  pageWidth = pageSize[0];
	  pageHeight = pageSize[1];
	  windowHeight = pageSize[3];

	  id.style.display = "block";
	  divHeight = id.clientHeight;

	  topMargin = (windowHeight - divHeight)/2;
	  if (topMargin <= 0) {topMargin = 0;}
	  else {topMargin -= 10;}

	  positionY = pageScroll[1] + topMargin;
	  id.style.top = positionY + 'px';
	  id.style.display = "none";
	  id.style.zIndex = 2;

	  $('opacity_background').style.display = "block";
	  $('opacity_background').style.height = pageHeight + 'px';

	  new Effect.Parallel(
	    [ new Effect.Opacity(containerId, {from: 0.0, to: 0.85, sync: true}),
	      Effect.Appear(id, {sync: true})],
	    { duration: dur}
	  );
  }

  Popup.prototype.closePopup = function() {
    new Effect.Parallel(
      [ Effect.Fade(id, {sync: true}),
        new Effect.Opacity(containerId, {from: 0.85, to: 0.0, sync: true})],
      { duration: dur,
        afterFinish: function() {
          id.style.display = "none";
          id.style.zIndex = -1;
          $('opacity_background').style.display = "none";
        }
      }
    );
  }
}

function Link() {
  Link.prototype.handOn = function() {
    document.body.style.cursor='pointer';
  }

  Link.prototype.handOff = function() {
    document.body.style.cursor='default';
  }
}

function endDateToggle() {
  var checkbox = $('ResumeStill');

  if (checkbox) {
    checkbox.onclick = function() {
      if(checkbox.checked) {
        $('ResumeEnddateMonth').disabled = true;
        $('ResumeEnddateYear').disabled = true;
      }
      else {
        $('ResumeEnddateMonth').disabled = false;
        $('ResumeEnddateYear').disabled = false;
      }
    }
  }
}

// -----------------------------------------------------------------------------------

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
// Code taken from http://www.dynamicdrive.com/dynamicindex4/lightbox2/js/lightbox.js
//
function getPageScroll(){
  if (self.pageYOffset) {
    yScroll = self.pageYOffset;
  } else if (document.documentElement && document.documentElement.scrollTop){    // Explorer 6 Strict
    yScroll = document.documentElement.scrollTop;
  } else if (document.body) {// all other Explorers
    yScroll = document.body.scrollTop;
  } 

  arrayPageScroll = new Array(0,yScroll) 
  return arrayPageScroll;
}

// -----------------------------------------------------------------------------------

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Code taken from http://www.dynamicdrive.com/dynamicindex4/lightbox2/js/lightbox.js
// Edit for Firefox by pHaez
//
function getPageSize(){
  
  var xScroll, yScroll;
  
  if (window.innerHeight && window.scrollMaxY) {        
    xScroll = document.body.scrollWidth;
    yScroll = window.innerHeight + window.scrollMaxY;
  } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
    xScroll = document.body.scrollWidth;
    yScroll = document.body.scrollHeight;
  } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
    xScroll = document.body.offsetWidth;
    yScroll = document.body.offsetHeight;
  }
  
  var windowWidth, windowHeight;
  if (self.innerHeight) {       // all except Explorer
    windowWidth = self.innerWidth;
    windowHeight = self.innerHeight;
  } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
    windowWidth = document.documentElement.clientWidth;
    windowHeight = document.documentElement.clientHeight;
  } else if (document.body) { // other Explorers
    windowWidth = document.body.clientWidth;
    windowHeight = document.body.clientHeight;
  }     
  
  // for small pages with total height less then height of the viewport
  if(yScroll < windowHeight){
    pageHeight = windowHeight;
  } else { 
    pageHeight = yScroll;
  }

  // for small pages with total width less then width of the viewport
  if(xScroll < windowWidth){    
    pageWidth = windowWidth;
  } else {
    pageWidth = xScroll;
  }

  arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
  return arrayPageSize;
}


//show/hide help DIVs for forms
function show_help(help_field) {
  $('help_' + help_field).show();
}

function hide_help(help_field) {
  $('help_' + help_field).hide();
}

//show/hide selected border for form inputs
function show_border(help_field, form_field) {
  $(form_field + "_" + help_field).style.borderColor = '#82b0cc';
}

function hide_border(help_field, form_field) {
  $(form_field + "_" + help_field).style.borderColor = '#b6c8db';
}



// Any tagcloud - activate/inactivate tags based on what the user typed in the "field_name" field
function tag_toggle(span_name, field_name, list) {
  
  return;

  //disabled

  string = $(field_name).value.toLowerCase();
  text_values = string.split(/[,]/).without('');
  typed_words = []
  text_values.each(function(word) { typed_words.push(word.strip()) });
  list.each(function(member) {
    if(typed_words.indexOf(member[1].toLowerCase()) != -1) {
      $(span_name+"_" + member[0]).hide();
      $(span_name+"_" + member[0]+"_nolink").show();
    } else {
      $(span_name+"_" + member[0]).show();
      $(span_name+"_" + member[0]+"_nolink").hide();
    }
  });
}

// Year Assessment helper
function select_year_option(key, value, index, max) {
  $('assessment_' + key).value = value;

  for (var i=1; i<=max; i++) {
    //console.log("Element "+'assessment_link_' + key + '_' + i);
    if (i==index) {
      $('assessment_link_' + key + '_' + i).addClassName('selected_highlight');
    } else {
      $('assessment_link_' + key + '_' + i).removeClassName('selected_highlight');
    }
  }
}


// Open a tag cloud, close all the others
function toggle_tagclouds(id) {
  clouds = $$('.toggleCloud', '.wideTagCloud');
  clouds.each(function(cloud) {
      if (cloud.id == id) {
        if (!$(cloud).visible()) {
	  $(cloud).show();
	}
      } else {
	if ($(cloud).visible()) {
	  $(cloud).hide();
	}
      }
    });
}


// Zapoint Multiple Selector helper
function multiple_selector_toggler(obj_name, obj_field) {
  link_el  = $(obj_name + '_' + obj_field + '_link');
  input_el = $(obj_name + '_' + obj_field); 
  if(link_el.className.split(' ').include('selected_highlight')) {
    link_el.removeClassName('selected_highlight');
    input_el.value = 0;
  } else {
    link_el.addClassName('selected_highlight');
    input_el.value = 1;
  }
  return false;
}

// Zapoint Multiple Selector helper - for the multiline version
function multiple_selector_toggler_multiline(obj_name, obj_field) {
  link_el  = $(obj_name + '_' + obj_field + '_link');
  div_el   = $('multiline_selector_' + obj_name + '_' + obj_field);
  input_el = $(obj_name + '_' + obj_field); 
  if(link_el.className.split(' ').include('selected_highlight')) {
    link_el.removeClassName('selected_highlight');
    div_el.className = 'multiline_row';
    input_el.value = 0;
  } else {
    link_el.addClassName('selected_highlight');
    div_el.className = 'multiline_row_selected';
    input_el.value = 1;
  }
  return false;
}

// Switch sub-forms when choosing an assessment date type for enterprises
function assessment_date_subform_switch(index, type) {
	subforms = $$('.dummy_ad_subform_'+index);
	subforms.each(function(sf) {
	    if (sf.id == 'ad_type'+type+'_'+index) {
	      $(sf).show(); 
	    } else {
  		  $(sf).hide();
        }
    });
}
