
// JS library

//var imgPath = rootVirtual + "/_img/"; // keep this a local variable, defined per function

window.onload = function()
{

    if (document.getElementById('barlogin')) // JS function for bar login popup
    {
        var aBarLogin = document.getElementById('barlogin');
        var aBarLoginHref = aBarLogin.getAttribute('href');
        aBarLogin.onclick = function()
        {
            return(popuplink(aBarLoginHref, this, 600, 300, true));
        }
    }


    if (document.getElementById('lowerleftcorner'))
    {
        randomLLCBackgroundImg();
    }


    if (document.getElementById('newsreleases')) // JS function for news item mouseover effect
    {
        var getNewsReleases = document.getElementById('newsreleases');
        var getNRDivs = getNewsReleases.getElementsByTagName('div');
        for (var i=0; i<getNRDivs.length; i++)
        {
            if (getNRDivs[i].className == "secondaryNewsItem")
            {
                getNRDivs[i].onmouseover = function()
                {
                    this.style.backgroundColor='#e9e8e7';
                }
                getNRDivs[i].onmouseout = function()
                {
                    this.style.backgroundColor='';
                }
            }
        }
    }


    if (document.getElementById('imageGallery')) // JS functions for switching between image gallery's, switching image's, and mouseover effect
    {
        // the page is init in CSS for non-JS users,
        // JS will init page for JS users
        document.getElementById('featuredPhoto').style.display='block';
        document.getElementById('featuredPhotoCaption').style.display='block';
        document.getElementById('tablist_container').style.display='block';
        document.getElementById('tab01').className='in';
        document.getElementById('igTable01').style.display='block';
        document.getElementById('igTable02').style.display='none';
        document.getElementById('igTable03').style.display='none';
        var tab01 = document.getElementById('tab01');
        var tab02 = document.getElementById('tab02');
        var tab03 = document.getElementById('tab03');
        
        var getThumbTables = document.getElementsByTagName("table"); // get all tables
        for (var i=0; i<getThumbTables.length; i++) // loop thru tables
        {
            if (getThumbTables[i].className == "thumbtable") // get only thumbtable's
            {
                var thumbtableTD = getThumbTables[i].getElementsByTagName("td"); // get all thumbtable child td's
                for (var j=0; j<thumbtableTD.length; j++) // loop thru td's
                {
                    thumbtableTD[j].onmouseover = function() // call td onmouseover
                    {
                        this.style.backgroundColor='#e9e8e7'; // change bgcolor
                    }
                    thumbtableTD[j].onmouseout = function() // call td onmouseout
                    {
                        this.style.backgroundColor=''; // change bgcolor
                    }
                }
                var thumbtableA = getThumbTables[i].getElementsByTagName("a"); // get all thumbtable child anchor's
                for (var k=0; k<thumbtableA.length; k++) // loop thru a's
                {
                    thumbtableA[k].onclick = function() // call anchor onclick
                    {
                        swapIGImg(this);
                        return false;
                    }
                }
            }
        }
        tab01.onclick = function()
        {
            switchImageGallery('tab01','igTable01');
        }
        tab02.onclick = function()
        {
            switchImageGallery('tab02','igTable02');
        }
        tab03.onclick = function()
        {
            switchImageGallery('tab03','igTable03');
        }
    }


    // run JS functions

    function randomLLCBackgroundImg()
    {
	    //create an array of background images
	    var arrBGImg = new Array("corner1.jpg","corner2.jpg","corner4.jpg","corner5.jpg","corner6.jpg","corner7.jpg","corner8.jpg");

	    //set the path to the bottle images
        var imgPath = rootVirtual + "/_img/_corner/";

	    //get a random number (rn) between 0 and the number of bottle images available
	    var rn = Math.round(Math.random() * arrBGImg.length);
	    while (rn <= 0 || rn > arrBGImg.length) rn = Math.round(Math.random() * arrBGImg.length);

	    //change the background image
	    document.getElementById('lowerleftcorner').style.backgroundImage = "url(" + imgPath + arrBGImg[rn -1] + ")";
    }

    function resetImageGallery()
    {
	    document.getElementById('tab01').className='';
	    document.getElementById('igTable01').style.display='none';
	    document.getElementById('tab02').className='';
	    document.getElementById('igTable02').style.display='none';
	    document.getElementById('tab03').className='';
	    document.getElementById('igTable03').style.display='none';
    }
    function switchImageGallery(sigTab,sigTable)
    {
        resetImageGallery();
	    document.getElementById(sigTab).className='in';
	    var getIGTable = document.getElementById(sigTable);
	    getIGTable.style.display='block';
	    var getIGTableA = getIGTable.getElementsByTagName("a");
        swapIGImg(getIGTableA[0]);
        return false;
    }

    function swapIGImg(newIGImg){
        var newIGImgSource = newIGImg.getAttribute("href");
        var newIGImgCaption = newIGImg.getAttribute("title");
        var featuredPhoto = document.getElementById('featuredPhoto');
        featuredPhoto.setAttribute("src",newIGImgSource);
        featuredPhoto.setAttribute("alt",newIGImgCaption);
        featuredPhoto.setAttribute("title",newIGImgCaption);
        document.getElementById('featuredPhotoCaption').innerHTML = newIGImgCaption;
    }


}


