// site specific javascript

	// html 5 elements
    document.createElement("header");
    document.createElement("hgroup");
    document.createElement("nav");
    document.createElement("article");
    document.createElement("address");
    document.createElement("section");
    document.createElement("datalist");
    document.createElement("time");
    document.createElement("footer");

if(!jQuery.browser.safari)
{
	jQuery(document).ready(function(){
		behavior_binder();
	});
}
else
{
	jQuery(window).load(function(){
		behavior_binder();
	});
}


/*
Binds behaviors
** can be re-run whenever there are DOM changes
*/
function behavior_binder(){
	
	/* TRUNCATE TEXT BLOCKS */
	if(jQuery('.pod_detail_boilerplate').length){
		truncate_textblock('pod_detail_boilerplate');
	}
	if(jQuery('.entry-content').length){
		truncate_textblock('entry-content');
	}
	/* DRAWFONT */
	jQuery('.sidebar_article header h2').drawFont();
	jQuery('.contact-us h2.section_title').drawFont();
	jQuery('.contact-us #pod_selector_container #pod_selector li a').drawFont();
	jQuery('.corporate-citizenship #pod_selector_container #pod_selector li a').drawFont();
	jQuery('#homepage_news .items li .item_title').drawFont();
	jQuery('#division_name').drawFont();
	jQuery('#section_menu li a').drawFont();
	jQuery('#company_head #company_detail_links li a').drawFont(); 
	
	/* HOMEPAGE DL */	
	jQuery('.dynamic_lead .inline_pager_list').inlinePager({
		numItems:1,
		autoPage: 5000 //switch to 5000 when ready for dev
	});
	/* HOMEPAGE LOGO CAROUSEL */
	// turns on the carousel; numItems should be set to 9	
	jQuery('#homepage_logocarousel').inlinePager({
		numItems:9
	});
	// shows the tooltip on hover of logos
	jQuery(".show_tooltip").qtip({
            content: {
               text: function(api){
               		return jQuery(this).siblings('.tooltip').find('p').html();
               }, 
               title: {
                  text: function(api){
               		return jQuery(this).siblings('.tooltip').find('h3 a').html();
               },
                  button: false
               },
			   style: {
				   tip: false
				}	
			},
			position: {
					 adjust: {
						 x: -7,
						 y:-25
					  }
			}
			
			
			
	});
	
	/*Division Carousel roll overs*/
	
	var dl_items_a = jQuery('.dl-divisions .dl_item a'); //array of all the elements that have tooltips
	
	
	
	jQuery.each(dl_items_a, function(i, slice){
	
		var slice = $(slice);
		
		//Check if there are attributes for the roll over tooltip
		if( !(slice.attr('href') === '' && slice.find('img').attr('alt') == '' && slice.find('span').text() == '') ){
		
			slice.qtip({ 

						content: {
						   text: function(api){
									return jQuery(this).find('span').text();
						   },
						   title: function(api){
									return jQuery(this).find('img').attr('alt');
						   },
						   button: false
						},
						style: {
							   tip: false
						},
						position: {
									  my: 'bottom center',  // Position my top left...
									  at: 'center', // at the bottom right of...
									 adjust: {
										 x: 175,
										 y:0
  								     }
								  
								  }
								  
						
			
	    });	
		
		
		}//End of if
	
	
	
	});
	/*
	jQuery('.dl-divisions .dl_item a').qtip({ 

						content: {
						   text: function(api){
									return jQuery(this).find('span').text();
						   },
						   title: function(api){
									return jQuery(this).find('img').attr('alt');
						   },
						   button: false,
						   style: {
							   tip: false
							}
							
						},
						position: {
									  my: 'bottom center',  // Position my top left...
									  at: 'center', // at the bottom right of...
									 adjust: {
										 x: 175,
										 y:0
  								     }
								  
								  }
						
			
	});
	*/
	/* HOMEPAGE NEWS TICKER */	
	jQuery('#homepage_newsticker').inlinePager({
		numItems:1
	});
	
	/* HEADER SEARCH FORM */
	jQuery('#searchsubmit').attr('value','');
	jQuery('#searchform').submit(function() {
		if(jQuery(this).find('#s').val()==''){
			alert('Please add a search term');
			return false;
		}
	});
	jQuery('#search_engine_Area form').submit(function() {
		if(jQuery(this).find('.search_engine_Box').val()==''){
			alert('Please add a search term');
			return false;
		}else{
			var s = jQuery(this).find('.search_engine_Box').val();
			s = s.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/"/g, '&quot;');
			jQuery(this).find('.search_engine_Box').val(s);
		}
	});
	/* MENU FUNCTIONS */
	
	jQuery('#menu-nbcuni-menu li a').each(function(){
		jQuery(this).parent('li').addClass('menu-item-'+jQuery(this).html().toLowerCase().replace(/\&amp;/g,'').replace(/[^a-zA-Z0-9 ]/g,'').replace(/\s+/g,'-'));
	})
	jQuery('#menu-corporate-menu li a').each(function(){
		jQuery(this).parent('li').addClass('menu-item-'+jQuery(this).html().toLowerCase().replace(/\&amp;/g,'').replace(/[^a-zA-Z0-9 ]/g,'').replace(/\s+/g,'-'));
	})
	menu_corporate_menu();
	jQuery('.menu-item').append('<span class="menu-item-bg"></span>');
	jQuery('.menu-item-careers a').attr('target','_blank');
	jQuery('.menu-item-investor-relations a').attr('target','_blank');
        
        jQuery('.menu-item-investor-relations a').click(function(){
         alert("You are being re-directed to the Comcast IR site.");    

    })


	jQuery('.menu-item-shopping a').attr('target','_blank');
	jQuery('#menu-nbcuni-menu').show();
	/* PHOTO GALLERY */
	jQuery('div#photoGallery').slideProjector({keep_state:0,preload_images: false,counter_format: '$$AT of $$TOTAL'});
	
	/* MEDIA UPLOAD (in pods) */
	jQuery('.dropme').live('click',function(){
		jQuery(this).parents('.image_manager_block').html('');
	})
	
	/* PROJECT IMAGES */
	jQuery('#project_thumb_images li a').click(function(){
		var this_img = jQuery(this).attr('rel');
		jQuery('#project_main_images li').hide();
		jQuery('#'+this_img).show();
		jQuery('#project_thumb_images .selected').removeClass('selected');
		jQuery(this).parent('li').addClass('selected');
	})
	
	// grabs a clone of the list of LI's in pod_list
	var pod_list_clone = jQuery('#pod_list').clone();
	// count the number of people showing, fill in the number
	jQuery('.section_list_title_number').html(jQuery('#pod_list li').length);
	jQuery('.pod_selector_selection').click(function(){
		jQuery('#pod_list li').remove();
		jQuery('#pod_selector li').removeClass('selected');
		jQuery(this).parent('li').addClass('selected');
		var this_letter = jQuery(this).attr('rel');
		var this_name = jQuery(this).html();
		if(this_letter == 'all'){
			// if they've clicked ALL, show all items from the cloned version of the whole list, change the hashtag
			if(typeof window.url_hash_parser == 'function') {
				url_hash_parser('company='+this_letter);
			}
			jQuery('#pod_list').html(jQuery(pod_list_clone).find('li.item').clone());
			
		}else{
			if(jQuery('.pod_selector_business_container').length){
				if(typeof window.url_hash_parser == 'function') {
					url_hash_parser('company='+this_letter);
				}
				jQuery('#pod_list').html(jQuery(pod_list_clone).find('li.sort-company-'+this_letter).clone());
			}else{
				if(typeof window.url_hash_parser == 'function') {
					url_hash_parser('letter='+this_letter);
				}
				jQuery('#pod_list').html(jQuery(pod_list_clone).find('li.sort-letter-'+this_letter).clone());
			}
		}
		jQuery('#pod_list_container').html(jQuery('#pod_list'));
		jQuery('.section_list_title_number').html(jQuery('#pod_list li').length);
		jQuery('.section_list_title_business').html(this_name);
		set_pagination();
		
	})
	
	/* SEARCH RESULTS */
	
	jQuery(".search_engine_results").quickPager({
		pagerLocation:"after",
		pageSize:6,
		itemsPerRow:1,
		pageCounterTemplate: 'Showing [$$START]-[$$END] of [$$TOTAL]'
	});
	trigger_page_hash(pod_list_clone);
}

