$(document).ready(function() {
	var isIE = $.browser.msie;
	
	if ( isIE && parseInt($.browser.version) <= 6 ) {// This is only for IE6
		if($('#old_content_container select').length > 0 && $.SelectBox){
			$('#old_content_container select').each(function(){
				$(this).selectbox(); // replace all selects using jquery.selectbox-0.6-beta1.js script
			});
		}
	}
	
	if($('#switchbox').length > 0){
		switchBoxNamespace.switchInit();
	}
	
	if($('#criteria-filter').length > 0){
		collapsableFilter()
	}
	
	if($('#half-w-boxes.votes-list').length > 0){
		voteRadioNamespace.voteRadios();
	}
	
	if($('#registerShortForm').length > 0){
		$('#registerShortForm input').each(function(){
			$(this).focus(function(){
				$('#exposeBox').expose({
					onClose: function(){
						$('#exposeBox').css('zIndex', '1');
					}
				});
			})
		})
	}
	
	if($('#sample-box .sample-product-on-groups-page').length > 0 && (!isIE)){ // not for IE
		var sampleProductBox = $('#sample-box .sample-product-on-groups-page');
		$(sampleProductBox).expose({
			onClose: function(){
				$('#exposeBox').css('zIndex', '1');
			}
		});
		$('.request-a-sample-button', sampleProductBox).click(function(){
			$('#exposeMask').css('display', 'none');
		});
		
	}

	if($('#dropdown-nav').length > 0){
		$('#products-intro .current-item').click(function(){
			dropdownNav($(this));
			return false;
		})
	}
	
	/*
	if($('#cl_filter_form').length > 0){
		clickFiter();
	}*/
	
	if($('#whats-widget').length > 0){
		widgetInfo();
	}
	
	/*
	if($('#hint-drop').length > 0){
		dropHint();
	}*/
	
	if($('a.join_group').length > 0){
		$('a.join_group').click(function() {
			// what is the ID of the clicked element?
			id = $(this).attr('id');

			// remove 'join_group_' prefix
			slug = id.substring(11);

			// send to server and get lightbox, etc
			ajax('Form_JoinGroup::process', 'slug=' + encodeURIComponent(slug));

			// done, don't activate the link
			return false;
		});
	}

	if($('a.leave_group').length > 0){
		$('a.leave_group').click(function() {
	 	    // what is the ID of the clicked element?
	 	    id = $(this).attr('id');

	 	    // remove 'leave_group_' prefix
	 	    slug = id.substring(12);

	 	    // send to server and get lightbox, etc
	 		ajax('Form_LeaveGroup::process', 'slug=' + encodeURIComponent(slug));

	 	    // done, don't activate the link
	 	    return false;
	 	});
	}
	
	if($('input.clearable').length > 0){
		$('input.clearable').each(function(i, val){
			$(this).focus(function(){
				if(this.title == this.value) this.value = "";
				if($(this).hasClass("inactive")) $(this).removeClass('inactive')
			})
			$(this).blur(function(){
				if(this.value == ""){
					this.value = this.title;
					if(!$(this).hasClass('inactive')) $(this).addClass('inactive')
				} 
			})
		})
	}
	
 });

 function dropdownNav(srcObj){
	var dropNav = $('#dropdown-nav');
	if($('#dropdown-nav:visible').length == 1){
		srcObj.removeClass('passive')
		dropNav.hide();
	}else{
		srcObj.addClass('passive')
		dropNav.show();
	}
}



var voteRadioNamespace = {
	allRadios: [],
	voteRadios: function(){
		voteRadioNamespace.allRadios = $('#half-w-boxes.votes-list input');
		voteRadioNamespace.clearAllRadio(voteRadioNamespace.allRadios);
		jQuery.each(voteRadioNamespace.allRadios, function(i, val){
			$(val).focus(function(i, val){ // all but safari
				voteRadioNamespace.addActiveRadio($(this));
			});
			$(val).change(function(i, val){ // safari
				voteRadioNamespace.addActiveRadio($(this));
			});

		});
	},
	addActiveRadio: function(srcObj){
		voteRadioNamespace.clearAllRadio(voteRadioNamespace.allRadios);
		$($(srcObj)[0].parentNode).addClass('active');
	},
	clearAllRadio: function(){
		jQuery.each(voteRadioNamespace.allRadios, function(i, val){
			$($(val)[0].parentNode).removeClass('active');
		});
	},
	validateVoteForm: function(){
		var invalidForm = true;
		jQuery.each(voteRadioNamespace.allRadios, function(i, val){
			if($(this).is(":checked")){
				invalidForm = false;
			}
		});
		if(invalidForm){
			alert('Please select a shelter!')
			return false;
		}
	}
}

