var iconTimer = null;
var animIcon = 1;
$(function() {
    if ($('#main').hasClass('products')) {
        // in product page, just expand product section
        $('#subProducts').css({'margin-bottom': '10px', 'display': 'block'});
        $('#home').css('margin-top', '10px').parent().css('margin-top', '10px');
        $('#nav li.postProd').css('margin-top', '2px');
        $('#products').parent().css('margin-top', '7px');
    } else {
        // not in product page, setup expanding animation
    	$('#products').click(function(evt) {
    		evt.preventDefault();
    		if ($('#subProducts').css('display') == 'none') {
    			$('#subProducts').slideDown(500).css('margin-bottom', '10px');
       			$('#home').animate({marginTop: 10}, 500).parent().animate({marginTop: 10}, 500);
       			$('#nav li.postProd').animate({marginTop: 2}, 500);
       			$('#products').parent().animate({marginTop: 7}, 500);
       			$(this).addClass('highlighted');
       		} else {
       			$('#subProducts').slideUp(500);
       			$('#home').animate({marginTop: 38}, 500).parent().animate({marginTop: 20}, 500);
       			$('#nav li.postProd').add($('#products').parent()).animate({marginTop: 20}, 500);
       			$('#subProducts').css('margin-bottom', '0');
       			$(this).removeClass('highlighted');
       		}
        })
    }
	
	// home page icons auto rotation
	$('#item1Desc, #item2Desc').hide();
    var infoItems = $('#item1, #item2, #item3');
    infoItems.mouseover(function(evt) {
        if (iconTimer != null) {
    		clearInterval(iconTimer);
        }
		switchIcons(this.id);
	});
    if ($('#main').hasClass('home')) {
        infoItems.mouseout(function(evt) {
            iconTimer = setInterval('rotateIcons()', 5000);
        });
        iconTimer = setInterval('rotateIcons()', 5000);
    }

	// dialog functionality
	$('a.dialog').click(function(evt) {
		evt.preventDefault();
		$('#dlg_content').load(this.href, null, loadComplete)
		$('#screen').css({'width': $(document).width(), 'height': $(document).height()})
		if ($.browser.msie) {
			$('#screen').show();
		} else {
			$('#screen').fadeIn(300);
		}
	});
	$('#dlg_t a').click(function(evt) {
		evt.preventDefault();
		if ($.browser.msie) {
			$('#dialog, #screen').hide();
		} else {
			$('#dialog, #screen').fadeOut(300);
		}
	});
	
    // this is for the text that swaps out on the basic packaging section
    // basically needs to find the tallest list and set the outer div to that
    // height so everything flows correctly on the site.
    var maxHeight = 0;
    $('div.swapItems').children('ul').each(function() {
        if ($(this).height() > maxHeight) {
            maxHeight = $(this).height();
        }
        if (!$(this).hasClass('selected')) {
            $(this).hide();
        }
    }).end().css('height', maxHeight);

	// accordion functionality
    $('div.accCont').hide();
	$('#content div.accItem h2, span.accTitle').click(function(evt) {
	    hideAccordions(this.id);
		$(this).parent().parent().children('div.accCont').each(function() {
            $(this).slideToggle(500);
        });
        $(this).parent().children('div.tabs').css('visibility', 'visible');
	});
    if (document.location.hash != '') {
        $(document.location.hash + ' div.accCont').slideDown(500);
        $(document.location.hash + ' div.accHeader div.tabs').css('visibility', 'visible');
    } else {
        $('#content div.accItem:first div.accCont').slideDown(500);
        $('#content div.accItem:first div.accHeader div.tabs').css('visibility', 'visible');
    }


    // products inner accordion swap functionality
    $('#main.products div.tabs').children('a').click(function(evt) {
        evt.preventDefault();
        if ($('#' + this.id + '_img').size() > 0) {
            if (!$(this).hasClass('selected')) {
                $(this).parent().children('a').each(function() {
                    if ($(this).hasClass('selected')) {
                        $(this).removeClass('selected');
                        $('#' + this.id + '_img, #' + this.id + '_ul').fadeOut(300);
                    }
                })
                $(this).addClass('selected');
                $('#' + this.id + '_img, #' + this.id + '_ul').fadeIn(300);
                $('#' + this.id.substring(0, 6) + '_tmplt').attr('href', $(this).attr('_template'));
            }
        }
    });
	
	// gallery functionality
	$('#images div').mouseover(function(evt) {
		if (!$(this).hasClass('hilite')) {
			// need to swap to this image
			swapImage(this.id);
		}
	});
	
	// customer quotes functionality
	if ($('#custQuotes').size() > 0) {
		setTimeout('swapQuotes(1)', 10000);
	}

    $('#upcAd').click(function() {
        location = 'services.php#accordion8';
    })
})
function rotateIcons() {
	switchIcons('item' + animIcon);
	animIcon = (animIcon >= 3 ? 1 : animIcon + 1);
}
function switchIcons(icon) {
	id = icon + 'Desc';
	if (id == 'item1Desc') {
		$('#item2').animate({left: 280}, 300);
		$('#item3').animate({left: 340}, 300);
		$('#item2Desc, #item3Desc').fadeOut(200);
		$('#item1Desc').fadeIn(300);
		$('#item1').animate({left: 0}, 300);
	} else if (id == 'item2Desc') {
		$('#item1').animate({left: 0}, 300);
		$('#item3').animate({left: 340}, 300);
		$('#item1Desc, #item3Desc').fadeOut(200);
		$('#item2Desc').fadeIn(300);
		$('#item2').animate({left: 60}, 300);
	} else {
		$('#item1').animate({left: 0}, 300);
		$('#item2').animate({left: 60}, 300);
		$('#item1Desc, #item2Desc').fadeOut(200);
		$('#item3Desc').fadeIn(300);
		$('#item3').animate({left: 120}, 300);
	}
}
function loadComplete() {
	var dlg = $('#dialog');
	var width = dlg.width() / 2;
	var height = dlg.height() /2;
	dlg.css({marginLeft: -width, marginTop: $(window).scrollTop() - height});
	if ($.browser.msie) {
		$('#dialog').show();
	} else {
		$('#dialog').fadeIn(600);
	}
}
function swapDialog(url, params) {
	if ($.browser.msie) {
		$('#dialog').hide();
		$('#dlg_content').load(url, params, loadComplete);
	} else {
		$('#dialog').fadeOut(300, function() {
			$('#dlg_content').load(url, params, loadComplete);
		});
	}
}
function hideAccordions(id) {
	$('#content div.accItem').each(function() {
		if (this.id != id) {
   			$(this).children('div.accCont').slideUp(300);
            $(this).children('div.accHeader').children('div.tabs').css('visibility', 'hidden');
		}	
	})
}
function swapImage(id) {
	$('#images div').each(function() {
		if ($(this).hasClass('hilite')) {
			// need to hide
			$(this).removeClass('hilite');
			$('#' + this.id.replace(/image/, 'img')).fadeOut(400);
			$('#' + this.id + 'Desc').fadeOut(400);
		}
	})
	$('#' + id).addClass('hilite');
	$('#' + id.replace(/image/, 'img')).fadeIn(400);
	$('#' + id + 'Desc').fadeIn(400);
}
function swapQuotes(idNum) {
	$('#quote' + (idNum % 2)).load('quotes/quote' + idNum + '.html', fadeQuotes);
	if (idNum == 5) {
		idNum = 1;
	} else {
		idNum += 1;
	}
	setTimeout('swapQuotes(' + idNum + ')', 10000);
}
function fadeQuotes() {
	if (this.id == 'quote0') {
		$('#quote0').fadeIn(400);
		$('#quote1').fadeOut(400);
	} else {
		$('#quote0').fadeOut(400);
		$('#quote1').fadeIn(400);
	}
}