var popup = {
    hideClass: null,
    courtineId: "courtine",
    courtineOpacity: "0",
    courtineZindex: "100",
    courtineBackgroundColor: "#000",
    popupClassName: "popup",
    popupZindex: "120",
    showPopup: function(id, hideClass,opacity){
		if(arguments[2]){
			popup.courtineOpacity = opacity;
		}
		if ($("#" + id) && $("#" + id).hasClass(popup.popupClassName)) {
            if (hideClass!="" && $("#" + id + "> ." + hideClass)) {
                popup.hideClass = hideClass;
                $("#" + id + "> ." + hideClass).click(function(){
                    popup.hidePopup(id);
                    return false;
                })
            }
            $("#" + id).css("z-index", popup.popupZindex);
            popup.hideSelectsForIE6(id);
            popup.popupCourtine(id);
            popup.justify(id);
			$("#" + id).show();
			$("#" + id).find("input").each(function(){
				if(!$(this + "[type='submit']")){
					popup.hideDefaultInputText($(this));
				}
			})
        }
        return false;
    },
    hidePopup: function(id){
        if ($("#" + id) && $("#" + id).hasClass(popup.popupClassName)) {
            $("#" + popup.courtineId).remove();
            $("#" + id).hide();
            $("select").show();
        }
    },
    popupCourtine: function(windowId){
        var courtine = document.createElement("div");
		if($(document).height()>$(window).height()){
			var height = $(document).height();
		}else{
			var height = $(window).height();
		}
		
        courtine.id = popup.courtineId;
        $(courtine).css({
            backgroundColor: popup.courtineBackgroundColor,
            zIndex: popup.courtineZindex,
            position: "absolute",
            top: "0",
            left: "0",
            opacity: popup.courtineOpacity,
            width: $(document).width() + "px",
            height: height + "px"
        });
        document.body.appendChild(courtine);
        $('#' + popup.courtineId).click(function() {
            popup.hidePopup(windowId);
        });
    },
    justify: function(id){
        if ($("#" + id) && $("#" + id).hasClass(popup.popupClassName)) {
            var popupHeight = $("#" + id + "." + popup.popupClassName).height()
            var popupWidth = $("#" + id + "." + popup.popupClassName).width()
            var winDimensions = popup.getPageSize();
            var topPosition = $(document).scrollTop() + ((winDimensions[3] - popupHeight) / 2);
            var leftPosition = $(document).scrollLeft() + ((winDimensions[2] - popupWidth) / 2);
            
            $("#" + id).css({
                top: topPosition + "px",
                left: leftPosition + "px"
            })
        }
    },
    hideSelectsForIE6: function(eId){
        if ($.browser.msie && 6 < parseInt($.browser.version) < 7) {
            $("select").each(function(){
                $(this).hide();
            });
            $("#" + eId).find("select").each(function(){
                $(this).show();
            })
        }
    },
    getPageSize: function(){
        var xScroll, yScroll;
        if (window.innerHeight && window.scrollMaxY) {
            xScroll = document.body.scrollWidth;
            yScroll = window.innerHeight + window.scrollMaxY;
        }
        else 
            if (document.body.scrollHeight > document.body.offsetHeight) {
                // all but Explorer Mac 
                xScroll = document.body.scrollWidth;
                yScroll = document.body.scrollHeight;
            }
            else {
                // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari 
                xScroll = document.body.offsetWidth;
                yScroll = document.body.offsetHeight;
            }
        var windowWidth, windowHeight;
        if (self.innerHeight) {
            // all except Explorer 
            windowWidth = self.innerWidth;
            windowHeight = self.innerHeight;
        }
        else 
            if (document.documentElement && document.documentElement.clientHeight) {
                // Explorer 6 Strict Mode 
                windowWidth = document.documentElement.clientWidth;
                windowHeight = document.documentElement.clientHeight;
            }
            else 
                if (document.body) {
                    // other Explorers 
                    windowWidth = document.body.clientWidth;
                    windowHeight = document.body.clientHeight;
                }
        // for small pages with total height less then height of the viewport 
        if (yScroll < windowHeight) {
            pageHeight = windowHeight;
        }
        else {
            pageHeight = yScroll;
        }
        // for small pages with total width less then width of the viewport 
        if (xScroll < windowWidth) {
            pageWidth = windowWidth;
        }
        else {
            pageWidth = xScroll;
        }
        arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight)
        return arrayPageSize;
    },
	hideDefaultInputText: function(e){
		var oldText = null;
		var focusNo = null;
		$(e).focus(function(){
			if(focusNo==null){
				focusNo+=1;
				oldText = $(this).val();
				$(this).val("");
			}else{
				if($(this).val()==oldText){
					$(this).val("");
				}
			}
		})
		$(e).blur(function(){
			if($(this).val()==""){
				$(this).val(oldText);
			}
		})
	}
}