var switchBoxNamespace = {
	AUTOSWITCHON: false,
	LASTACTIVESWITCH: {},
	ROTAIONDELAY: 2000,
	PAUSEDELAY: 4000,
	switchBoxTicker: function(){
		if(AUTOSWITCHON){
			if($('#switchbox .item.active').next().hasClass('item')){
				this.switchboxTo($('#switchbox .item.active').next()[0]);
			}else{
				this.switchboxTo($('#switchbox .item')[0]);
			}
		}
	},
	clearAllSwitchItems: function(){
		jQuery.each($('#switchbox .item'), function(i, val){
			$(val).removeClass('active');
		})
	},
	switchBoxClearThis: function(srcObj){
		if($(srcObj).hasClass('active')){
			switchBoxNamespace.LASTACTIVESWITCH = $(srcObj);
			$(srcObj).removeClass('active');
		}
	},
	switchboxTo: function(srcObj){
		if(!$(srcObj).hasClass('active')){
			var overlayImage = $('#overlay-image')[0];
			var overlayText = $('#overlay-text')[0];
			
			switchBoxNamespace.clearAllSwitchItems();
			$(srcObj).addClass('active');
			overlayImage.src = $('.overlay-image', srcObj)[0].src;
			overlayText.innerHTML = $('.overlay-text', srcObj)[0].innerHTML;
		}
	},
	switchInit: function(){
		AUTOSWITCHON = true;
		setInterval(function(){
			switchBoxNamespace.switchBoxTicker();
		}, this.ROTAIONDELAY)
		this.attachEventHandlers();
	},
	attachEventHandlers: function(){
		$('#switchitems').hover(function(){}, function(){
			if(switchBoxNamespace.LASTACTIVESWITCH != null){
				$(switchBoxNamespace.LASTACTIVESWITCH).addClass('active');
			}
		})
		
		$('#switchbox').hover(function(){}, function(){
			if(switchBoxNamespace.LASTACTIVESWITCH){
				setTimeout(function(){AUTOSWITCHON = true}, this.PAUSEDELAY)
			}
		})
		
		jQuery.each($('#switchbox .item'), function(i, val){
			$(val).hover(function(){
					AUTOSWITCHON = false;
					switchBoxNamespace.switchboxTo($(val));
				}, function(){
					switchBoxNamespace.switchBoxClearThis($(val));
				}	
			)
			if(i == 0) $(val).addClass('active');
		});
	}
}

function collapsableFilter(){
	var effectTime = 300;
	var last_height = $("#criteria-filter .extandable-cont .extandable-el").height();
	$("#criteria-filter").removeClass("collapsed");
	
	var new_height = 0;
	$("#criteria-filter .extandable-cont .extandable-el").each(function(){
		if(new_height < $(this).height()) new_height = $(this).height();
	});
	
	$("#criteria-filter").addClass("collapsed");
	
	$(".collapsed .extandable-cont .collapsable-cont").append("<div class='expand'>More...</div>");
	$(".collapsed .extandable-cont .collapsable-cont").append("<div class='contract'>Less...</div>");
	$("div.contract").hide();
	
	$('.expandTrigger').each(function(){
		$(this).focus(function(){
			if($("#criteria-filter").hasClass('collapsed')){
				expandCollapsableFilter(effectTime, last_height, new_height)
			}
		})
	})
	
	$(".expand").each(function(){
		$(this).click(function(){
			expandCollapsableFilter(effectTime, last_height, new_height)
		});
	});
	$(".contract").each(function(){
		$(this).click(function(){
			contractCollapsableFilter(effectTime, last_height, new_height)
		});
	});
}

function expandCollapsableFilter(effectTime, last_height, new_height){
	 $(".expand").each(function(){
	 	$(this).hide();
	 });
	$("#criteria-filter .extandable-cont .extandable-el").animate({height:new_height}, effectTime );
	$("#criteria-filter .extandable-cont").addClass('expanded'); //ie6fix
	$("#criteria-filter").removeClass('collapsed');
	$("div.collapsable-cont div.contract").show(effectTime);
}

function contractCollapsableFilter(effectTime, last_height, new_height){
	 $(".contract").each(function(){
	 	$(this).hide();
	 });
	$("#criteria-filter .extandable-cont .extandable-el").animate({height:last_height}, effectTime )
	$("#criteria-filter").addClass('collapsed');
	$("#criteria-filter .extandable-cont").removeClass('expanded'); //ie6fix
	$("div.collapsable-cont div.expand").show(effectTime);
}

