var is_opera =  navigator.userAgent.indexOf('Opera') > -1;
var is_safari = navigator.userAgent.indexOf('AppleWebKit/') > -1 && navigator.userAgent.indexOf('Chrome') == -1;
var is_chrome = navigator.userAgent.indexOf('Chrome') > -1;
var is_firefox = navigator.userAgent.indexOf('Firefox') > -1;
var is_mac = navigator.platform.toLowerCase().indexOf('mac') > -1;
var is_linux = navigator.platform.toLowerCase().indexOf('linux') > -1;
var ie6 = false;
var ie7 = false;
var ie8 = false;

function doTotal() {	
	var aAmt = (document.getElementById('amount_1').value != '') ? eval(document.getElementById('amount_1').value) : 0;
	var bAmt = (document.getElementById('amount_2').value != '') ? eval(document.getElementById('amount_2').value) : 0;
	var cAmt = (document.getElementById('amount_3').value != '') ? eval(document.getElementById('amount_3').value) : 0;
	var dAmt = (document.getElementById('amount_4').value != '') ? eval(document.getElementById('amount_4').value) : 0;
	var eAmt = (document.getElementById('amount_5').value != '') ? eval(document.getElementById('amount_5').value) : 0;
	var aQty = (document.getElementById('quantity_1').value != '') ? eval(document.getElementById('quantity_1').value) : 1;
	var bQty = (document.getElementById('quantity_2').value != '') ? eval(document.getElementById('quantity_2').value) : 0;
	var cQty = (document.getElementById('quantity_3').value != '') ? eval(document.getElementById('quantity_3').value) : 0;
	var dQty = (document.getElementById('quantity_4').value != '') ? eval(document.getElementById('quantity_4').value) : 0;
	var eQty = (document.getElementById('quantity_5').value != '') ? eval(document.getElementById('quantity_5').value) : 0;
	
	var aTotal = (aAmt * aQty);
	var bTotal = (bAmt * bQty);
	var cTotal = (cAmt * cQty);
	var dTotal = (dAmt * dQty);
	var eTotal = (eAmt * eQty);
	var orderTotal = aTotal + bTotal + cTotal + dTotal + eTotal;
	var calcPercentage = (orderTotal * .029);
	var handlingFee = roundNumber(calcPercentage,2) + .30;
	var hFee = handlingFee.toFixed(2);
	document.sales.handling_cart.value = hFee;
}

function roundNumber(num, dec) {
	var result = Math.round( Math.round( num * Math.pow( 10, dec + 1 ) ) / Math.pow( 10, 1 ) ) / Math.pow(10,dec)
	return result;
}

$.fn.getTitle = function() {
	var arr = jQuery("a.fancybox");
	jQuery.each(arr, function() {
		var title = jQuery(this).children("img").attr("title");
		jQuery(this).attr('title',title);
	})
}

