/**
 * Setup handles & divs for tabbed content areas. 
 * USAGE:
 *	tab = new Tabs({
 *		tabs:['tab1','tab2'],
 *		divs:['div1','div2']
 *	}); 
 */
var Tabs = new Class({
	Implements: [Events,Options],
	els: {},
	options: {
        init: 0,
		onInitialized: function() {},
		onCompleted: function() {}
	},
	initialize: function() {
		var params = Array.link(arguments, {options:Object.type});
		this.setOptions(params.options || null);
		this.setupEvents();
		this.setupTabs(); 
		this.fireEvent('initialized',this);
	},
	setupEvents: function() {
		this.options.tabs.each(function(item,index){
			$(item).addEvent('click',function(e){
				var e = new Event(e); 
					e.stop();
                location.hash = "#"+(index+1); 
				this.activate(this.options.divs[index]); 
			}.bind(this));
		},this); 
	},
	setupTabs: function() {
        var init = this.options.init; 
        if(!this.options.init) {
            this.options.init = location.hash.length==2 ? location.hash.substring(1) : 1;
            var init = this.options.init!=0 && this.options.init-1<this.options.divs.length ? this.options.init-1 : 0; 
        }
		this.activate(this.options.divs[init]); 
	},
	activate: function(id) {
		var idx,obj; 
		this.options.divs.each(function(item,index){
			if(id==item) {
				idx = index; 
				obj = $(item); 
			}
			$(item).setStyle('display','none');
		}); 
		obj.setStyle('display','block');
		this.reset(this.options.tabs[idx]); 
	},
	reset: function(id) {
		this.options.tabs.each(function(item,index){
			$(item).removeClass('active');
		}); 
		$(id).addClass('active');
		this.fireEvent('completed',id);
	}
});


/**
 * Setup objects @ domready
 */
var tab;
window.addEvent('domready',function(){
});

function showHideDiv(divID) 
{
    if (document.getElementById) 
    { // DOM3 = IE5, NS6
        if(document.getElementById(divID).style.display == 'none')
        {
            document.getElementById(divID).style.display = 'block';
        }
        else
        {
            document.getElementById(divID).style.display = 'none';
        }
    }
    else 
    {
        if (document.layers) 
        { // Netscape 4
            if(document.divID.display == 'none')
            {
                document.divID.display = 'block';
            }
            else
            {
                document.divID.display = 'none';
            }
        }
        else 
        { // IE 4
            if(document.all.divID.style.display == 'none')
            {
                document.all.divID.style.display = 'block';
            }
            else
            {
                document.all.divID.style.display = 'none';
            }
        }
    }
}

function winPop(pageName) {
    
    winPopHeightWidth(pageName,500,950);
}

function winPopHeightWidth(pageName, height, width, resizable)
{

    if (navigator.appName=="Netscape" && window.popWin) window.popWin.close();
       
        if( (pageName.lastIndexOf('pdf') > pageName.length-4 ||pageName.indexOf('ashx') > 0 ) &&  ifAdobePresent() == false)
          { 
            var getAcrobat = confirm("You do not have Adobe Acrobat required to open this document. Would you like to download a free copy of Adobe Acrobat PDF Reader?");
            if(getAcrobat )
               { 
                    pageName = "http://www.adobe.com/products/acrobat/readstep2.html"
               }
          }
        // alert('hello ' +width+' '+height);
        var resize = "no";
        if (resizable != null)
        {
			resize = (resizable ? "yes" : "no");
        }
        popWin = window.open(pageName,'popWin','width='+width+',height='+height+',toolbar=yes,location=no,directories=no,status=yes,menubar=yes,scrollbars=yes,resizable=' + resize);
       
        if(window.popWin)
            window.popWin.focus();
        donebeenpopped=true;
}

//Detect pdf reader
function ifAdobePresent()
{
    var acrobat=new Object();
    acrobat.installed=false;
    acrobat.version='0.0';

    if (navigator.plugins && navigator.plugins.length)
    {
        for (x=0; x < navigator.plugins.length; x++)  {
            if (navigator.plugins[x].description.indexOf('Adobe PDF') != -1)
            {
                acrobat.version=parseFloat(navigator.plugins[x].description.split('Version ')[1]);
                //document.write(acrobat.version);
                if (acrobat.version.toString().length == 1) acrobat.version+='.0';

                acrobat.installed=true;
                break;
            }
        }
    }
    else if (window.ActiveXObject)
    {
        
        try //Latest versions  - 7 and 8 
        {
            oAcro7=new ActiveXObject('AcroPDF.PDF.1');
            if (oAcro7)
            {
                acrobat.installed=true;
                acrobat.version='7.x|8.x';
            }
        }
        catch(e) {}
        if( acrobat.installed == false) 
        {  // if not found check version 4.x 
            try
            {
                oAcro4=new ActiveXObject('PDF.PdfCtrl.1');
                if (oAcro4)
                {
                    acrobat.installed=true;
                    acrobat.version='4.x';
                }
            }
            catch(e) {}
        }
        if( acrobat.installed == false)
        { // else check the really old 2.x stuff
            for (x=2; x<10; x++)  
            {
                try
                {
                    oAcro=eval("new ActiveXObject('PDF.PdfCtrl."+x+"');");
                    if (oAcro)
                    {
                        acrobat.installed=true;
                        acrobat.version=x+'.0';
                        break;
                    }
                }
                catch(e) {}
            }
        }
   }
   return acrobat.installed;
}