//************************************
// misc JS, inline event handlers, etc

//swap bgcolor, used for the 3rd-party IR pages
function mOverBG(src){
    src.style.backgroundColor='#e9e8e7';
}
function mOutBG(src){
    src.style.backgroundColor='';
}


function showInfo(showID,hideID){
	document.getElementById(showID).className='divHideInfo';
	document.getElementById(hideID).className='divShowInfo';
}
function hideInfo(showID,hideID){
	document.getElementById(showID).className='divShowInfo';
	document.getElementById(hideID).className='divHideInfo';
}




// POP UP
// usage: popuplink(['js-only url',] this[, w[, h[, scroll[, extras]]]])
// basic usage: <a href="popup.html" target="_blank" onclick="return(popuplink(this));">new pop</a>
// advanced usage: <a href="popup_nojs.html" target="_blank" onclick="return(popuplink('popup_yesjs.html', this, 200, 100, false));">new pop</a>
// site-wide defaults:
POPUP_W = 400;
POPUP_H = 300;
POPUP_SCROLL = true;
POPUP_EXTRAS = 'location=0,statusbar=0,menubar=0';
function popuplink() {
	var undef, i=0, args=popuplink.arguments;
	var url = (typeof(args[i])=='string') ? args[i++] : args[i].getAttribute('href');
	var target = args[i++].getAttribute('target') || '_blank';
	var w = args[i++];
	var h = args[i++];
	var s = (args[i]===undef) ? POPUP_SCROLL : args[i++];
	var features = 'width=' + (w || POPUP_W)
				 + ',height=' + (h || POPUP_H)
				 + ',scrollbars=' + (s ? 'yes,' : 'no,')
				 + (args[i] || POPUP_EXTRAS);
	var win = window.open(url, target, features);
	win.focus();
	return false;
}
// END POP UP

// end misc JS
//************



/* 
Alex's JS using JS written by Pop
 */
 
//<![CDATA[
	//
	//	Thumbnail Switcher v0.1
	//	Gavin Suntop
	//
	$(document).ready(function(){
	
		// Only run when thumbnails are present:
		if($("div.thumbnails").length) {
			
			// Get number of thumbnails
			var thumbCount = $("div.thumbnails div").length;	
			
			// Start with the 1st thumbnail			
			var currentThumb = 0;
			
			// Hide all but 1st thumbnail
			$("div.thumbnails div:not(:first)").hide();					
			
			// Insert thumbnail navigation
			$("div.thumbnails").append(
				'<div class="thumb_nav">' +
				'<span class="thumb-prev thumbSwitch">&lt; Previous<\/span> ' +
				'<span class="thumb-numbers">[<span class="currentThumb">1<\/span> of ' + thumbCount + ']</span> ' +
				'<span class="thumb-next thumbSwitch">Next &gt;<\/span>' +
				'<\/div>'
			);
			

			//	----------------
			//	EVENT LISTENERS:
			//	----------------
			
			// Thumbnail Nav Click (For either direction)
			$("div.thumb_nav span.thumbSwitch").click(function(){
			
				// Hide current thumbnail
				$("div.thumbnails div:eq("+currentThumb+")").hide();
				
				// Increment or decrement thumbnail based on nav direction
				if($(this).hasClass("thumb-prev"))
					currentThumb != 0 ? currentThumb-- : currentThumb = (thumbCount-1);
				else 
					currentThumb != (thumbCount-1) ? currentThumb++ : currentThumb = 0;

				// Show new thumbnail
				$("div.thumbnails div:eq("+currentThumb+")").show();
				
				// Update the text display
				$("div.thumb_nav span.currentThumb").html(currentThumb+1);
				
			});
		}
	});
