// types/topmost/js/topmost.js
// Preload Tab Images
var most_read =    new Image(); most_read.src = "http://www.brantford.com/images/v3/most_read_on.jpg";
var most_emailed = new Image(); most_emailed.src = "http://www.brantford.com/images/v3/most_emailed_on.jpg";
var most_commented =  new Image(); most_commented.src =  "http://www.brantford.com/images/v3/most_commented_on.jpg";

var divids=new Array('mostread','mostemailed','topstories');
var tabids=new Array('most_read','most_emailed','most_commented');


function switchid(id,tab){  
        hideallids();
        showdiv(id,tab);
}

function hideallids(){
        //loop through the array and hide each element by id
        for (var i=0;i<divids.length;i++){
                hidediv(divids[i],tabids[i]);
        }                 
}

function hidediv(id,taboff) {
        //safe function to hide an element with a specified id
        if (document.getElementById) { // DOM3 = IE5, NS6
                document.getElementById(id).style.display = 'none';
        }
        else {
                if (document.layers) { // Netscape 4
                        document.id.display = 'none';
                }
                else { // IE 4
                        document.all.id.style.display = 'none';
                }
        }
        // switch tab image to off state
        document.getElementById(taboff).src = "http://www.brantford.com/images/v3/"+taboff+"_off.jpg";
}

function showdiv(id,tab) {
        //safe function to show an element with a specified id
                  
        if (document.getElementById) { // DOM3 = IE5, NS6
                document.getElementById(id).style.display = 'block';
        }
        else {
                if (document.layers) { // Netscape 4
                        document.id.display = 'block';
                }
                else { // IE 4
                        document.all.id.style.display = 'block';
                }
        }
        // switch tab image to current selection
        document.getElementById(tab).src = "http://www.brantford.com/images/v3/"+tab+"_on.jpg";
}
// end types/topmost/js/topmost.js

// js/home/interstitial.js
var sfgate_interstitial_showad = '/cgi-bin/interstitial/main/showad?target_url=';
var sfgate_interstitial_cookie_name = 'SFGateInterstitial4';
var sfgate_interstitial_cookie_expire = 1;
function sfgate_setaonclick() {
}
// end js/home/interstitial.js

// js/home/footer_ads.js
function navdots_on() {
    var sfg_nav002el = document.getElementById('sfg_nav002');
    if ( sfg_nav002el !== undefined && sfg_nav002el != null) {
        sfg_nav002el.style.display= 'block';
    }
}
// end js/home/footer_ads.js

// js/utils/fstore.js
// utility functions to store values in hidden inputs
// get integer value stored in form input el. Returns 0 if el is null
function sfggifel(el) {
    if ( el ) {
        return parseInt(el.value);
    }
    return 0;
}
// set integer in input id;
function sfgsitel(el, num) {
    sfgsstel(el,num); 
}
// get boolean value.
function sfggbfel(el) {
    if ( el ) {
        if ( el.value == "True" ) {
            return true;
        }
    }
    return false;
}
// set boolean value
function sfgsbtel(el, b) {
    sfgsstel(el, (b ? "True" : "False" ));
}
// get string value
function sfggsfel(el) {
    if ( el ) {
        return el.value;
    }
    return '';
}
// set string value
function sfgsstel(el, s) {
    if ( el ) {
        el.value = s;
    }
}
// end js/utils/fstore.js

//function dip_popup(dtldate) {
function dip_popup() {
    // winurl = '/cgi-bin/object/dayinpictures?f=/g/archive/' + dtldate + '/dip.DTL';
    winname = 'DayInPictures';
    //            width=748,height=597       actual content and margins area
    winsizesaf = 'width=748,height=599'; //  safari
    winsizemac = 'width=748,height=600'; //  non-safari mac browsers
    winsizefox = 'width=748,height=598'; //  non-mac firefox browser
    winsizeoth = 'width=748,height=602'; //  all other browsers
    checkbrowser();
    makewindow();
}

function checkbrowser() {
    winmodes = ',screenx=15,screeny=15,resizable=no,toolbar=no,directories=no,location=no,scrollbars=no';
    // var browserOS = navigator.appVersion;

    if ( navigator.appVersion.indexOf("Safari") != -1 ) {
        attribs = winsizesaf + winmodes;
        return;
    }
    if ( navigator.appVersion.indexOf("Mac")    != -1 ) {
        attribs = winsizemac + winmodes;
        return;
    }
    if ( navigator.userAgent.indexOf("Firefox") != -1 ) {
		attribs = winsizefox + winmodes;
    } else {
        attribs = winsizeoth + winmodes;
    }
}

function makewindow() {
    popupWindow=window.open('',winname,attribs);
    popupWindow.opener.name='SFGateHomePage';
    popupWindow.focus();
}