function menu_corporate_menu(){
	jQuery('.menu-item-'+section_main).addClass('current-menu-item');
	jQuery('.menu-corporate .menu-item-'+section_path_1).addClass('current-menu-item');
}
function trigger_page_hash(pod_list_clone){
	/* TRIGGER PAGINATION FOR POD LIST */
	set_pagination();
	var currentHash = window.location.hash;
	currentHash = currentHash.replace("#", '');
	var currentHashes = currentHash.split("&");
	for(x in currentHashes){
		var currHashSplit = currentHashes[x].split("=");
		if(currHashSplit[0] == 'company'){
			jQuery('a.pod_selector_selection[rel="'+currHashSplit[1]+'"]').click();
			//this is if we're coming into the contact-us page wanting to only show contacts from X company
			if((!jQuery('.pod_selector_business_container').length) && (currHashSplit[1] != 'all')){
				jQuery('#pod_list').html(jQuery(pod_list_clone).find('li.sort-company-'+currHashSplit[1]).clone());
				jQuery('#pod_list').find('.item').show();
			}
		}
		if(currHashSplit[0] == 'letter'){
			jQuery('a.pod_selector_selection[rel="'+currHashSplit[1]+'"]').click();
		}
		if(currHashSplit[0] == 'page'){
			jQuery('ul.simplePagerNav a[rel="'+currHashSplit[1]+'"]').click();
		}
	}

}
function url_hash_parser(new_hash){
	var newHashSplit = new_hash.split("=");
	if(newHashSplit[0] != 'page'){
		window.location.replace('#'+new_hash);
		return;
	}
	var currentHash = window.location.hash;
	currentHash = currentHash.replace("#", '');
	var currentHashes = currentHash.split("&");
	var replaced;
	for(x in currentHashes){
		var currHashSplit = currentHashes[x].split("=");
		if(currHashSplit[0] == newHashSplit[0]){
			currentHashes[x] = new_hash;
			replaced = 1;
		}
	}
	var newFullHash = implode( '&', currentHashes );
	if(!replaced){
		if(currentHash){
			newFullHash = newFullHash+'&'+new_hash
		}else{
			newFullHash = new_hash
		}
		
	}
	window.location.replace('#'+newFullHash);
	var currentHash = window.location.hash;
}
function implode( glue, pieces ) {  
    // Joins array elements placing glue string between items and return one string    
    //   
    // version: 812.316  
    // discuss at: http://phpjs.org/functions/implode  
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)  
    // +   improved by: Waldo Malqui Silva  
    // *     example 1: implode(' ', ['Kevin', 'van', 'Zonneveld']);  
    // *     returns 1: 'Kevin van Zonneveld'  
    return ( ( pieces instanceof Array ) ? pieces.join ( glue ) : pieces );  
}

