var vpw_o;
var vph_o;
var vid = '';
var is_you_tube = false;
var glob_youtubeid;
var prev_initial_size;
var glob_youtube_extra_params;
var prev_ori = '';
var ori = '';
var reori_done = false;
var vp_fallback_element_id;
 
function online1_present()
{return true;}

function bindEvent(el, eventName, eventHandler) {
  if (el.addEventListener){
    el.addEventListener(eventName, eventHandler, false); 
  } else if (el.attachEvent){
    el.attachEvent('on'+eventName, eventHandler);
  }
}

function findPos(obj) {
	var curleft = curtop = 0;
  if (!obj.offsetParent) return;
  do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
    } while (obj = obj.offsetParent);
    
    return [curleft,curtop];
}


function open_map(name, latitude, longitude, address1, address2, city, state, zip, country){
  name = encodeURIComponent(name);
  address = "";
  if(address1 != null) address += address1 + ",";
  if(address2 != null) address += address2 + ",";
  if(city != null) address += city + ",";
  if(state != null) address += state + ",";
  if(zip != null) address += zip + ",";
  if(country != null) address += country + ",";
  address = encodeURIComponent(address);
  
  window.location.href = "http://maps.google.com/maps?q=%22" + name + "%22+" + address + "+@" + latitude + "," + longitude;  
}

function check_email_address(val){
    emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
    eres = (val.search(emailRegEx)!=-1)  
    return eres;
  }

function ctctf_change(id){
 el = document.getElementById(id); 
 
 if(el.type=='email' && el.value){    
    emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
    val1 = el.value; 
    eres = (val1.search(emailRegEx)!=-1)  
    if(eres) 
      document.getElementById('signup').disabled = false;
    else
      document.getElementById('signup').disabled = true;
 }
 
 
 
}

function ctctf_clear_initial(id, initial_value){
 el = document.getElementById(id);
 if(el.value==initial_value) el.value='';
}

function online_flip_photo(from_div_id, div_id) {
  document.getElementById(div_id).style.display = 'block';
  document.getElementById(from_div_id).style.display = 'none';

}


function addToFavorites() {
  urlAddress = document.location.href;
  pageName = window.title;
  if (window.external) {
      window.external.AddFavorite(urlAddress,pageName);
  } else {
      alert("Sorry! Your browser doesn't support this function.");
  }
}

function shrink_all_except(s){

  for (i=0; i<30; i++){
    if(i+1 != s){ 
      e = document.getElementById('exp'+(i+1));
      if(e != null) 
        e.style.height='1px';            
    }
  }

}

function expand_section(s){

  shrink_all_except(s);
  e = document.getElementById('exp'+s);
  if(e==null) return;
  
  if(e.style.height==null || e.style.height=='' || e.style.height=='1px'){    
    e.style.height='auto';
    window.scrollTo(0,findPos(e)[1]-64);
  }
  else {
    e.style.height='1px';
  }
    
  
}

function expand_block(s, doscroll){
  if (doscroll==null) doscroll = false; 
  e = document.getElementById('block'+s);
  if(e==null) return;
  
  if(e.style.height==null || e.style.height=='' || e.style.height=='1px'){    
    e.style.height='auto';
    if(doscroll) window.scrollTo(0,findPos(e)[1]-64);
  }
  else {
    e.style.height='1px';
  }
}

function do_orientation(){
  vpw = document.viewport.getWidth();
  vph = document.viewport.getHeight();
  
  if(vpw==null || vph==null){
    vpw=parseInt(document.body.clientWidth);
    if(vpw < 380)
      vph=vpw /  0.5625;
    else
      vph=vpw * 0.5625;
  }  
  
  
  br = $('blockrandom');
  new_height = Math.round(vpw * 0.5625);

    
  if(vpw > vph){        
    br.scrollTo();    
  }

  if(vph > vpw)
    $('rotate_message').style.display='block';
  else
    $('rotate_message').style.display='none';
    
  initial_size = "width:" + Math.round(vpw) + "px; height:" + Math.round(new_height ) + "px;" ;
  if(prev_initial_size!=initial_size){
    if(is_you_tube) //&& navigator.platform.indexOf('Android') != -1
    {  
      prev_initial_size = initial_size;
      vid = set_you_tube_html(glob_youtubeid, initial_size, glob_youtube_extra_params);
      tmp_el = '<div id="tempel1">Resizing video. One moment...</div>'
      br.replace(tmp_el);
      $('tempel1').replace(vid);
    }else{
      br.style.width = Math.round(vpw)+'px';  
      br.style.height = new_height + 'px';
    }
  }
}

