var	fxAttrs = [ "height", "marginTop", "marginBottom", "paddingTop", "paddingBottom", "opacity" ];
function genFx( type ){
	var obj = {};
	jQuery.each(fxAttrs, function(){
		obj[ this ] = type;
	});
	return obj;
}

var Site = {
    navigationFade: function() {
		nav = $('#navigation').addClass('js-nav');
		nav.find('a').wrap('<span></span>');
		$('#navigation ul li a').show();
		nav.find('a:not(.active)').parent().css('opacity', 0);
		nav.find('a:not(.active)').parent().hover(function(){
			$(this).animate({opacity: 1}, 200); }, function(){ $(this).animate({opacity: 0}, 200);
		});
	},
	shortlist: {
	    currShortlistItem: null,
	    animationTime: 400,
		viewShortlist: function() {
			if ((link = $('.shortlist')).length > 0) {
				var hidden = false;

				link.click(function() {
					if (hidden) {
						$(this).next().animate(genFx("show"), Site.shortlist.animationTime);
						hidden = false;
					} else {
						$(this).next().animate(genFx("hide"), Site.shortlist.animationTime);
						hidden = true;
					}
				});

				link.trigger('click');
			}
		},
		addToShortlist: function(eid, uid, webAddress, title, town, state, callback) {
			Site.shortlist.currShortlistItem = null;
			Site.shortlist.currShortlistItem = {
	            "ID" : eid,
	            "Url" : webAddress,
	            "Title" : title,
	            "Town" : town,
	            "State" : state
	            }
			WebUtils.AddToShortlist(eid, uid, webAddress, title, town, state, callback);
		},
		removeFromShortlist: function(eid, uid, callback) {
			WebUtils.RemoveFromShortlist(eid, uid, callback);
		},
		shortlistAjax_AddCallback: function(id) {
		    Site.shortlist.onAdded(id.value);
			$('.' + id.value).find('li.add-shortlist').animate(genFx("hide"), Site.shortlist.animationTime);
			$('.' + id.value).find('li.remove-shortlist').animate(genFx("show"), Site.shortlist.animationTime);
		},
		shortlistAjax_RemoveCallback: function(id) {
		    Site.shortlist.onRemoved(id.value);
			$('.' + id.value).find('li.add-shortlist').animate(genFx("show"), Site.shortlist.animationTime);
			$('.' + id.value).find('li.remove-shortlist').animate(genFx("hide"), Site.shortlist.animationTime);

		},
        shortlistAjax_AddProfileCallback: function(id) {
            Site.shortlist.onAdded(id.value);
            $(".tools .hlAddToShortlist").fadeOut(Site.shortlist.animationTime, function() {
                $(".tools .hlRemoveFromShortlist").fadeIn(Site.shortlist.animationTime);
            });
        },
        shortlistAjax_RemoveProfileCallback: function(id) {
            Site.shortlist.onRemoved(id.value);
            $(".tools .hlRemoveFromShortlist").fadeOut(Site.shortlist.animationTime, function() {
                $(".tools .hlAddToShortlist").fadeIn(Site.shortlist.animationTime);
            });
        },
		onAdded: function(id) {
		    if ($(".utility div.user-nav ul li.sideList").css("display") == "none") {
		        $(".utility div.user-nav ul li.sideList").slideDown(Site.shortlist.animationTime, function() {
		            Site.shortlist.addToSideBar(id);
		        });
		    } else {
		        Site.shortlist.addToSideBar(id);
		    }
		},
		onRemoved: function(id) {
		    Site.shortlist.removeFromSideBar(id);
		},
        addToSideBar: function(id) {
		    var isAdded = false;
		    $(".utility div.user-nav ul li.sideList dt").each(function() {
		        if ($(this).attr("class") == 'sideItem-' + id) {
		            isAdded = true;
		        }
		    });
		    if (isAdded == false) {
		        $("<a/>")
		            .text(Site.shortlist.currShortlistItem.Title)
		            .attr("href", Site.shortlist.currShortlistItem.Url)
		            .appendTo($("<dt/>")
	                    .attr("class","sideItem-" + Site.shortlist.currShortlistItem.ID)
	                    .css({display:"none"})
	                    .appendTo($(".utility div.user-nav ul li.sideList dl")));
		        $("<dd/>")
		            .attr("class","sideItem-" + Site.shortlist.currShortlistItem.ID)
		            .text(Site.shortlist.currShortlistItem.Town != '' ? Site.shortlist.currShortlistItem.Town + ', ' + Site.shortlist.currShortlistItem.State : Site.shortlist.currShortlistItem.State)
		            .css({display:"none"})
		            .appendTo($(".utility div.user-nav ul li.sideList dl"));
		        $(".utility div.user-nav ul li.sideList dl .sideItem-" + Site.shortlist.currShortlistItem.ID).slideDown(Site.shortlist.animationTime);
		    }
		},
		removeFromSideBar: function(id) {
		    var isFound = false;
		    $(".utility div.user-nav ul li.sideList dt").each(function() {
		        if ($(this).attr("class") == "sideItem-" + id) {
		            isFound = true;
		        }
		    });
		    if (isFound == true) {
		        $(".utility div.user-nav ul li.sideList .sideItem-" + id).slideUp(Site.shortlist.animationTime, function() {
		            $(".utility div.user-nav ul li.sideList .sideItem-" + id).remove();
		            if ($(".utility div.user-nav ul li.sideList dt").length == 0) {
		                $(".utility div.user-nav ul li.sideList").slideUp(Site.shortlist.animationTime);
		            }
		        });
		    }
		},
		init: function() {
			Site.shortlist.viewShortlist();
		}
	},
	landing: {
		browseAnimalType: function() {
			$('.quick-browse .animal-type label').hide();
			$('.quick-browse .animal-type div').show();
			$('.quick-browse-' + $('select.quick-browse-type.').val() + ' a').addClass('active');
			$('.quick-browse a').click(function(e) {
				e.preventDefault();
				animal = $(this).parent().attr('class').substring(13);
				$('select.quick-browse-type').val(animal);
				$(this).closest('ul').find('a').removeClass('active')
				$(this).addClass('active');
			});
		},
		findAnimalType: function() {
			function selectIcon(label) {
				$(label).closest('ul').find('label').removeClass('active')
				$(label).addClass('active');
				if ($(label).closest("ol:has(.find-stock-breed)").length > 0) {
					if ($(label).parent().length > 0) {
						var animalType = $(label).parent().attr('class').substring(11);
						Site.search.getAnimalBreeds(animalType, false, Site.search.getAnimalBreeds_Callback);
					}
				}
			}
			$('.find-stock-type label').click(function(e) {
			    if ($(this).attr('class') != 'active') {
				    selectIcon(this);
				}
			});
			selectIcon($($('.find-stock-type input:checked')[0]).closest('label'));
		},
		init: function() {
			if ($('body.landing').length > 0) {
				Site.landing.browseAnimalType();
				Site.landing.findAnimalType();
			}
		}
	},
	// this for landing search forms as well
	search: {
	    forUserProfileType: false,
		animalType: function() {
			if ($(".forUserProfileType").length > 0) {
			    Site.search.forUserProfileType = true;
			}
			function selectIcon(radio) {
				if (radio) {
					animal = $(radio).attr('id').substring(2);
					$('.animal-icon a').each(function() {
						selected = $(this).attr('class').substring(7) == animal ? true : false;
						$(this).parent().toggleClass('selected', selected);
					});
				}
			}
			$('.animal-icon a').click(function(e) {
				e.preventDefault();
				if ($(this).parent().attr('class') != 'selected') {
				    animal = $(this).attr('class').substring(7);
				    $(this).closest('ul').find('li').removeClass('selected')
				    $(this).parent().addClass('selected');
				    $('.animal-radio #r-'+ animal).attr('checked', true).focus();
				    Site.search.getAnimalBreeds(animal, Site.search.forUserProfileType, Site.search.getSearchAnimalBreeds_Callback);
				}
			});

			$('.animal-radio label').click(function() {
			    if ($('input:checked', this).length < 1) {
				    selectIcon($(this).find('input'));
                    Site.search.getAnimalBreeds(animal, Site.search.forUserProfileType, Site.search.getSearchAnimalBreeds_Callback);
		        }
			});
		},
		advancedExpand: function() {
			var link = $('.show-hide');
			var fieldset = link.closest('h3').next();
			var hidden = false;
			if ($(fieldset).css("display") == "none") {
			    hidden = true;
			    link.addClass('show').text('show');
			}
			link.click(function(e) {
				e.preventDefault();
				if (hidden) {
					fieldset.animate(genFx("show"), 500);
					hidden = false;
					link.removeClass('show').text('show');
				} else {
					fieldset.animate(genFx("hide"), 500);
					hidden = true;
					link.addClass('show').text('hide');
				}
			});
		},
		findUserType: null,
		findUser: function() {
		    $("body.landing-breeders .findUser .ddlAnimal").change(function(e) {
		        Site.search.findUserType = $(this).attr('class').substring(10);
                WebUtils.GetAnimalBreeds($(this).val(), true, Site.search.getFindUserAnimalBreeds_Callback);
            });
		},
		getAnimalBreeds: function(animal, forTypeUserProfile, callback) {
			WebUtils.GetAnimalBreeds(animal, forTypeUserProfile, callback);
		},
		getAnimalBreeds_Callback: function(data) {
			$('.find-stock-breed select').find('option').remove().end().append('<option value="">all</option>');
			$.each(data.value.split(","), function(key, val) {
				$('.find-stock-breed select').append($("<option></option>").attr("value",val).text(val));
			});
		},
		getSearchAnimalBreeds_Callback: function(data) {
		    $('.f-search .regular-form .breed select').find('option').remove().end().append('<option value="">all</option>');
			$.each(data.value.split(","), function(key, val) {
				$('.f-search .regular-form .breed select').append($("<option></option>").attr("value",val).text(val));
			});
		},
		getFindUserAnimalBreeds_Callback: function(data) {
		    $("body.landing-breeders .findUser #ddlBreed" + Site.search.findUserType).find('option').remove().end().append('<option value="">all</option>');
			$.each(data.value.split(","), function(key, val) {
				$("body.landing-breeders .findUser #ddlBreed" + Site.search.findUserType).append($("<option></option>").attr("value",val).text(val));
			});
		},
		init: function() {
			if ($('body.search').length > 0) {
				this.animalType();
				this.advancedExpand();
		    } else if ($('body.landing-sell').length > 0) {
		        $('.form-create-classified select.choose-animal').change(function() {
			        Site.search.getAnimalBreeds(this.value, false, Site.search.getAnimalBreeds_Callback);
		        });
		    } else if ($('body.landing-breeders').length > 0) {
		        this.findUser();
		    }
		}
	},
	classified: {
		expandContact: function() {
			var hidden = false;
			var link = $('.quick-facts .contact-details a');
			var di = $('.quick-facts .contact')
			function hide() {
				di.animate(genFx("hide"), 500);
				hidden = true;
			}
			function show() {
				di.animate(genFx("show"), 500);
				hidden = false;
			}

			link.click(function(e) {
				e.preventDefault();
				if (hidden) {
					show();
					link.removeClass('show').text('Hide Contact Details');
					if ($(this).attr('active') == 'true') {
					    pageTracker._trackPageview(pageTrackViewURL);
					    $(this).attr('active', 'false');
					}
				} else {
					hide();
					link.addClass('show').text('View Contact Details');
				}
			});

			hide();
		},
		init: function() {
			if ($('body.classified').length > 0) {
				this.expandContact();
			}
		}
	},
	profile: {
		expandContact: function() {
			var hidden = false;
			var link = $('.quick-facts .contact-details a');
			var di = $('.quick-facts .contact')
			function hide() {
				di.animate(genFx("hide"), 500);
				hidden = true;
			}
			function show() {
				di.animate(genFx("show"), 500);
				hidden = false;
			}

			link.click(function(e) {
				e.preventDefault();
				if (hidden) {
					show();
					link.removeClass('show').text('Hide Contact Details');
					if ($(this).attr('active') == 'true') {
					    pageTracker._trackPageview(pageTrackViewURL);
					    $(this).attr('active', 'false');
					}
				} else {
					hide();
					link.addClass('show').text('View Contact Details');
				}
			});

			hide();
		},
		init: function() {
			if ($('body.profile').length > 0) {
				this.expandContact();
			}
		}
	},
	regions: {
	    defaultOptionText: null,
	    currStateRegionForm: null,
	    getByState_callback: function(regions) {
	        if (regions.value && Site.regions.currStateRegionForm != null) {
                $(".ddlRegion option", Site.regions.currStateRegionForm).remove();
                if (regions.value.length > 1) {
                    $("<option/>")
                        .text(Site.regions.defaultOptionText)
                        .attr("value", "")
                        .appendTo($(".ddlRegion", Site.regions.currStateRegionForm));
                }
                for (var i = 0; i < regions.value.length; i++) {
                    $("<option/>")
                        .text(regions.value[i])
                        .attr("value", regions.value[i])
                        .appendTo($(".ddlRegion", Site.regions.currStateRegionForm));
                }
	        }
	    },
	    init: function() {
	        if ($(".submission .ddlRegion").length > 0 && $(".submission .ddlState").length > 0) {
	            $(".ddlState").change(function(e) {
	                Site.regions.defaultOptionText = "please select";
	                Site.regions.currStateRegionForm = $(this).closest("ol");
	                WebUtils.GetRegionsByState($(this).val(), Site.regions.getByState_callback);
	            });
	        } else if ($(".ddlRegion").length > 0 && $(".ddlState").length > 0) {
	            $(".ddlState").change(function(e) {
                    Site.regions.defaultOptionText = "all";
	                Site.regions.currStateRegionForm = $(this).closest("ol");
	                WebUtils.GetRegionsByState($(this).val(), Site.regions.getByState_callback);
	            });
	        }
	    }
	},
	manageProfile: {
	    professionType: null,
	    init: function() {
	        if ($('fieldset.manageProfile .profession').length > 0 &&
	            $('fieldset.manageProfile .animalType').length > 0) {
	            $('fieldset.manageProfile .profession input').click(function(e) {
	                selected = $('fieldset.manageProfile .profession input:checked').parent().attr('class');
	                if (Site.manageProfile.professionType != selected && 
	                    (selected == 'breeder' || selected == 'other')) {
	                    $('fieldset.manageProfile .animalType').slideDown("slow");
	                    Site.manageProfile.professionType = selected;
	                } else if (Site.manageProfile.professionType != selected && selected == 'agent') {
	                    $('fieldset.manageProfile .animalType').slideUp("slow");
	                    Site.manageProfile.professionType = selected;
	                    $('fieldset.manageProfile .animalType input:checked').removeAttr("checked");
	                }
	            });
	        }
	        if ($('fieldset.manageProfile .membership input').length > 0 &&
	            $('fieldset.manageProfile .profession input').length > 0) {
	            $('fieldset.manageProfile .membership input').click(function(e) {
	                var type = $(this).parent().attr('class');
	                if (type.indexOf('selected', 0) == -1) {
                        $('.type', $(this).parent().parent()).removeClass('selected');
	                    $(this).parent().addClass('selected');
	                    if ($('fieldset.manageProfile .profession input:checked').length > 0) {
	                        var profession = $('fieldset.manageProfile .profession input:checked').val().substring(3);
	                        if (type.indexOf('premium', 0) > -1 && profession == "Other") {
                                $('fieldset.manageProfile .profession input:checked').removeAttr("checked");
                            }
	                    }
	                    if (type.indexOf('free', 0) > -1) {
	                        $('fieldset.manageProfile .profession .other').show();
	                    } else if (type.indexOf('premium', 0) > -1) {
	                        $('fieldset.manageProfile .profession .other').hide();
	                    }
	                }
	            });
	        }
	    }
	},
	centeringpage: {
		centerpaging: function(){
			$('div.search-paging ul').width(($('div.search-paging ul li').length * 33) + (50) - 6);
		},
		init: function(){
			if($('body').hasClass('search')){
				this.centerpaging();
			}
		}
	},
	newsListing: {
	    init: function (){
	        if ($('.moduleNewsList .moduleContent .moduleListing .item').length > 0) {
	            $('.moduleListing li').hover(function(){
                    $(this).addClass('hover');
                }, function(){
                    $(this).removeClass('hover');
                });	            
	        }
	    }
	},
	bookmarkButtons: {
	    init: function() {
	        $('div.tools ul.page-tools li.bookmark a').jFav();
	        $('div.footer-nav ul li.bookmark a').jFav();
	    }
	},
	init: function() {
		this.navigationFade();
		this.shortlist.init();
		this.landing.init();
		this.search.init();
		this.classified.init();
		this.profile.init();
		this.regions.init();
		this.centeringpage.init();
		this.manageProfile.init();
		this.newsListing.init();
		this.bookmarkButtons.init();
	}
}

$(document).ready(function() {
  Site.init();
});

if (!(typeof(DD_belatedPNG) == "undefined")) {
	DD_belatedPNG.fix('a.badger-advertise, div.top, #header .site-name, #navigation, #navigation ul li a, #navigation ul li a.active, #navigation ul li a:hover, #navigation ul li  span, .btn-brown, .btn-yellow, .btn-green, .btn-grey,.btn-blue, .btn-brown, .btn-yellow span, .btn-green span, .btn-grey span, .btn-blue span, .btn-brown span, .advertise-list li, .ad img, #utility div.user-nav ul li a, div.secondary-navigation ul li a, #footer a.back-top, body.signup table img, .benefits-list .benefits-point1, .benefits-list .benefits-point2, .benefits-list .benefits-point3, .benefits-list .benefits-point4, a.badger-sell, .benefits-tick, .page-tools li a, .user-nav ul li a, .img .sold, .bottom .wrap .back-top');
}