// chr005.js
function sfg_day_toprevchron(elid) {
    this.elid = elid;
    this.tform = document.getElementById(elid);
    if ( this.tform ) {
        this.y = this.tform.y;
        this.m = this.tform.m;
        this.d = this.tform.d;
    }
    window[this.elid+'_obj'] = this;
}
sfg_day_toprevchron.prototype.onsubmit = function() {
    if ( this.tform ) {
        var re = /\d/;
        if ( ! re.test(this.y.value) ||
             ! re.test(this.m.value) ||
             ! re.test(this.d.value) ) {
            return false;
        } else {
            return true;
        }
    }
    return false;
}
// end chr005.js

// js/home/hed011markets.js
function indexWatchlistLoader (HTML,JS) {
 var el = document.getElementById('indexWatchlist');
 if ( el ) {
     el.innerHTML = HTML;
     eval(JS);
 }
}
var headTag = document.getElementsByTagName("head").item(0);
var scriptTag = document.createElement("script");  
scriptTag.src = 'http://finance.sfgate.com/hearst?Account=sfgate&Module=watchlist&Output=jscallback&Callback=indexWatchlistLoader';
// function call after page load:
// headTag.appendChild( scriptTag );
// end js/home/hed011markets.js

function sfg_ToggleConsole () {
  var timg = document.getElementById('sfg_toggle_img');
  var src = timg.src;
  if (src.indexOf('/graphics/home/customize/closed.gif') > 0) {
    timg.src = 'http://imgs.sfgate.com/graphics/home/customize/open.gif';
    Effect.BlindDown(document.getElementById('your_content'),{duration:0.5,fps:50});
    Effect.BlindDown(document.getElementById('console_form'),{duration:0.5,fps:50});
  } else {
    timg.src = 'http://imgs.sfgate.com/graphics/home/customize/closed.gif';
    Effect.BlindUp(document.getElementById('console_form'),{duration:0.5,fps:50});
    Effect.BlindUp(document.getElementById('your_content'),{duration:0.5,fps:50});
  }
  timg.blur();
}

function sfg_SetFirstLastSeq (yc,fseq,lseq) {
  if (fseq != '' && lseq != '') {
      sfg_RemoveFirst(yc);
      sfg_RemoveLast(yc);
      sfg_ApplyFirst(yc,fseq);
      sfg_ApplyLast(yc,lseq);
  } else {
    if (fseq != '') {
      sfg_RemoveFirst(yc);
      sfg_ApplyFirst(yc,fseq);
    }
    if (lseq != '') {
      sfg_RemoveLast(yc);
      sfg_ApplyLast(yc,lseq);
    }
  }
}

function sfg_RemoveFirst (yc) {
  document.getElementById('sfg_hed_item_'+yc.firstseqnum).className = 'your_content';
  document.getElementById('sfg_custom_imgclear_'+yc.firstseqnum).style.display = 'none';
  document.getElementById('sfg_custom_imgup_'+yc.firstseqnum).style.display = 'block';
}

function sfg_RemoveLast (yc) {
  document.getElementById('sfg_hed_item_'+yc.lastseqnum).className = 'your_content';
  document.getElementById('sfg_custom_imgclear_'+yc.lastseqnum).style.display = 'none';
  document.getElementById('sfg_custom_imgdown_'+yc.lastseqnum).style.display = 'block';
}

function sfg_ApplyFirst (yc,seqnum) {
  yc.firstseqnum = seqnum;
  document.getElementById('sfg_hed_item_'+yc.firstseqnum).className = 'your_content firstrow';
  document.getElementById('sfg_custom_imgup_'+yc.firstseqnum).style.display = 'none';
  document.getElementById('sfg_custom_imgclear_'+yc.firstseqnum).style.display = 'block';
}

function sfg_ApplyLast (yc,seqnum) {
  yc.lastseqnum = seqnum;
  document.getElementById('sfg_hed_item_'+yc.lastseqnum).className = 'your_content lastrow';
  document.getElementById('sfg_custom_imgdown_'+yc.lastseqnum).style.display = 'none';
  document.getElementById('sfg_custom_imgclear_'+yc.lastseqnum).style.display = 'block';
}