//]]>

// To make sure the last item in the news articles do not have a border on the bottom
$(document).ready(function() {
    $("ul#mediaList li:last, div#content .primaryNewsItem:last").css("border", "none");
});

// Truncating plugin to save me time and the client money.
jQuery.fn.truncate = function( max, settings ) {
    settings = jQuery.extend( {
        chars: /\s/,
        trail: [ "...", "" ]
    }, settings );
    var myResults = {};
    var ie = $.browser.msie;
    function fixIE( o ) {
        if ( ie ) {
            o.style.removeAttribute( "filter" );
        }
    }
    return this.each( function() {
        var $this = jQuery(this);
        var myStrOrig = $this.html().replace( /\r\n/gim, "" );
        var myStr = myStrOrig;
        var myRegEx = /<\/?[^<>]*\/?>/gim;
        var myRegExArray;
        var myRegExHash = {};
        var myResultsKey = $("*").index( this );
        while ( ( myRegExArray = myRegEx.exec( myStr ) ) != null ) {
            myRegExHash[ myRegExArray.index ] = myRegExArray[ 0 ];
        }
        myStr = jQuery.trim( myStr.split( myRegEx ).join( "" ) );
        if ( myStr.length > max ) {
            var c;
            while ( max < myStr.length ) {
                c = myStr.charAt( max );
                if ( c.match( settings.chars ) ) {
                    myStr = myStr.substring( 0, max );
                    break;
                }
                max--;
            }
            if ( myStrOrig.search( myRegEx ) != -1 ) {
                var endCap = 0;
                for ( eachEl in myRegExHash ) {
                    myStr = [ myStr.substring( 0, eachEl ), myRegExHash[ eachEl ], myStr.substring( eachEl, myStr.length ) ].join( "" );
                    if ( eachEl < myStr.length ) {
                        endCap = myStr.length;
                    }
                }
                $this.html( [ myStr.substring( 0, endCap ), myStr.substring( endCap, myStr.length ).replace( /<(\w+)[^>]*>.*<\/\1>/gim, "" ).replace( /<(br|hr|img|input)[^<>]*\/?>/gim, "" ) ].join( "" ) );
            } else {
                $this.html( myStr );
            }
            myResults[ myResultsKey ] = myStrOrig;
            $this.html( [ "<div class='truncate_less'>", $this.html(), settings.trail[ 0 ], "</div>" ].join( "" ) )
            .find(".truncate_show",this).click( function() {
                if ( $this.find( ".truncate_more" ).length == 0 ) {
                    $this.append( [ "<div class='truncate_more' style='display: none;'>", myResults[ myResultsKey ], settings.trail[ 1 ], "</div>" ].join( "" ) )
                    .find( ".truncate_hide" ).click( function() {
                        $this.find( ".truncate_more" ).fadeOut( "normal", function() {
                            $this.find( ".truncate_less" ).fadeIn( "normal", function() {
                                fixIE( this );
                                $(this).css( "background", "none" );
                            });
                            fixIE( this );
                        });
                        return false;
                    });
                }
                $this.find( ".truncate_less" ).fadeOut( "normal", function() {
                    $this.find( ".truncate_more" ).fadeIn( "normal", function() {
                        fixIE( this );
                    });
                    fixIE( this );
                });
                jQuery(".truncate_show",$this).click( function() {
                    $this.find( ".truncate_less" ).fadeOut( "normal", function() {
                        $this.find( ".truncate_more" ).fadeIn( "normal", function() {
                            fixIE( this );
                            $(this).css( "background", "none" );
                        });
                        fixIE( this );
                    });
                    return false;
                });
                return false;
            });
        }
    });
};

// function call for the truncating plugin
$(document).ready(function() {
    $(".responsChartDescr").truncate( 220,{
     //   chars: "\\.",
        trail: [ "... <span class='truncateTrigger truncate_show'>(see more +)</span>", "<span class='truncateTrigger truncate_hide'>(see less - )</span>" ]
    });
});

