// JavaScript Document
var timer, currentDD;
var timeout = 1000;
var printwindow;

$(document).ready(function () {
    var flashpath = '';
    if (location.href.indexOf('siteadmin') != -1) {
        flashpath = '../' + flashpath;
    }
    if ($('#project-mod').length > 0) {
        var flashvars = {};
        var params = { wmode: 'transparent' };
        swfobject.embedSWF(flashpath + 'kimbermap.swf', 'project-mod', '209', '197', '9.0.0', false, flashvars, params);
    }
    if ($('#mexicomap').length > 0) {
        swfobject.embedSWF(flashpath + 'mexicomap.swf', 'mexicomap', '482', '305', '9.0.0');
    }
    if ($('.flashbanner').length) {
        var flashvars = {};
        var params = { wmode: 'transparent' };
        swfobject.embedSWF(flashpath + 'banner_loader.swf', 'bannerSection', '860', '230', '10', false, flashvars, params);
    }    

    if (!Modernizr.boxshadow) {
        $('#wrapper').wrap('<div id="wrapper-Out"></div>').wrap('<div id="wrapper-L"></div>').wrap('<div id="wrapper-R"></div>');
        $('#wrapper-Out').append('<div id="wrapper-BL"><div id="wrapper-BR"><div id="wrapper-B"><!--stuff--></div></div></div>');
    }
    if (Modernizr.multiplebgs) {
        $('#mineral-back').empty();
        $('#mineral-back').append('<div id="mineral-holder"><div id="mineral-full"></div></div>');
    }
    //menu events
    $('li.link > a:first-child').each(function () {
        $(this).hover(
	        openDD,
	        setCloseDD
	    );
    });
    //dropdown events
    $('.dropdown').each(function () {
        $(this).hover(
	        cancelClose,
	        setCloseDD
	    );
    });
    //select parent of subsubpage in sidebar
    $('.subsubpage > li a.current').parent().parent().prev().addClass('currentParent');
    //select parent of subpage in sidebar
    $('.subpage > li a.current').parent().parent().prev().addClass('currentParent');

    //fight MAC validation stuff, using some lame hacks
    $('input, select').attr('disabled', 'disabled');
    $('a').not('#quick-report-actions a').live('click', removeViewState);
    $('.propertyLinks').live('mousemove', removeViewState);
    //$('#mainContent').mousemove(removeViewState);
    //$('select').live('change',removeViewState);

    var browser = navigator.userAgent; //only interested in finding out if it's stupid IE    

    if (browser.indexOf('MSIE') != -1) {
        var verspos = browser.indexOf('MSIE');
        var b_version = browser.substring(verspos, browser.indexOf(';', verspos));
        var version = parseFloat(b_version.replace('MSIE', ''));

        if (version < 8) {
            var imagepath = 'images/';
            if (location.href.indexOf('siteadmin') != -1) {
                imagepath = '../' + imagepath;
            }
            if (version < 7) { //detect IE 6
                $('.goldButton').append('&nbsp;&nbsp;<img src="' + imagepath + 'right-triangle.gif" />');
                $('.gridPager .prev').prepend('<img src="' + imagepath + 'left-triangle.gif" />&nbsp;&nbsp;');
                $('.gridPager .next').append('&nbsp;&nbsp;<img src="' + imagepath + 'right-triangle.gif" />');
            } else {
                $('.goldButton').append('&nbsp;&nbsp;<img src="' + imagepath + 'right-triangle.png" />');
                $('.gridPager .prev').prepend('<img src="' + imagepath + 'left-triangle.png" />&nbsp;&nbsp;');
                $('.gridPager .next').append('&nbsp;&nbsp;<img src="' + imagepath + 'right-triangle.png" />');
            }
            $('#sidebarNav li ul.subpage li .subsubpage li a').prepend('- ');
            $('.depositTable caption').attr('align', 'bottom');
        }
    }
});
 
 //when page completely loaded including images
