$(document).ready(function() {
	if (!$('#main-nav li.b-dashboard a').hasClass('active')) {
		$('#subnav').hide();
	};
	$('#welcome .box .color-box').hide();
	tooltip();
	$('table tr:odd').addClass('odd-row');

	$('.toggle-table tbody tr[class^=for-]').hide();
	$('.toggle-table tbody tr.for-campaign-1').show();

	$('#write-ad-block').hide();
});
$(function() {
	$('.status-dropdown').hover(function() {
		$(this).addClass('status-dropdown-hover');
	}, function() {
		$(this).removeClass('status-dropdown-hover');
	});
	$('.status-dropdown li').mouseover(function() {
		$(this).addClass('hover');
	}).mouseout(function() {
		$(this).removeClass('hover');
	});

	$('.sort-buttons ul li.dropdown').hover(function() {
		$(this).find('.dd-content').slideDown('fast');
		$(this).find('a:eq(0)').addClass('hover');
	}, function() {
		$(this).find('.dd-content').slideUp('fast');
		$(this).find('a:eq(0)').removeClass('hover');
	});
	if ($('#how-pages').length) {
		$(window).scroll(function () {
			var top = $('#how-pages').position().top + $('#main').position().top + $('#how-pages h2:eq(0)').height();
			
			if ($.browser.safari) { var scollpos = document.body.scrollTop; } else { var scollpos = document.documentElement.scrollTop; }
			
			if (scollpos > top) { $('#how-pages #sidebar').css({'top' : parseInt(scollpos - top) + 'px'}); }
		});
	}

	$('.toggle-table tr.toggler').live('click', function() {
		$('.toggle-table tbody tr[class^=for-]').hide();
		$('.toggle-table tbody tr.toggler').removeClass('toggler-opened');
		$(this).addClass('toggler-opened');
		var id = $(this).attr('id');
		$(this).parents('.toggle-table tbody').find('tr.for-'+id).show();
	});
	
    $('#filters .toggler').click(function () {
	    if ($(this).parents('#filters:eq(0)').find('.content').is(':hidden')) {
		    $(this).parents('#filters:eq(0)').find('.content').slideDown();
	    } else {
		    $(this).parents('#filters:eq(0)').find('.content').slideUp();
	    }
	    $(this).toggleClass('toggler-opened');
	    if ($(this).hasClass('toggler-opened')) {

            if($(this).html() == "View Answer")
                $(this).html('Hide Answer');
            else if($(this).html() == "Reply")
                $(this).html('Hide');
            else
                $(this).html('Hide Filters');
            return;
	    };
        if($(this).html() == "Hide Answer")
            $(this).html('View Answer');
        else if($(this).html() == "Hide")
            $(this).html('Reply');
	    else
	        $(this).html('Show Filters');
    });

	$('table tr').live('mouseover',function() {
		$(this).addClass('tr-hover');
	}).live('mouseout',function() {
		$(this).removeClass('tr-hover');
	});
	
	$('.all-expander').live('click', function() {
		$('.content-toggler').addClass('content-toggler-opened');
		$('.content-toggled').slideDown();
		$('.all-collapser').show();
		$('.all-expander').hide();
	});
	$('.all-collapser').live('click', function() {
		$('.content-toggler').removeClass('content-toggler-opened');
		$('.content-toggled').slideUp();
		$('.all-expander').show();
		$('.all-collapser').hide();
	});
	$('.content-toggler').live('click', function() {
		$(this).toggleClass('content-toggler-opened');
		$(this).next('.content-toggled').toggle('slow');
	});	
	
	$('#agency-box .menu').click(function() {
	    $(this).find('.expanding-content').eq(0).toggle();
	    return false;
    });
    $('#agency-box .menu').hover(
    	function () {},
    	function() { $(this).find('.expanding-content').eq(0).hide() }
	);
    $('#welcome .box').hover(
    	function() {
    		$(this).find('.color-box').eq(0).show();
    	},
    	function() {
    		$(this).find('.color-box').eq(0).hide();
    	});
	$('#welcome .close').click(function() {
		$('#welcome').slideUp();		
		return false;
	});
	$('input.field').
    focus(function() {
        if(this.title==this.value) {
            this.value = '';
        }
    }).
    blur(function(){
        if(this.value=='') {
            this.value = this.title;
        }
    });
    if ($.browser.msie && parseInt($.browser.version)==6) {
	    $('.hideselect').hover(function () {
	        $(this).next().css('visibility', 'hidden');
	    }, function () {
	        $(this).next().css('visibility', 'visible');
	    });
	}

	$('.challenge-box .side .ico, .health-info, .challenge-box .percent-box .ico').hover(function() {
		$(this).find('.bubble:eq(0)').show();
	}, function() {
		$(this).find('.bubble:eq(0)').hide();
	});
	
	$('.bubble .close').live('click', function() {
		$(this).parents('.bubble:eq(0)').fadeOut();
	});	

	$('#write-ad-block .close, #write-ad-block .buttons a:contains(Cancel)').live('click', function() {
		$(this).parents('#write-ad-block:eq(0)').slideUp();
		$('.button-write-ad').removeClass('button-write-ad-active');
	});
	
	$('.button-write-ad').live('click', function() {
		$('#write-ad-block').slideDown();
		$(this).toggleClass('button-write-ad-active');
		return false;
	});
});
this.tooltip = function() {
    /* CONFIG */
    xOffset = 20;
    yOffset = -195;

    // these 2 variable determine popup's distance from the cursor
    // you might want to adjust to get the right result		
    /* END CONFIG */
    $("a.tooltip").hover(function(e) {
        width = "154";
        _yOffset = yOffset;
        this.t = this.title;
        this.title = "";

        if ($(this).attr("width") != undefined) {
            customWidth = $(this).attr("width");
            if (customWidth > width)
                _yOffset = _yOffset - (customWidth - width);
            else
                _yOffset = _yOffset + (width - customWidth);

            width = customWidth;
        }

        $("body").append("<div id='tooltip'><a href='#' class='close'>x</a><span class='arrow'>&nbsp;</span>" + this.t + "</div>");

        $("#tooltip")
			.css("top", (e.pageY - xOffset) + "px")
			.css("left", (e.pageX + _yOffset) + "px")
			.css("width", width + "px")
			.fadeIn("fast");

    },
	function() {
	    this.title = this.t;
	    $("#tooltip").remove();
	});
	$("a.tooltip").mousemove(function(e) {
	    width = "154";
	    _yOffset = yOffset;
	    if ($(this).attr("width") != undefined) {
	        customWidth = $(this).attr("width");
	        if (customWidth > width)
	            _yOffset = _yOffset - (customWidth - width);
	        else
	            _yOffset = _yOffset + (width - customWidth);
	    }
	    
        $("#tooltip")
			.css("top", (e.pageY - xOffset) + "px")
			.css("left", (e.pageX + _yOffset) + "px");
    });

};