function sfg_GetCustomContent () {
  var your_content = document.getElementById('your_content');
  var j = 0;
  for (var i = 0; i < content_modules.length; ++i) { 
    var li = document.createElement('li');
    li.id = 'sfg_hed_item_'+content_modules[i].sfg_seqnum;
    li.sfg_seqnum = content_modules[i].sfg_seqnum;
    li.className = 'your_content';
    li.onmouseover = function () {this.style.backgroundColor = '#f8f8f0'};
    li.onmouseout = function () {this.style.backgroundColor = '#e2e5c6'};
    var imgup = document.createElement('img');
    imgup.id = 'sfg_custom_imgup_'+li.sfg_seqnum;
    imgup.src = 'http://imgs.sfgate.com/graphics/home/customize/moveup.gif';
    var func = 'sfg_ShiftContent('+li.sfg_seqnum+',"up"); return false;';
    imgup.onclick = new Function(func);      
    imgup.className = 'your_content';
    var imgdown = document.createElement('img');
    imgdown.id = 'sfg_custom_imgdown_'+li.sfg_seqnum;
    imgdown.src = 'http://imgs.sfgate.com/graphics/home/customize/movedown.gif';
    imgdown.className = 'your_content';
    func = 'sfg_ShiftContent('+li.sfg_seqnum+',"down"); return false;';
    imgdown.onclick = new Function(func);
    var imgclear = document.createElement('img');
    imgclear.id = 'sfg_custom_imgclear_'+li.sfg_seqnum;
    imgclear.src = 'http://imgs.sfgate.com/graphics/home/customize/moveclear.gif';
    imgclear.className = 'your_content';
    imgclear.style.display = 'none';
    imgclear.style.cursor = 'move';
    var handlebody = document.createElement('div');
    handlebody.className = 'handle';
    handlebody.innerHTML = content_modules[i].sfg_alttext;
    li.appendChild(imgup);
    li.appendChild(imgclear);
    li.appendChild(imgdown);
    li.appendChild(handlebody);
    your_content.appendChild(li);
  }
  Sortable.create('your_content',{handle:'handle',dropOnEmpty: true,constraint:'vertical',starteffect: function(element) {var cn = $(element).className; $(element).className = (cn.indexOf('lastrow') > 0 ? 'your_content alt' : 'your_content alt');  return true; }, endeffect: function(element) {var sequence=Sortable.sequence('your_content'); if ($(element).sfg_seqnum == sequence[0]) {$(element).className = 'your_content firstrow';} else if ($(element).sfg_seqnum == sequence[sequence.length-1]) {$(element).className = 'your_content lastrow';} else {$(element).className = 'your_content';} return true; }, onUpdate: function () {sfg_UpdateContentSetSeq()}});
  if (custom_content_cookie != '') {
    custom_content_cookie = custom_content_cookie.split(',');
    sfg_ApplyFirst(your_content,custom_content_cookie[0]);
    sfg_ApplyLast(your_content,custom_content_cookie[custom_content_cookie.length-1]);
    Sortable.setSequence('your_content',custom_content_cookie);
  } else {
    sfg_ApplyFirst(your_content,1);
    sfg_ApplyLast(your_content,content_modules.length);
  }
}

function sfg_ShiftContent (key, ud) {
  var sequence=Sortable.sequence('your_content');
  var newsequence=[];
  var reordered=false;
  var ordereditem=-1;
  var shift = (ud == 'up' ? -1 : 1);
  if (sequence.length>1) {
    for (var j=0; j<sequence.length; j++) {
      if ((ud == 'up' ? j>0 : j<(sequence.length-1)) && sequence[j].length > 0 && sequence[j]==key) {
        var temp=newsequence[j+shift];
        newsequence[j+shift]=key;
        newsequence[j]=(ud == 'up' ? temp : sequence[j+shift]);
        reordered=true;
        ordereditem=key
        if (ud == 'down') {j++;}
      } else {
        newsequence[j]=sequence[j];
      }
    }
  }
  if (reordered) {
    document.getElementById('sfg_hed_item_'+ordereditem).style.backgroundColor = '#e2e5c6';
    var yc = document.getElementById('your_content');
    if (yc.lastseqnum != newsequence[newsequence.length-1]) {
      sfg_RemoveLast(yc);
    } else if (yc.firstseqnum != newsequence[0]) {
      sfg_RemoveFirst(yc);
    }
    Sortable.setSequence('your_content',newsequence);
    if (yc.lastseqnum != newsequence[newsequence.length-1]) {
      sfg_ApplyLast(yc,newsequence[newsequence.length-1]);
    } else if (yc.firstseqnum != newsequence[0]) {
      sfg_ApplyFirst(yc,newsequence[0]);
    }
    sfg_UpdateContent();
  }
  return reordered;
}

function sfg_UpdateContent(setseq) {
  var sequence=(setseq ? setseq : Sortable.sequence('your_content'));
  var temp_ids = new Array();
  var cat_container = document.getElementById('categories_container');
  for (var i = 0; i < sequence.length; i++) {
    var mod = content_modules[sequence[i]-1];
    cat_container.appendChild(mod);
    if (mod.sfg_alttext == 'Business & Technology') {
      var sub_content = document.getElementsByClassName('sfg_hed011biz')[0];
      cat_container.appendChild(sub_content);
    }
    temp_ids.push(mod);
  }
  return false;
}