$(window).load(function () {
    var bannerHeight = $('#bannerSection img').length ? $('#bannerSection img').height() : $('#bannerSection').height();
    var newHeight;

    if (Modernizr.boxshadow) {
        var padHeight = 6;
        newHeight = bannerHeight + padHeight;
        $('#mineral-left-pic').css({ 'height': newHeight + 'px' });
        $('#mineral-right-pic').css({ 'height': newHeight + 'px' });
        $('#mineral-back').css({ 'height': newHeight + 'px' });
        $('#mineral-full').css({ 'height': newHeight + 'px' });
    } else {
        var topHeight = $('#mainHead').height() + $('#stockbar').height() + $('#mainNav').height();
        newHeight = bannerHeight + topHeight;
        $('#mineral-left-pic').css({ 'height': newHeight + 'px' });
        $('#mineral-right-pic').css({ 'height': newHeight + 'px' });
        $('#backgroundSection').css({ 'height': newHeight + 11 + 'px' });
        $('#backgroundSection').append('<div id="mineral-bottom"><!--stuff--></div>');
    }
    $('body#printwindow').each(function () {
        if (confirm('Do you want to print this document?')) {
            window.print();
        }
    });
    if ($('select').length) {
        removeViewState();
    }
    $('form').submit(function () {
        var url = document.location.href;
        var urlparts = url.split('?');
        var req = urlparts[1];
        if (req.indexOf('add') != -1) {
            removeViewState();
        }
    });
    enableForm();
});

function enableForm() {
    $("input, select").removeAttr('disabled');
}
function removeViewState() {
    $('#__VIEWSTATE').remove();
}


//gets the id of the corresponding dropdown from the link id
function getDropID(id){
    var dd = '#' + $(id).attr('id');
    return dd.replace('link','drop');
}
//opens a dropdown
function openDD(){
    cancelClose();   
    var newDD = getDropID($(this));
    //alert (newDD+' '+currentDD);
    if ((currentDD != null)&&(currentDD != newDD)){
		closeDD();
	}
	currentDD = newDD;	

	$(currentDD).show();	
}
//starts the countdown to close a dropdown
function setCloseDD(){
    timer = setTimeout(closeDD, timeout);	
}
//closes the dropdown
function closeDD(){
    $(currentDD).hide();
    currentDD = null;
}
//cancels the closedown
function cancelClose(){
	clearTimeout(timer);
}
var urlobj;
function textbox_browser(n)
{ 
    BrowseServer($('#'+n.id).prev().attr('id'));
    ///BrowseServer(sib);
}
//opens the FCKeditor browse server outside of the editor
function BrowseServer(obj)
{
	urlobj = obj;
	
	OpenServerBrowser(
		'fckeditor/editor/filemanager/browser/default/browser.html?Connector=../../connectors/aspx/connector.aspx',
		screen.width * 0.7,
		screen.height * 0.7 ) ;
}
//opens the FCKeditor browse server outside of the editor
function OpenServerBrowser( url, width, height )
{
	var iLeft = (screen.width  - width) / 2 ;
	var iTop  = (screen.height - height) / 2 ;

	var sOptions = "toolbar=no,status=no,resizable=yes,dependent=yes" ;
	sOptions += ",width=" + width ;
	sOptions += ",height=" + height ;
	sOptions += ",left=" + iLeft ;
	sOptions += ",top=" + iTop ;

	var oWindow = window.open( url, "BrowseWindow", sOptions ) ;
}
function SetUrl( url, width, height, alt )
{
	document.getElementById(urlobj).value = url ;
	oWindow = null;
}
function showEdit(){
    $find('editext').show();
}
function reset(){
    $('INPUT[type="checkbox"]').each(function (){
        $(this).attr('checked',false);
    });
}
//opens the print window so it can have it's location adjusted below, and not be pop-up blocked
function openPrintWindow(){
    printwindow = window.open('','printwindow',
    'width=800,height=600,status=no,location=no,menubar=no,toolbar=no,scrollbars=yes');    
}
//changes the print window location based on what the server has determined.
function printQuickReport(query){
    printwindow.location = '../printreport.aspx?' + query;    
}