var showAdDetailsTabInformation = {
	allIds: [],
	menuClass: "accountMenu",
	tabClass: "tab",
	activeTabClass: "active",
	getAllPlaceId: function(){
		$("." + showAdDetailsTabInformation.menuClass).find("li."+showAdDetailsTabInformation.tabClass).each(function(){
			if($(this).attr("class").split(" ").length >= 2){
				showAdDetailsTabInformation.allIds.push($(this).attr("class").split(" ")[1]);
			}
		})
	},
	hideAllPlaces: function(){
		for(var i=0; i<showAdDetailsTabInformation.allIds.length; i++){
			$("#"+showAdDetailsTabInformation.allIds[i]+"Content").hide();
		}
	},
	clearAllActiveClass: function(){
		$("." + showAdDetailsTabInformation.menuClass).find("li."+showAdDetailsTabInformation.tabClass).each(function(){
			$(this).removeClass(showAdDetailsTabInformation.activeTabClass);
		});
	},
	showPlaceHolder: function(e){
		$("#"+$(e).attr("class").split(" ")[1]+"Content").show();
	},
	init: function(){
		showAdDetailsTabInformation.getAllPlaceId();
		$("." + showAdDetailsTabInformation.menuClass).find("li."+showAdDetailsTabInformation.tabClass).each(function(){
			$(this).click(function(){
				showAdDetailsTabInformation.hideAllPlaces();
				showAdDetailsTabInformation.clearAllActiveClass();
				showAdDetailsTabInformation.showPlaceHolder($(this));
				$(this).addClass(showAdDetailsTabInformation.activeTabClass);
				if($(this).attr("class").indexOf("advertDetails")>-1){
					$("#bookmarkingContainer").show();
				}else{
					$("#bookmarkingContainer").hide();
				}
                var classname = $(this).attr("class"); 
				if( classname.indexOf("location")>-1 || classname.indexOf("stockListing")>-1){
					$("#reviewForm").hide();
					$("#car_reviews").hide();
					$("#reviewMainBox").hide();
					$( "#anonymousAdReviewsContainer" ).hide();
					$( "#anonymousAdReviewFormContainer" ).hide();
					$(".gallery_ads_box").hide();
					if($(this).attr("class").indexOf("location")>-1 ){
						loadBestMapType();
					}
                } else if( classname.indexOf("dealerInfo")>-1 ) {
                    $("#reviewForm").hide();
                    $("#car_reviews").hide();
					$( "#anonymousAdReviewsContainer" ).hide();
					$( "#anonymousAdReviewFormContainer" ).hide();
                    $(".gallery_ads_box").hide();
                    $("#reviewMainBox").show();
				}else{
					$("#reviewForm").show();
					$("#car_reviews").show();
					$( "#anonymousAdReviewsContainer" ).show();
					$( "#anonymousAdReviewFormContainer" ).show();
					$("#reviewMainBox").hide();
					$(".gallery_ads_box").show();
				}
				return false
			})
		})
	},
	showTab: function(id){
		showAdDetailsTabInformation.hideAllPlaces();
		showAdDetailsTabInformation.clearAllActiveClass();
		$("#"+id).show();
		$("."+id.replace("Content","")).addClass(showAdDetailsTabInformation.activeTabClass);
		if(id.indexOf("advertDetails")>-1){
			$("#bookmarkingContainer").show();
		}else{
			$("#bookmarkingContainer").hide();
		}
		if(id.indexOf("location")>-1 || id.indexOf("stockListing")>-1){
			$("#reviewForm").hide();
			$("#car_reviews").hide();
			$( "#anonymousAdReviewsContainer" ).hide();
			$( "#anonymousAdReviewFormContainer" ).hide();
			$("#reviewMainBox").hide();
			$(".gallery_ads_box").hide();
			if(id.indexOf("location")>-1 ){
				loadBestMapType();
			}
		}else{
			$("#reviewForm").show();
			$("#car_reviews").show();
			$( "#anonymousAdReviewsContainer" ).show();
			$( "#anonymousAdReviewFormContainer" ).show();
			$("#reviewMainBox").show();
			$(".gallery_ads_box").show();
		}
		return false
	}
}