function sfg_UpdateContentSetSeq (setseq) {
  var sequence=(setseq ? setseq : Sortable.sequence('your_content'));
  var yc = document.getElementById('your_content');
  if (yc.firstseqnum != sequence[0] && yc.lastseqnum != sequence[sequence.length-1]) {
    sfg_SetFirstLastSeq(yc,sequence[0],sequence[sequence.length-1]);
  } else if (yc.firstseqnum != sequence[0]) {
    sfg_SetFirstLastSeq(yc,sequence[0],'');
  } else if (yc.lastseqnum != sequence[sequence.length-1]) {
    sfg_SetFirstLastSeq(yc,'',sequence[sequence.length-1]);
  }
  sfg_UpdateContent(setseq);
  return false;
}
function sfg_GetHpCustomContent () {
  if (document.cookie.length>0) {
    var c_name = 'sfg_HpCustom';
    c_start=document.cookie.indexOf(c_name+"=");
    if (c_start!=-1) { 
      c_start=c_start + c_name.length+1; 
      c_end=document.cookie.indexOf(";",c_start);
      if (c_end==-1) c_end=document.cookie.length;
      return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
  return "";
}
function sfg_SaveHpCustom (expiredays) {
  var value = (expiredays == -1 ? '' : Sortable.sequence('your_content').join(','));
  var exdate=new Date();
  exdate.setDate(exdate.getDate()+expiredays);
  document.cookie="sfg_HpCustom=" +escape(value)+
  ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
  sfg_ToggleConsole();
}
function sfg_CancelHpCustom () {
  var yc = document.getElementById('your_content');
  var custom_content_cookie = sfg_GetHpCustomContent();
  if (custom_content_cookie != '') {
    custom_content_cookie = custom_content_cookie.split(',');
    sfg_SetFirstLastSeq(yc,custom_content_cookie[0],custom_content_cookie[custom_content_cookie.length-1]);
    Sortable.setSequence('your_content',custom_content_cookie);
  } else {
    var ysr = new Array();
    for (var i = 0; i < content_modules.length; i++) {ysr.push(i+1);}
    sfg_SetFirstLastSeq(yc,1,ysr.length);
    Sortable.setSequence('your_content',ysr);
  }
  sfg_UpdateContent();
  sfg_ToggleConsole();
}
function sfg_ResetHpCustom () {
  var ysr = new Array();
  for (var i = 0; i < content_modules.length; i++) {ysr.push(i+1);}
  var yc = document.getElementById('your_content');
  sfg_SetFirstLastSeq(yc,1,ysr.length);
  Sortable.setSequence('your_content',ysr);
  sfg_UpdateContent();
  sfg_SaveHpCustom(-1);
}

// js/home/reload.js
// this reloads the page.
function sfgreload() {
    document.location.reload(false);
}
// called from the onload on the home page.
function setsfgreload() {
    // to deploy: comment out the code that checks on the location
//    var loc = new String(document.location);
//    if ( ! loc.match(/ssaux.u.sfgate.com/) ) {
//        return;
//    }

    // timing: 30 minutes.
    sfgreltimer = setTimeout("sfgreload()", 1800000);
    // this functionality is the "notification"
    //$('sfgnewcontent').style.display = 'none';
    // sfgckinterval = setInterval("sfg_ckupd()", 60000);
}
// notification request currently not used.
function sfg_ckupd() {
    var opt = {
        // Use POST
        method: 'post',
        // Handler
        onSuccess: function(t) {
            sfg_prupd(t)
        },
        // Handle other errors
        onFailure: function(t) {
            // alert('Error ' + t.status + ' -- ' + t.statusText);
        }
    }
    var req = new Ajax.Request('/widgets/home/important_v3.txt', opt);
}
// notification response, currently not used.
function sfg_prupd(t) {
    var respObj;
    try {
        respObj = eval( '('+t.responseText+')');
    } catch (e) {
        return;
    }
    // sfgate_dump('respObj: "'+respObj.last_upd+' '+document.location+'"\n');
    if ( sfgupd == 0 ) {
        sfgupd = respObj.last_upd;
        return;
    }
    if ( sfgupd != respObj.last_upd ) {
        sfgupd = respObj.last_upd;
        // currently disabled.
        // sfgate_dump("showing sfgnewconten\n");
        // $('sfgnewcontent').style.display = 'block';
    } else {
        // sfgate_dump("no changes sfgupd: "+sfgupd+"\n");
    }
}
//  end js/home/reload.js

