﻿jQuery(document).ready(function($) {
	// Add Rounded Corners and Pointer to Dropdown Menu
	var templateURL = $(".ot_template_url").text();
	$("#topNav > li > ul").prepend('<li class="topNavPointer"><img src="' + templateURL + '/images/top-nav-rollover-pointer.png" width="18" height="16" alt="" /></li><li class="noText"><img src="' + templateURL + '/images/top-nav-dropdown-tl.png" width="10" height="9" alt="" /><div class="dropDownBackgroundEdge"></div><img src="' + templateURL + '/images/top-nav-dropdown-tr.png" width="10" height="9" alt="" /></li>').append('<li class="noText"><img src="' + templateURL + '/images/top-nav-dropdown-bl.png" width="10" height="9" alt="" /><div class="dropDownBackgroundEdge"></div><img src="' + templateURL + '/images/top-nav-dropdown-br.png" width="10" height="9" alt="" /></li>');
	$("#topNav > li > ul > li > ul").prepend('<li class="noText"><img src="' + templateURL + '/images/top-nav-dropdown-tl.png" width="10" height="9" alt="" /><div class="dropDownBackgroundEdge"></div><img src="' + templateURL + '/images/top-nav-dropdown-tr.png" width="10" height="9" alt="" /></li>').append('<li class="noText"><img src="' + templateURL + '/images/top-nav-dropdown-bl.png" width="10" height="9" alt="" /><div class="dropDownBackgroundEdge"></div><img src="' + templateURL + '/images/top-nav-dropdown-br.png" width="10" height="9" alt="" /></li>');
	$("#topNav > li > ul > li:first-child, #topNav > li  > ul > li:last-child").css({ 'float' : 'none' });
	
	// First Child Dropdown
	$("#topNav > li").hover(function(){
		$(this).children("ul").show();
		$(this).children("a").addClass("blueLink");
	}, function(){
		$(this).children("ul").hide();
		$(this).children("a").removeClass("blueLink");
	});
	
	// Second Child Dropdown
	$("#topNav > li > ul > li").hover(function(){
		$(this).children("ul").show();
		$(this).children("a").addClass("anchorHover");
	}, function(){
		$(this).children("ul").hide();
		$(this).children("a").removeClass("anchorHover");
	});
	
	// Physicians Menu Box
	var columns = 3;
	var physicians = $(".menu-item-14 > ul > li").length - 3;
	var remainder = physicians % columns;
	var columnWidth = columns * 200;
	var otherWidth = columnWidth - 20;
	if(remainder != 0){	var extraColumns = columns - remainder; }
	if(extraColumns) {
		var myHtml = '';
		for(i = 0; i < extraColumns; i++) {
			myHtml += '<li><a href="#" class="empty">&nbsp;</a></li>';
		}
	}
	$(".menu-item-14 > ul").css({ 'width' : columnWidth + 'px'});
	$(".menu-item-14 > ul > li .dropDownBackgroundEdge").css({ 'width' : otherWidth + 'px' });
	if(extraColumns) {
		$(".menu-item-14 > ul > li:last-child").before(myHtml);
	}
	$(".menu-item-14 > ul > li > a").css({ 'margin' : 0 });
	
	// Main Page Rotator
	if($("#rotatorContent").is(':visible')) {
		$('#rotatorContent').cycle({ 
			pager: '#rotatorNavigation', 
			pagerAnchorBuilder: function(idx, slide) { return '<li><a href="#"></a></li>'; }, 
			pause: 1
		});
	}
	
	// Rotator Right Side Panel Icon Hover
	$("#rotatorPanel li").hover(function(){
		$(this).find(".rotatorPanelIconActive").show().find(".rotatorPanelIconInactive").hide();
	}, function(){
		$(this).find(".rotatorPanelIconActive").hide().find(".rotatorPanelIconInactive").show();
	});
	
	// What's New Arrow Functionality
	$("#whatsNewArrows, #sidebarWhatsNewArrows").find("a").eq(0).click(function(){
		var currentWhatsNew = $("#whatsNewCycleContainer > div:visible");
		if(currentWhatsNew.prev().length) { 
			currentWhatsNew.hide().prev().fadeIn();
		} else {
			currentWhatsNew.hide();
			$("#whatsNewCycleContainer > div:last-child").fadeIn();
		}
		return false;
	});
	$("#whatsNewArrows, #sidebarWhatsNewArrows").find("a").eq(1).click(function(){
		var currentWhatsNew = $("#whatsNewCycleContainer > div:visible");
		if(currentWhatsNew.next().length) { 
			currentWhatsNew.hide().next().fadeIn();
		} else {
			currentWhatsNew.hide();
			$("#whatsNewCycleContainer > div:first-child").fadeIn();
		}
		return false;
	});
	
	// FAQ Arrow Functionality
	$("#faqCycleContainer > div").eq(Math.random() * $("#faqCycleContainer > div").length).show();
	$("#faqArrows").find("a").eq(0).click(function(){
		var currentfaq = $("#faqCycleContainer > div:visible");
		if(currentfaq.prev().length) { 
			currentfaq.hide().prev().fadeIn();
		} else {
			currentfaq.hide();
			$("#faqCycleContainer > div:last-child").fadeIn();
		}
		return false;
	});
	$("#faqArrows").find("a").eq(1).click(function(){
		var currentfaq = $("#faqCycleContainer > div:visible");
		if(currentfaq.next().length) { 
			currentfaq.hide().next().fadeIn();
		} else {
			currentfaq.hide();
			$("#faqCycleContainer > div:first-child").fadeIn();
		}
		return false;
	});
	
	// Add Current Page Class to Active Menu Item for CPT
	if($(".cptSubMenu").is(":visible")) {
		var currentPage = $(".ot_post_id").text();
		$(".cptSubMenu").find(".page-item-" + currentPage).addClass("current_page_item");
	}
	
	// Locations Menu
	$(".locationsMenu > li > a").click(function(){
		var eleIndex = $(this).parent().index();
		$(this).parent().parent().find(".active").removeClass("active");
		$(this).addClass("active");
		$(".locationsContent > div").hide();
		$(".locationsContent > div").eq(eleIndex).show();
		
		var blogURL = $(".ot_url").text();
		var postID = $(this).attr("rel");
		if($(".locationsContent > div").eq(eleIndex).find(".locationMap").html() == '') {
			$.ajax({
				url: blogURL + '/wp-admin/admin-ajax.php',
				type: 'POST', 
				dataType: 'json', 
				data: { 'action' : 'wosm_maps', 'post_id' : postID },
				success: function(data) {
					$(".locationsContent > div").eq(eleIndex).find(".locationMap").html(data.iframe);
				}
			}); // end AJAX request
		} // end if statement
		return false;
	});
	
	// Forms
	$(".form input[type='text'], .form textarea").focus(function(){
		$(this).val('').css({ 'color' : '#333' });
	});
	
	// Footer Search Text
	$("#footerSearch #s").focus(function(){
		$(this).val('');
	});
	
	// Main Page Videos
	$(".specialityVideoList li:nth-child(3n)").css({ 'padding-right' : 0 });
	//console.log($(".specialityVideoList li:nth-child(3n)"));
});
