/* for the "new updates" in the footer  */
var table_25002 = {"1":{"title":"Online and Offline Social Networks Key to Teen Shopping","article_id":"jason","date":"100601"},"2":{"title":"AMP Agency ranked #24 on this year's Promo 100 agency list","article_id":"promo100","date":"100601"}}

jQuery(document).ready(function(){
/************************************** */
/* string utilies */
/************************************** */
String.prototype.trim = function(){return
(this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, ""))}

String.prototype.startsWith = function(str)
{return (this.match("^"+str)==str)}

String.prototype.endsWith = function(str)
{return (this.match(str+"$")==str)}
/************************************** */

/************************************** */
/* fint the string start with in the array and return it
*/
function startWith(strArray, str) {
	var returnStr = "";
	for (var i = 0;i<strArray.length;i++) {
		if (strArray[i].startsWith(str) ) {
			returnStr = strArray[i];
		}
	}
	return returnStr;	
}

/************************************** */
/* Init the menu */
/************************************** */
function menuInit() {
	var sSection = "";
	var sSubSection = "";
    var active_menu_item = "";
    var active_sub_menu_item = "";	
	
	var classArray = jQuery("body").attr('class').split(' ');
	
	//menu
	var tempArray = startWith(classArray, "section" ).split('_');
	if (tempArray.length === 2) {
		sSection = tempArray[1];
	    active_menu_item = "#main_item_" + sSection;			
        jQuery("#sub_menu_"+ sSection).show();	//show the submenu if they have it..
	    jQuery(active_menu_item).addClass("active");		
	}
	
	//submenu
	var tempArray2 = startWith(classArray, "sub_section" ).split('_');
	if (tempArray2.length === 3) {	
		sSubSection = tempArray2[2];
        active_sub_menu_item = "#sub_item_" + sSubSection;
	    jQuery(active_sub_menu_item).addClass("active");
	}	
	
//	log.debug("section: " + sSection );
//	log.debug("sub_section: " + sSubSection );
}

/************************************** */

    menuInit();
    var bHideSubMenu = false;
    jQuery('.main_item').hover(
        //mouse over
        function(event) {
            if (bHideSubMenu) clearTimeout(bHideSubMenu);
            jQuery(".sub_menu").hide();
            // The Agency
            if ($(this).attr('id') == "main_item_2" ){
                jQuery("#sub_menu_2").fadeIn("slow");
            }
            // The Work
            if ($(this).attr('id') == "main_item_3" ){
                jQuery("#sub_menu_3").fadeIn("slow");
            }
            // The Skill
            if ($(this).attr('id') == "main_item_4" ){
                jQuery("#sub_menu_4").fadeIn("slow");
            }
            //the Thinking
            if ($(this).attr('id') == "main_item_5" ){
                jQuery("#sub_menu_5").fadeIn("slow");
            }
        },
        //mouse out
        function() {
            bHideSubMenu = setTimeout(function() {
                jQuery(".sub_menu").hide();
                menuInit();
            }, 1000);
        }
    );
    jQuery('.sub_menu').hover(
        function(){
        if (bHideSubMenu) clearTimeout(bHideSubMenu);
    },	function() {
        bHideSubMenu = setTimeout(function() {
            jQuery(".sub_menu").hide();
            menuInit();
        }, 1000);
    });
    
}); // END: jquery function

/***********************************************/
/* Email Validation ********************* */
/***********************************************/

 jQuery.fn.isValidEmail =  function (str) {
	var objRegExp  = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/
	 return objRegExp.test(str);
};

/***********************************************/
/* contact_us_form ********************* */
/***********************************************/
jQuery(document).ready(function(){
    jQuery("#contact_us_form").submit(function() {
        var bHasError = false;
        //inspire
        if (jQuery('#optionalText1').val() == "" ) {
            bHasError = true;
	        jQuery("#optionalText1").addClass("highlight");
	        jQuery("#optionalText1_error").addClass("texthighlight");
        } else {
			jQuery("#optionalText1_error").hide();
	        jQuery("#optionalText1").removeClass("highlight");
		}
        //firstName
        if (jQuery('#firstName').val() == "" ) {
            bHasError = true;
            jQuery("#firstName").addClass("highlight");
            jQuery("#firstName_label").addClass("texthighlight");
        } else {
            jQuery("#firstName").removeClass("highlight");
            jQuery("#firstName_label").removeClass("texthighlight");
		}
        //email
        if (jQuery('#email').val() == "") {
            bHasError = true;
            jQuery("#email").addClass("highlight");
            jQuery("#email_label").addClass("texthighlight");
        } else if( !jQuery('#email').isValidEmail(jQuery('#email').val()) ) {
            bHasError = true;
            jQuery("#email").addClass("highlight");
            jQuery("#email_label").addClass("texthighlight");
            jQuery("#email_invalid").show();
            jQuery("#email_invalid").addClass("texthighlight");
        } else {
            jQuery("#email_invalid").hide();
            jQuery("#email").removeClass("highlight");
            jQuery("#email_label").removeClass("texthighlight");
		}
        //lastName
        if (jQuery('#lastName').val() == "" ) {
            bHasError = true;
            jQuery("#lastName").addClass("highlight");
            jQuery("#lastName_label").addClass("texthighlight");
        } else {
            jQuery("#lastName").removeClass("highlight");
            jQuery("#lastName_label").removeClass("texthighlight");
		}
        //phone
        if (jQuery('#phone').val() == "" ) {
            bHasError = true;
            jQuery("#phone").addClass("highlight");
            jQuery("#phone_label").addClass("texthighlight");
        } else {
            jQuery("#phone").removeClass("highlight");
            jQuery("#phone_label").removeClass("texthighlight");
		}
        if (jQuery('#optionalText2').val() == "" ) {
            bHasError = true;
            jQuery("#optionalText2").addClass("highlight");
            jQuery("#optionalText2_label").addClass("texthighlight");
        } else {
            jQuery("#optionalText2").removeClass("highlight");
            jQuery("#optionalText2_label").removeClass("texthighlight");
		}
        if (jQuery('#optionalText3').val() == "" ) {
            bHasError = true;
            jQuery("#optionalText3").addClass("highlight");
            jQuery("#optionalText3_label").addClass("texthighlight");
        } else {
            jQuery("#optionalText3").removeClass("highlight");
            jQuery("#optionalText3_label").removeClass("texthighlight");
		}
		
		if(bHasError) {jQuery("#error_msg").show(); }
        return !bHasError;
    } );
	
	/***********************************************/
	/* the "New update" in the footer************* */
	/***********************************************/
	jQuery(".footer_item:first ul li").remove();
	function createPressLinkElement( title, article_id ) {
		var url = "http://www.ampagency.com/the_agency/press.vm?article=" + article_id ;
		var pressLink = jQuery(document.createElement('li') )
							.append( 
								jQuery( document.createElement('a') )
									.attr('href', url)
									.text(title)
							 );
		return pressLink;	
	}
		
	jQuery.each(table_25002 , function(iRow, row){
		if ( iRow<=2) {
			jQuery(".footer_item:first ul").append(createPressLinkElement ( row.title, row.article_id ) );
		}
	});
}); // END: jquery function

/***********************************************/
/* The mosaic in template c ****************** */
/***********************************************/
jQuery(document).ready(function(){
	jQuery('.frame_mosaic')
	.cycle({
		fx:     'fade', 
	    speed:  1000,
		timeout: 0,
        random: 1
	});
}); // END: jquery function
/***********************************************/
/* END: The mosaic in template c ************* */
/***********************************************/