$(document).ready(function() {

	navigation_handler.setup();
	
	if(is_safari) {
		$('body').addClass('browser-safari');
	}

	if(is_chrome) {
		$('body').addClass('browser-chrome');
	}
		
	if(is_opera) {
		$('body').addClass('browser-opera');
	}

	if(is_firefox) {
		$('body').addClass('browser-firefox');
	}
	
	if(is_mac) {
		$('body').addClass('platform-mac');	
	}	

	var all_links = $('a[href]');
	var hostname = window.location.hostname;
	hostname = hostname.replace("www.","").toLowerCase();

	all_links.map(function(i, el){
		if(($(el).attr('href').indexOf('http://') != -1 || $(el).attr('href').indexOf('https://') != -1) && $(el).attr('href').indexOf(hostname) == -1) {
			$(el).attr('target', '_blank');
		}
		
		if($(el).attr('href').indexOf('.pdf') != -1) {
			$(el).attr('target', '_blank');
		}
	});
	
	var thumbnails = '.default-txt a:has(img)[href$=".bmp"],.default-txt a:has(img)[href$=".gif"],.default-txt a:has(img)[href$=".jpg"],.default-txt a:has(img)[href$=".jpeg"],.default-txt a:has(img)[href$=".png"],.default-txt a:has(img)[href$=".BMP"],.default-txt a:has(img)[href$=".GIF"],.default-txt a:has(img)[href$=".JPG"],.default-txt a:has(img)[href$=".JPEG"],.default-txt a:has(img)[href$=".PNG"]';

	$(thumbnails).addClass("fancybox").attr("rel","fancybox").getTitle();

	$("a.fancybox").fancybox({
		'overlayOpacity' : 0.6,
		'overlayColor': '#000',
		'padding': 1,
		'cyclic': 1,
		'titlePosition': 'over',
		'centerOnScroll': true				
	});		
	
	$('.default-txt tr').map(function(i, el){
		if(i%2==0) {
			$(el).addClass('odd');
		} else {
			$(el).addClass('even');
		}
    })

	
	if(typeof content_handler == 'object') {
		content_handler.setup();
	}

	if(typeof slideshow_handler == 'object') {
		slideshow_handler.setup();
	}

	$('#paypal-info a').bind('click', function(event) {
		event.preventDefault();
		window.open('https://www.paypal.com/us/cgi-bin/webscr?cmd=xpt/Marketing/popup/OLCWhatIsPayPal-outside','olcwhatispaypal','toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=550, height=470');
	});
	
	$('#sales').bind('submit', function(event) {
		doTotal();
	});
	
	if($('body.home').length > 0) {
		
		var el_h2 = $('#news-and-events h2');
		if( el_h2.length > 0) {
			var el_h2_text = $(el_h2).html();
			var text_array = el_h2_text.split(' ');
			text_array[1] = '<span>' + text_array[1];
			text_array[text_array.length-1] = text_array[text_array.length-1] + '</span>';
			$(el_h2).html(text_array.join(' '));
		}
	
		var el_h2 = $('#location-container h2');
		if(el_h2.length > 0) {
			var el_h2_text = $(el_h2).html();
			var text_array = el_h2_text.split(' ');
			text_array[text_array.length-1] = '<span>' + text_array[text_array.length-1] + '</span>';
			$(el_h2).html(text_array.join(' '));	
		}
		
		var el_h2 = $('#hours-container h2');
		if(el_h2.length > 0) {
			var el_h2_text = $(el_h2).html();
			var text_array = el_h2_text.split(' ');
			text_array[text_array.length-1] = '<span>' + text_array[text_array.length-1] + '</span>';
			$(el_h2).html(text_array.join(' '));
		}
	
		var el_h2 = $('#twitter-container h2');
		if(el_h2.length > 0) {
			var el_h2_text = $(el_h2).html();
			var text_array = el_h2_text.split(' ');
			text_array[1] = '<span>' + text_array[1];
			text_array[text_array.length-1] = text_array[text_array.length-1] + '</span>';
			$(el_h2).html(text_array.join(' '));
		}
		
	} else {
		
		var el_h1 = $('#content h1').first();
		if(el_h1.length > 0) {
			
			var el_h1_text = $(el_h1).html();
			var text_array = el_h1_text.split(' ');
			
			if(text_array.length > 1) {
				if(text_array.length == 1) {
					text_array[text_array.length-1] = '<span>' + text_array[text_array.length-1] + '</span>';
					$(el_h1).html(text_array.join(' '));
				} else {
					var el_h1_text = $(el_h1).html();
					var text_array = el_h1_text.split(' ');
					text_array[1] = '<span>' + text_array[1];
					text_array[text_array.length-1] = text_array[text_array.length-1] + '</span>';
					$(el_h1).html(text_array.join(' '));					
				}
			}
		}		
	}
	


});


