function UI() {
	
	var self = this;
	var interval_id;
	
    var init = function() {
        formatHeadlines($('h1'));
        formatPullquotes($('.pullquote'));
        formatNewsItems($('.news_item'));
		if ($('#home_dynamic').length > 0) {
			initializeAccordion($('#home_dynamic'));
		}
        initializeNavigation($('#nav'));
		initializeProducts($('.product'));
		initializeNews();
		$('#search_btn').click(function(e) {
			$('#search_form').submit();
		});
		$('#query').keypress(function(e) {
			if (e.which == 13) {
				$('#search_form').submit();
			}
		})
		$('.QapTcha').QapTcha();
		$('.mini_gallery a').lightBox();
		$('.map').hover(function(e) {
			$(this).attr('src', "images/map_over.gif");
		}, function(e) {
			$(this).attr('src', "images/map.gif");
		}).click(function(e) {
			window.open('http://maps.google.com/maps?q=121+Kiwi+Drive+Waynesburg,+PA+15370&hl=en&ll=39.899202,-80.131209&spn=0.014453,0.027874&sll=37.0625,-95.677068&sspn=60.116586,114.169922&vpsrc=6&hnear=121+Kiwi+Rd,+Waynesburg,+Pennsylvania+15370&t=h&z=16&iwloc=A');
			return false;
		});
		$('#contact-form #button').click(function(e) {
			
			var fields = new Array('#name', '#email', '#subject', '#message');
			errors = false;
			for (var i = 0; i < fields.length; i++) {
				if ($.trim($(fields[i]).val()) === "") {
					errors = true;
					$(fields[i]).parent().parent().addClass('error');
				} else {
					$(fields[i]).parent().parent().removeClass('error');
				}
			}
			
			//console.log(errors);
			if (errors) {
				$('#error').show();
			} else {
				$('#contact-form').submit();
			}

		});
		$('.careers').click(function(e) {
			location.href='mailto:rachel@producerssupplyco.com';
		})
    }

	var initializeNews = function() {
		$('.news_archives').treeview({'collapsed':true});
		$('.year-hitarea').click();
		$('.clickable_thumb').lightBox();
	}

	var initializeProducts = function(prods) {
		prods.click(function(e) {
			window.location=$(this).attr('linkto');
		})
	}
    
    var formatHeadlines = function(hls) {
        hls.each(function() {
            var new_hl = $(this).clone();
            var outer = $('<div class="outer_hl" />').append(new_hl);
			if (new_hl.hasClass('product_header')) {
				outer.addClass('product_header');
				new_hl.removeClass('product_header');
			}
            $(this).before(outer).remove();
        });
    }
    
    var formatPullquotes = function(pls) {
        pls.each(function() {
            $(this).find('.pl_head')
                .before($('<div class="pl_corner"><img src="images/pullquote_corner.gif" /></div>'))
                .after('<div style="clear:both;" /><div class="pl_left" />');
        });
    }
    
    var formatNewsItems = function(nis) {
        nis.each(function() {
            var new_ni = $(this).clone();
            new_ni.find('div').remove();
            new_ni.append('<div class="news_left" />').append('<div class="news_right" />');
            new_ni.find('.news_left').append('<img src="images/news_arrow.gif" />');
            new_ni.find('.news_right')
                .append($(this).find('.date'))
                .append($(this).find('.headline'))
                .append("<a href='" + $(this).attr("linkto") + "'>>> Read More</a>");
            $(this).before(new_ni).after('<div style="clear:both;" />').remove();
        });
    }

	var initializeAccordion = function(el) {
		el.find('#tab_1_content').addClass('current');
		el.find('#tab_2_content').css('width', 0);
		el.find('#products_nav').hide();
		el.find('#tab_3_content').css('width', 0);
		el.find('.tab').click(function(e) {		
			if (!$('#' + $(this).attr('id') + '_content').hasClass('current')) {
				switchTo($(this).attr('id'));
			}
		});
		
		el.find('#tab_1_content').click(function() {
			location.href='our_people.php';
		});
		
		el.find('#tab_2_content').click(function() {
			location.href='svc_solids.php';
		});
		
		interval_id = setInterval(nextAccItem, 5000);
	}
    
	var switchTo = function(id) {
		var old_num = null;
		var new_num = null;
		var img_string;
		$('.current').each(function(e) {
			old_num = $(this).attr('id').charAt(4);
		});
		new_num = $('#' + id).attr('id').charAt(4);
		if (new_num > old_num) {
			img_string = "images/right_arrows.png";
		} else {
			img_string = "images/left_arrows.png";
		}
		$('#tab_' + old_num + ' img').attr('src', img_string);
		if ($('.current').attr('id') == 'tab_3_content') {
			$('#products_nav').hide();
		}
		$('.current').stop().animate({'width':0}, 300).removeClass('current');			
		$('#' + id + '_content').animate({'width':546}, 300).addClass('current');
		if ($('#' + id + '_content').attr('id') == 'tab_3_content') {
			$('#products_nav').show();
		}
		$('#' + id).find('img').attr('src', 'images/right_arrows.png');
		clearInterval(interval_id);
		interval_id = setInterval(nextAccItem, 5000);
	}
	
	var nextAccItem = function() {
		var current = $('.current');
		if (current.length === 0) {
			currentNum = 0;
		} else {
			var currentString = $('.current').attr('id').substr(4, 1);
			currentNum = parseInt(currentString);
		}

		nextNum = currentNum + 1;
		if (nextNum > 3) { nextNum = 1; }
		switchTo('tab_' + nextNum);
	}

    var initializeNavigation = function(nav) {
        nav.find('li').hover(function(e) {
            $(this).addClass('over');
            $(this).find('ul').show();
            if ($(this).find('ul').length > 0) {
                $(this).find('a').addClass('extended');
            }
        }, function(e) {
            $(this).removeClass('over');
            $(this).find('ul').hide();
            $(this).find('a').removeClass('extended');
        });
        nav.find('li ul li').hover(function(e) {
            $(this).addClass('subover');
        }, function(e) {
            $(this).removeClass('subover');
        }).parent().parent().append($('<img />', {'class':'nav_arrow', 'src':'images/arrow.png'}));
    }
    
    return {
        //Public methods
		init : init,
		nextAccItem : nextAccItem,
		switchTo : switchTo
    };
}

var ui = new UI();
$(document).ready(ui.init);