/*
TRIGGERS THE PAGINATION ON THE GIVEN ELEMENT
*/
function set_pagination(num_items){
	var num_per_row = 6;
	var num_to_show = 6;
	var limit = 1000;
	if(jQuery('.pod_selector_business_container').length){
		var num_per_row = 5;
		var num_to_show = 15;
		var limit = 1000;
	}
	if(jQuery('.pod_list_citizenship').length){
		var num_per_row = 6;
		var num_to_show = 12;
	}
	if(jQuery('.contact-us').length){
		var num_per_row = 3;
		var num_to_show = 12;
	}
	var num_items=arguments[0]!==undefined?arguments[0]:num_to_show;
	jQuery(".pod_list_paginated").quickPager({
		pagerLocation:"after",
		pageSize:num_items,
		itemsPerRow:num_per_row,
		pageCounterTemplate: 'Showing <span class="num_showing">[$$START]-[$$END]</span> of <span class="total_showing">[$$TOTAL]</span>',
		paginationShowLimit: limit,
		previousText: "Previous",
		nextText: "Next"
	});
}
function return_pod_landing_list_item(list_array){
	
	var list_item = '';
	for(i in list_array){
		list_item += '<li id="'+list_array[i]['slug']+'" class="item">';
		if(list_array[i]['this_link'] != 'contact_us'){
			list_item += '<a class="item_image" href="/'+list_array[i]['this_link']+'/'+list_array[i]['slug']+'" title="'+list_array[i]['name'] +'">';
			list_item += '<img alt="'+list_array[i]['name'] +'" src="'+list_array[i]['thumbnail']['img_src']+'" height="'+list_array[i]['thumbnail']['img_h']+'" width="'+list_array[i]['thumbnail']['img_w'] +'" /></a>';
		}
		list_item += '<span class="item_text">';
		if(list_array[i]['pod_link'] != 'contact_us'){
			list_item += '<h2><a href="'+list_array[i]['pod_link']+'/'+list_array[i]['slug']+'" title="'+list_array[i]['name'] +'">'+list_array[i]['name'] +'</a></h2>';
			if(list_array[i]['this_pod'] == 'people'){
				list_item += '<p class="item_job_title">'+list_array[i]['job_title'];
				if(list_array[i]['company'] != false){
					list_item += ',</p><p class="item_company">'+list_array[i]['company'];
				}
				list_item += '</p>';
			}
			if((list_array[i]['this_pod'] == 'company')||(list_array[i]['this_pod'] == 'citizenship')){
				list_item += '<p class="item_excerpt">'+list_array[i]['excerpt']+'</p>';
			}
		}else{
			list_item += '<h2><a href="'+list_array[i]['pod_link']+'/'+list_array[i]['slug']+'" title="'+list_array[i]['name'] +'">'+list_array[i]['company'] +'</a></h2>';
			list_item += '<p class="item_contact_name">'+list_array[i]['name']+'</p>';
			list_item += '<p class="item_job_title">'+list_array[i]['job_title']+'</p>';
			list_item += '<p class="item_contact_info"><span class="item_contact_info_phone">';
			list_item += list_array[i]['phone'];
			list_item += '</span> | <span class="item_contact_info_email"><a href="mailto:'+list_array[i]['email']+'">'+list_array[i]['email']+'</a></span></p>';
					
		}
		
		list_item += '</span></li>';
	}
	return list_item;
}
function truncate_textblock(textblockclass){

// truncate_textblock adds a "continue"/"back to top" link to long textblocks where the user has specified a "more" section in the wysiwyg

	if(jQuery('.'+textblockclass).length){
		var btext = jQuery('.'+textblockclass).html();
		// does replacement for wordpress created content such as pages
	    	var pattern = /<span id="more-(\d+)"><\/span>/;
	    	var pattern2 = /<span id="more-(\d+)"\/>/;
	    	var pattern3 = /<SPAN id=more-(\d+)><\/SPAN>/;
	    	var replacement = '<!--more-->';
	    	btext =  btext.replace(pattern,replacement);
	    	btext =  btext.replace(pattern2,replacement);
	    	btext =  btext.replace(pattern3,replacement);
		if(btext.match("<!--more-->")){
			var split_btext = btext.split("<!--more-->");
			var btext2 = split_btext[1].replace('</p>','');
			jQuery('.'+textblockclass).html();
			jQuery('.'+textblockclass).html(split_btext[0]+'<div class="btext">'+btext2+'</div>');
			jQuery('.'+textblockclass).after('<div class="truncate_container"><a href="Javascript:;" class="truncate_more_link">continued</a></div>');
			jQuery('.btext').hide();
		}
	}
	jQuery('.truncate_more_link').live('click',function(){
		if(jQuery('.btext').is(':visible')){
			jQuery('.btext').hide();
			jQuery(this).removeClass('truncate_less_link');
			jQuery(this).html('continued');
		}else{
			jQuery('.btext').show();
			jQuery(this).addClass('truncate_less_link');
			jQuery(this).html('back to top');
		}
	})
}