function clickFiter(){
	$('.container .container-body', '#cl_filter_form').each(function(){
		$(this).hide()
		$(this).addClass('container-hidden');
	})
	$('.heading', '#cl_filter_form').each(function(){
		$(this).click(function(){
			var thisContainer = $(this).parents(".container").get(0);
			var containerBody = $('.container-body', thisContainer) 
			var expandLink = $('.expand-link', thisContainer)
			
			if(containerBody.hasClass('container-hidden')){
				expandLink.html('click here to collapse')
				containerBody.slideDown(300);
				containerBody.removeClass('container-hidden');
			}else{
				expandLink.html('click here to expand')
				containerBody.slideUp(300);
				containerBody.addClass('container-hidden');
			}
		})
	})
}

function widgetInfo(){
	var widgetHint = $('#whats-widget');
	widgetHint.hide();
	$('#whats-widget-button').click(function(){
		if($('#whats-widget:visible').length == 0){
			widgetHint.fadeIn(500);
		}else{
			widgetHint.fadeOut(100);	
		}
		
	})
	widgetHint.click(function(){
		widgetHint.fadeOut(100);
	})
}

var dropEffectOn = false;
var overContainer = false;
function dropHint(){
	$('#hint-drop').hover(
		function(){
			overContainer = true;
		},
		function(){
			overContainer = false;
			var t = setTimeout(function(){
				if(!overContainer){					
					$('#hint-drop .h-wrpr').animate({height: "29px"})
				}
			}, 1000)
		}		
	)
	
	$('#hint-drop h3').mouseover(function(){
		$('#hint-drop .h-wrpr').animate({height: "136px"})
	})
}

function doSearch(title, category){
	var searchForm = $('#header_nav_container form');
	var searchInput = $('#header_nav_container form #search-box')[0];
	var searchAllCheck = $('#header_nav_container form input[type=radio]');
	var searchHint = $('#searchform-hint')[0];
	
	$.scrollTo(searchForm, 1500, {onAfter: function(){ // scroll to search form
		searchInput.value = ''; // clear input
		$(searchHint).show(); // show tooltip
		setTimeout(function(){
			$(searchInput).css({border: "1px solid #0885F9"}) // highlight input
			for(var wCount = 0; wCount <= title.length; wCount++){; // fill input field
				var typeSearch = function(c){
								return function(){ 
									searchInput.value += title.charAt(c);
									if(c == title.length){ // on input fill complete
										searchInput.focus(); // focus on input - open categories
										selectCat();
										$(searchInput).css({border: "1px solid #C7C7C7"});
									}
								} 
							}(wCount)
							
				var typeTimeout = window.setTimeout(typeSearch, 200 * wCount);
				
				var selectCat = function(){// select radio button
					setTimeout(function(){
						searchAllCheck.each(function(){
							if($(this).get(0).value == category){
								$(this).focus();
								$(this).get(0).checked = true;
							}
						});
						
						setTimeout(searchForm.submit(), 1000); // submit form
						
					}, 1000)
				}	
			}
		}, 1000)
	}});
}

function selectFunTab(activeTab){
	$('#petfun-box li').removeClass('selected');
	$(activeTab.parentNode.parentNode).addClass('selected');
	$('#petfun-box .tabbed_content').hide();
	$("#"+[activeTab.rel]).show();
}

function selectMediaTab(activeTab){
	$('#media-box .tab-list li').removeClass('selected');
	$("#"+[activeTab.rel]+"_tab").addClass('selected');
	$('#media-box .tabbed_content').hide();
	$("#"+[activeTab.rel]).show();
	if(activeTab.rel != 'mediaBoxPhotos'){
		$('#photo-count').hide()
	}else{
		$('#photo-count').show()
	}
}

function photoNavBox(direction){
	var photoLine = $('#photoline-cont .photo-line-wrapper');
	var offset = parseInt(photoLine.css('left'));
	var totalLength = photoLine.width();
	var STEP = 187;
	var currentPhotoCount = $('#current_photo');
	if(direction == 'next'){
		if((offset - STEP) > (totalLength*-1)){
			offset -= STEP;
			currentPhotoCount.html(parseInt(currentPhotoCount.html())+1);
		}
	}else{
		if((offset + STEP) <= 0){
			offset += STEP;
			currentPhotoCount.html(parseInt(currentPhotoCount.html())-1);
		}
	}
	photoLine.css('left', offset+'px');
}