var FiledValidation = {
    IsValidEmail: function(obj) {
         var fieldvalue = $(obj).val();
         if (fieldvalue.length >0) {
             i=fieldvalue.indexOf("@");
             j=fieldvalue.indexOf(".",i);
             k=fieldvalue.indexOf(",");
             kk=fieldvalue.indexOf(" ");
             jj=fieldvalue.lastIndexOf(".")+1;
             len=fieldvalue.length;
            if ((i>0) && (j>(1+1)) && (k==-1) && (kk==-1) && (len-jj >=2) && (len-jj<=3)) {
            }
            else {
                return false;
            }
            return true;
         }
    },
    IsNoSpecialChar: function(obj) {
        var fieldvalue = $(obj).val();
        var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";

        for (var i = 0; i < fieldvalue.length; i++) {
            if (iChars.indexOf(fieldvalue.charAt(i)) != -1) {
                return false;
            }
        }
        return true;
    },
    IsChar: function(obj) {
        var fieldvalue = $(obj).val();
        var noalpha = /^[a-zA-Z]*$/;
        if (noalpha.test(fieldvalue))
            return true;
        else
            return false;
    },
    IsPhoneNumber: function(obj) {
        var fieldvalue = $(obj).val();
        //var nonums = /^[0-9]*-[0-9]*-[0-9]*$/;
        var nonums = /^(1\s*[-\/\.]?)?(\((\d{3})\)|(\d{3}))\s*[-\/\.]?\s*(\d{3})\s*[-\/\.]?\s*(\d{4})\s*(([xX]|[eE][xX][tT])\.?\s*(\d+))*$/;
        if (nonums.test(fieldvalue))
             return true;
        else
             return false;
    },
    IsWebURL: function(obj) {
        var fieldvalue = $(obj).val();
        //var nonums = /^(((ht|f){1}(tp:[/][/]){1})|((www.){1}))[-a-zA-Z0-9@:%_\+.~#?&//=]+$/;
        var nonums = /^(((ht|f){1}(tp:[/][/]){1})|((ht|f){1}(tps:[/][/]){1}))[-a-zA-Z0-9@:%_\+.~#?&//=]+$/;
        if (nonums.test(fieldvalue))
             return true;
        else
             return false;
    }
};