var otherTools = {
	holderId: "other_tools",
	showHideMenu: function(){
		$("#" + otherTools.holderId).hover(function(){
			$(this).find("ul").show();
		},function(){
			$(this).find("ul").hide();
		})
	},
	selectOption: function(){
		$("#" + otherTools.holderId).find("li").each(function(){
			$(this).hover(function(){
				$(this).addClass("hovered");
			},function(){
				$(this).removeClass("hovered");
			});
			
			$(this).click(function(){
				return popup.showPopup($(this).attr("class").split(" ")[0],$("#"+$(this).attr("class").split(" ")[0]).find("a:first").attr("class"),"0.5");
			});
		})
	},
	init: function(){
		otherTools.showHideMenu();
		otherTools.selectOption();
	}
}

$(document).ready(function(){
    $(".accountMenu > ul > li.tab").each(function(){
		showAdDetailsTabInformation.init();
		return false;
    });
	//otherTools.init();
})

var slide = {
        divIdOrClassName: '.gallery_container',
        rowElementTag: 'div.video_ads',
        rowElementNo: 0,
        currentElementIndex: 0,
        visibleElementNo: 5,
        jump: 5,
        left: function(){
	        slide.rowNo();
            if((slide.currentElementIndex - slide.jump) >= 0){
                var divOffset = $(slide.divIdOrClassName).offset().left;
                slide.currentElementIndex = slide.currentElementIndex - slide.jump;
                var elemOffset = $(slide.divIdOrClassName +' ' + slide.rowElementTag + ':eq('+slide.currentElementIndex+')').offset().left;
                var elemScroll = elemOffset - divOffset;
                $(slide.divIdOrClassName).animate({scrollLeft: '+=' + elemScroll + 'px'}, 1000);
                if((slide.currentElementIndex - slide.jump) <= 0){
                    $("#ScrollLeft").css("visibility","hidden");
                }
                if((slide.currentElementIndex + slide.jump ) <=    slide.rowElementNo-1){
                    $("#ScrollRight").css("visibility","visible");
                }
            }
        },
        right: function(){
            slide.rowNo();
            if((slide.currentElementIndex + slide.jump + slide.visibleElementNo) <= slide.rowElementNo){
                var divOffset = $(slide.divIdOrClassName).offset().left;
                slide.currentElementIndex = slide.currentElementIndex + slide.jump;
                var elemOffset = $(slide.divIdOrClassName +' ' + slide.rowElementTag + ':eq('+slide.currentElementIndex+')').offset().left;
                var elemScroll = elemOffset - divOffset;
                $(slide.divIdOrClassName).animate({scrollLeft: '+=' + elemScroll + 'px'}, 1000);
     
                if((slide.currentElementIndex + slide.jump + slide.visibleElementNo ) > slide.rowElementNo-1){
                    $("#ScrollRight").css("visibility","hidden");
                }
                if((slide.currentElementIndex - slide.jump) >= 0){
                    $("#ScrollLeft").css("visibility","visible");
                }
            }
        },
        rowNo: function(){
            slide.rowElementNo = $(slide.divIdOrClassName + ' ' + slide.rowElementTag).length;
        },
        check: function(){
            slide.rowNo();
            if( slide.rowElementNo <= slide.visibleElementNo+1 ){
                $("#ScrollDown").addClass("disable");
            }
        }
    }