function set_you_tube_html(youtubeid, initial_size, extra_params){
  
  res = '<iframe id="blockrandom" name="videoblock" src="http://www.youtube.com/embed/' + youtubeid + '?enablejsapi=1&origin=' + window.location.hostname  + '&' +  extra_params + '" style="' + initial_size + ' max-width:100%; max-height:500px; overflow:hidden;" scrolling="no" align="top" frameborder="0" class="wrapper"><a style="color: white;" href="http://www.youtube.com/watch?v=' + youtubeid + '&' +  extra_params + '" target="youtube">View video on YouTube</a></iframe>';
  return res; 
}

function setup_resizable_video(divid, youtubeid, videourl, extra_params){
  

  vpw_o = document.viewport.getWidth();
  vph_o = document.viewport.getHeight();
  
  if(extra_params==null)
    glob_youtube_extra_params = '';
  else
    glob_youtube_extra_params = extra_params;
  
  
  
  if(vpw_o==null || vph_o==null){
    vpw_o=parseInt(document.body.clientWidth);
    if(vpw_o < 380)
      vph_o=vpw_o /  0.5625;
    else
      vph_o=vpw_o * 0.5625;
  }  
    
  vph_o1 = vpw_o * 0.5625;
  
  initial_size = "width:" + Math.round(vpw_o) + "px; height:" + Math.round(vph_o1 ) + "px;" ;
  if(youtubeid.length>1){
    vid += set_you_tube_html(youtubeid, initial_size, glob_youtube_extra_params);
    glob_youtubeid = youtubeid;
    is_you_tube = true;
  }
  else
    vid += '<a style="color:white" href="' + videourl + '" _mce_href="' + videourl + '">Click to open video</a>';

  if(videourl.length>1)
    vid = '<video onclick="video_click(this)" id="blockrandom" controls="controls" name="videoblock" src="' + videourl + '" scrolling="no" align="top" frameborder="0" class="videoblock">' + vid + '</video>';

  $(divid).innerHTML = vid ; //+ "<p style='color:gray'>" + vpw_o + "," + vph_o + "</p>";
  set_video_message("rotate_message");
  
  var supportsOrientationChange = "onorientationchange" in window;
  //    orientationEvent = supportsOrientationChange ? "orientationchange" : "resize";

  orientationEvent = "resize" ;
   window.addEventListener(orientationEvent, function() {
    do_orientation();  
  }, false);


   if(supportsOrientationChange){
    orientationEvent = "orientationchange" ;
    window.addEventListener(orientationEvent, function() {
      do_orientation();  
    }, false);
  }
    
  
}
 

function video_click(v){

    if (v.paused) {
      v.play();
    } else {
      v.pause();
    }
}

function set_video_message(messageid){
if(vph_o > vpw_o)    
  $(messageid).style.display='block';
else
  $(messageid).style.display='none';
}


function insert_full_width_picture(workcase_id, force_width, format, quality, one_time_params, element_id, fileentry, subfolder){
  
  if (fileentry==null)
    fileentry = 'Photo.jpg%20%28current%29';
  else
    fileentry = encodeURIComponent(fileentry);
    
  if (subfolder==null)
    subfolder = '%2FWorkcase%2FConverter+Shrink';
  else
    subfolder = encodeURIComponent(subfolder);
  
  if(one_time_params==null)
   one_time_params = '';
  
  vp_fallback_element_id = element_id;
  if(element_id=='here')
    vp_fallback_element_id = 'content';
  
  ws = force_width;
  vpw = get_viewport_width();
  w = 0;
  if(ws.indexOf('px')>0)
    w = parseInt(force_width);
  if(ws.indexOf('%')>0)
    w = vpw * (0.01 * parseInt(force_width));
  if(ws.indexOf('em')>0)
    w = parseInt(force_width)*16;

  if(w==0 || w > vpw)
    w = vpw;
  h = get_viewport_height();
  
  inh = '<img style="max-width: 100%; width:' + force_width + 
  '; height: auto;" src="/document_conversion/convert?fileentry=' + fileentry + '&subfolder=' + subfolder + '&workcase_id=' + workcase_id +
  '&outformat=mobile-fill-vp_l_' + w + '_' + h + '_' + format + '_' + quality + one_time_params + '" border="0" />';

  if (element_id==null)
    element_id = 'picture-only-block-' + workcase_id;
  else if(element_id=='here'){
    document.write(inh);
    return;
  }
  else
    element_id = element_id;

  document.getElementById(element_id).innerHTML=(inh);
}

