$(document).ready(function() {
	if($('#url')) { $('#url').parents('p').hide(); }

	//ipad and iphone fix
	if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {
		$("#menu li.page_item").click(function(){
			$($(this).find('ul').get(0)).show();
		});
	}

	$('#logo').hover(function() { $(this).animate({ opacity: 0.5 }); }, function() { $(this).animate({ opacity: 1 }); });
	$('.current_page_item').parent('li').find('span').css({ "color": "#51CB4E" });
	$($($('.current_page_item').get(0)).parents('li').find('span').get(0)).css({ "color": "#51CB4E" });


	/* Home */
	$(window).load(function() {
		if($('#slider')) { $('#slider').nivoSlider({ pauseTime:5000, pauseOnHover:false }); }
	});


	/* FAQ */
	if($('#accord')) { 
		$('#accord div strong').click(function() {
			var active = $(this);
			$(active).toggleClass('bold');
			$(this).parent().find('div').slideToggle();
		});
	}


	/* Glossary */
	if($("#content-slider")) {
		$("#content-slider").slider({ animate: true, change: handleSliderChange, slide: handleSliderSlide });

		$('.showDef').click(function(){
			if($('#def_container').attr('rel') != $(this).html()){
				var word = $(this).html();
				var def = $(this).attr('def');
				var put = '<div><strong>' + word + '</strong><br />' + def + '<br /><br /><a class="closeThis">close</a></div>';

				if($(this).css('display') == 'none'){ $('#def_container').attr('rel', word).html(put).slideDown('slow'); }
				else { $('#def_container').slideUp('fast', function(){ $(this).delay(300).attr('rel', word).html(put).slideDown('slow'); }); }
			}
		});

		$('.closeThis').live('click', function(){ $('#def_container').attr('rel', '').slideUp('fast'); });
	}

	function handleSliderChange(e, ui) { var maxScroll = $("#content-scroll").attr("scrollWidth") - $("#content-scroll").width(); $("#content-scroll").animate({scrollLeft: ui.value * (maxScroll / 100) }, 1000); }
	function handleSliderSlide(e, ui) { var maxScroll = $("#content-scroll").attr("scrollWidth") - $("#content-scroll").width(); $("#content-scroll").attr({scrollLeft: ui.value * (maxScroll / 100) }); }

	/* Sales & Info State */
	if($('#state')) { 
		$('#state').change(function() {
			$('#state_info').html('<img style="display: block; margin: 0 auto; width: 16px" src="' + themeurl + '/images/loading.gif" />');
			getState($(this).val());
		});
	}
	function getState(state) {
		$.ajax({
			type: "POST",
			url: serverurl + "/ajax-state",
			data: 'state=' + state,
			dataType: 'application/json',
			async: false,
			success: function(msg, status){
				msg = $.parseJSON(msg);
				var sales_html = '';
				if(parseInt(msg.state_size) === 0) {
					sales_html = 'There are no licensees in your state. Please contact the <a class="green" href="' + serverurl + '/licensing-opportunitie">licensee</a> closest to you or contact <a class="green" href="' + serverurl + '/contact-us">Floating Island International</a> and we are happy to assist you.';
				} else {
					for(var i = 0; i < parseInt(msg.state_size); i++) {
						sales_html += '<table class="sales_table">' + 
											'<tr><td class="first">Name</td><td class="last">' + msg.state[i].name + '</td></tr>';
						if(msg.state[i].address) { sales_html += '<tr><td class="first">Address</td><td class="last">' + msg.state[i].address + '</td></tr>'; }
						sales_html += '<tr><td class="first">Phone</td><td class="last">' + msg.state[i].phone + '</td></tr>' + 
											'<tr><td class="first">Email</td><td class="last"><a href="mailto:' + msg.state[i].email + '">' + msg.state[i].email + '</a></td></tr>';
						if(msg.state[i].web) { sales_html += '<tr><td class="first">Web</td><td class="last"><a href="http://' + msg.state[i].web + '" target="_blank">' + msg.state[i].web + '</a></td></tr>'; }
						sales_html += '</table>';
					}
				}

				$('#state_info').html(sales_html)
			}
		});
	}
});