var navigation_handler = {
    items: [],
    
    setup: function()  {
        this.items = $('#menu-main-nav').children();

		$(this.items).first().addClass('first-child');
		$(this.items).last().addClass('last-child');
		
		$(this.items).map(function(i, el){
            var submenu = $('ul', el)[0];
            if(submenu) {
                $(el).addClass('has-submenu');
            }
        });  		

		$(this.items).bind('mouseover', function(event) {
            if($(event.currentTarget).hasClass('over')) return;
            
            navigation_handler.items.map(function(i, el){
                if($(event.currentTarget) != el) {
                    $(el).removeClass('over');
                }
            })

            var submenu = $('ul', event.currentTarget)[0];
            if(submenu) {
                $(event.currentTarget).addClass('over');

                    if(!ie8 && !ie7 && !ie6) {
						$(submenu).css({'opacity':'0'});
						$(submenu).animate({
						opacity: 1.0
						  }, 300, function() {
						});
					}
            }
        });        

        $('body').bind('mouseover', function(event) {
            if($(event.target).parents('#menu-header').length > 0) return;
            navigation_handler.items.map(function(i, el){
                
				if($(el).hasClass('over')) {
					$(el).removeClass('over');
				}
				
            })
			
        });

    }
}


var content_handler = {
	
    setup: function() {
        $(window).resize(function() {
            content_handler.content_resize();
        });
        this.content_resize();
    },
    
    content_resize: function() {

		$('#content-inner').first().css({'height': 'auto'});

		var dh = $(document).height();
		var ch = $('#content-inner').first().height();
		var lh = $('#layout').innerHeight();
		var wh = $(window).height();
	
		var content_inner_height = wh - parseInt($('#header').css('height')) - parseInt($('#footer').css('height')) - parseInt($('#home-slideshow-container').css('height')) - parseInt($('#content').css('paddingTop')) - parseInt($('#content').css('paddingBottom'));
	
		if(lh < wh) {
			$('#content-inner').first().css({'height' : content_inner_height}) 	
		}
    }
}

var slideshow_handler = {
    container: null,
	items: [],
    total_items: null,
    last_item: null,
    delay: 4000,
	animation: true,
	photos: [],
	to: null,
    
    setup: function()  {
		this.container = $('#home-slideshow');
		if(this.container.length == 0) return;
		
		this.photos = $('.photo', this.container);
		this.items = $('#slideshow-thumbs li');
		$(this.items).first().addClass('selected');
		
		this.total_items = this.items.length;
		
		this.last_item = 0;
		
		$('#slideshow-thumbs img').bind('click', function(event) {
			var parent_li = $(event.currentTarget).parents('li').first();
			if($(parent_li).hasClass('selected')) return;
			
			slideshow_handler.animation = false;
			clearTimeout(slideshow_handler.to);
			
			var current_index = $(slideshow_handler.items).index(parent_li);
			
			slideshow_handler.switchPic(current_index);
		});	

		this.to = setTimeout(function(){

			next_index = slideshow_handler.last_item + 1;
			if(next_index == slideshow_handler.total_items) {
				next_index = 0;
			}
			
			slideshow_handler.switchPic(next_index);
		}, slideshow_handler.delay);
		
		
    },
    
    switchPic: function(next_index) {
		
		$(slideshow_handler.items).removeClass('selected');
		$(slideshow_handler.items[next_index]).addClass('selected');

		var last_pic = $('#home-slideshow .active');
		$(last_pic).animate({
			queue: false,
			opacity: 0
		  }, 1000, function() {
			
		});
		setTimeout(function(){$(last_pic).removeClass('active')}, 1000);
	
		var next_pic = $('#home-slideshow .photo')[next_index];
		$(next_pic).css({'opacity':'0'});
		$(next_pic).addClass('active');
		$(next_pic).animate({
			queue: false,
			opacity: 1
		  }, 1000, function() {
			// Animation complete.
		});
	
		this.last_item = next_index;
		
		if(slideshow_handler.animation == true) {
			slideshow_handler.to = setTimeout(function(){
	
				next_index = slideshow_handler.last_item + 1;
				if(next_index == slideshow_handler.total_items) {
					next_index = 0;
				}
				
				slideshow_handler.switchPic(next_index);
			}, slideshow_handler.delay);
		}
		
    }	
}