function handle_page_embed(){
  c = document.getElementById('content');   
  c.style.minWidth = '1px';  
  c.style.maxWidth = '300px';
}

function go_find(){

  navigator.geolocation.getCurrentPosition(  
		function (position) {  
                   set_loc(position);
		}, 
		function (error)
		{
			switch(error.code) 
			{
				case error.TIMEOUT:
					alert ('Timeout getting your position. Please try again.');
					break;
				case error.POSITION_UNAVAILABLE:
					alert ('Location services appear to be unavailable at this time.');
					break;
				case error.PERMISSION_DENIED:
					alert ('Please enable location services to use this function. On iPhone, go to Settings | General | Location Services and ensure Safari is On.');
					break;
				case error.UNKNOWN_ERROR:
					alert ('Something unexpected went wrong with your location services.');
					break;
			}
		},
                { enableHighAccuracy:true,
		  maximumAge:0, 
		  timeout:30000 }
	);
}

function get_viewport_width(){
  vpw = 0;
  if(document.viewport)
    vpw = document.viewport.getWidth();
  if(typeof vpw == 'undefined' || parseInt(vpw)==0)    
   vpw = (document.getElementById(vp_fallback_element_id).getWidth());
  
  vpw = parseInt(vpw);
  return vpw;
}

function get_viewport_height(){
  
  h = 0;
  if(document.viewport)
    h = document.viewport.getHeight();
  if(typeof h == 'undefined' || parseInt(h)==0)
   h = (document.getElementById(vp_fallback_element_id).getHeight());
   
  h = parseInt(h);
  return h;
}

function handle_img_ori_change(){
  if(reori_done) return;
  vpw = get_viewport_width();
  h = get_viewport_height();

  ori_change = false;
  ori = 'l';
  if (vpw < h)
    ori = 'p';
      
  if(prev_ori != ori)
    ori_change = true;
  prev_ori = ori;
  
  
  if(ori_change){
    reori_done = true;
    window.location.href=window.location.href;
  }

}

function insert_cdn_image(cdnbase, imgpre, imgext, element_id, replace_el, imgclass, imgid, no_resize){

  if(no_resize==null)
    no_resize = false;

  vp_fallback_element_id = element_id;

  vpw = get_viewport_width(); 
  h = get_viewport_height();
   
   
  ori = 'l';
  vpw1 = vpw;
  if (vpw < h){
    ori = 'p';
    vpw1 = vpw * 1.75;
  }


 
  if(imgid == null || imgid == '') imgid="cdn_img";

  if(vpw1<=320) dim_x = 320 ;
  if (vpw1>320 && vpw1<=480) dim_x = 480;
  if (vpw1>480 && vpw1<=640) dim_x = 640;
  if (vpw1>640 && vpw1<=920) dim_x = 920;
  if (vpw1>920 && vpw1<=1220) dim_x = 1220;
  if (vpw1>1220) dim_x = 1800;
    
  new_src = cdnbase + imgpre + dim_x + '-' + ori + '.' + imgext;
  inh = '<img id="' + imgid + '" class="cdn_img ' + imgclass + '" src="' + new_src + '" border="0" />';

  var supportsOrientationChange = "onorientationchange" in window;
    
  if(!no_resize){   
  if(supportsOrientationChange){
    orientationEvent = "orientationchange" ;
    bindEvent(window, orientationEvent, handle_img_ori_change);
  }else{
    
      orientationEvent = "resize" ;
      bindEvent(window, orientationEvent, handle_img_ori_change);
    
  }
  }
  
  
  prev_ori = ori;  

  if (element_id==null)
    element_id = 'picture-only-block-' + workcase_id;
  else if(element_id=='here'){
    document.write(inh);
    return;
  }
  else
    element_id = element_id;

   e1 = document.getElementById(element_id);

   if(replace_el=='after')
    e1.innerHTML+=(inh); 
   else if(replace_el=='replace')
    e1.innerHTML=(inh); 
   else if(replace_el=='refresh'){
     if(e1.src != new_src)
       e1.src = new_src;
   }
   else
    e1.innerHTML=(inh);

}

