//Scripts by Dario

jQuery(function() { //	<-----after doc.ready
function checkSBSizeOpen() {
	var rightSidebarH = jQuery("#right_sidebar").height();
	var rightSidebarH = rightSidebarH * 1.2;
	
	jQuery('#container').css('min-height', rightSidebarH+'px');
}
function checkSBSizeClose() {
	var rightSidebarH = jQuery("#right_sidebar").height();
	
	if (rightSidebarH > 545) {
		var rightSidebarH = rightSidebarH / 1.2;
		
		jQuery('#container').css('min-height', rightSidebarH+'px');
	}
}	

function collapsing_nav() {
	jQuery('.collapsing-nav > li:first').addClass('selected');
	jQuery('.collapsing-nav > li').not(':first').find('ul').hide();
	checkSBSizeClose();
	jQuery('.collapsing-nav > li span').click(function() {
		if (jQuery(this).parent().find('ul').is(':hidden')) {
			jQuery('.collapsing-nav ul:visible').slideUp('fast');
			jQuery('.collapsing-nav > li').removeClass('selected');
			jQuery(this).parent().addClass('selected');
			jQuery(this).parent().find('ul').slideDown('fast');
			checkSBSizeOpen();
		}
	});
}

jQuery(collapsing_nav);

//For portfolio
function rememberWhere() {
/* always remember were we where so we can expand later through jQuery */
	jQuery("li.dir").click(function () {
	  jQuery.cookie("portfolioWasOn", null);
	  var index = jQuery("#right_sidebar li").index(this);
	  jQuery.cookie("portfolioWasOn", index, { path: '/' });
	});

}
jQuery(rememberWhere);

function collapsing_nav_rest() {	
	var indexCookie = jQuery.cookie("portfolioWasOn");
	jQuery('.collapsing-nav_o > li').find('ul').hide();
	//expand in case something was clicked before
	if (typeof indexCookie == 'undefined') {} else { jQuery('#right_sidebar li').eq(indexCookie).children('ul').css('display', 'block'); }
	jQuery.cookie("portfolioWasOn", null);
	jQuery('.collapsing-nav_o > li span').click(function() {
		if (jQuery(this).parent().find('ul').is(':visible')) {
			jQuery('.collapsing-nav_o ul:visible').slideUp('fast');
			jQuery('.collapsing-nav_o > li').removeClass('selected');
			checkSBSizeClose();
			} else {
				if (jQuery(this).parent().find('ul').is(':hidden')) {
					jQuery('.collapsing-nav_o ul:visible').slideUp('fast');
					jQuery('.collapsing-nav_o > li').removeClass('selected');
					jQuery(this).parent().addClass('selected');
					jQuery(this).parent().find('ul').slideDown('fast');
					checkSBSizeOpen();
				}
			}
	});

}

jQuery(collapsing_nav_rest);


//check url and expand the current menu right sidebar
function checkURLexpand() {

   if (/\/products/.test(window.location)) {
   		  jQuery("span:contains('Products')").parent().find('ul').slideDown('fast',function(){
  				checkSBSizeOpen();
				});
	} else 
	if (/\/product-families/.test(window.location)) {
   		  jQuery("span:contains('Product Families')").parent().find('ul').slideDown('fast',function(){
  				checkSBSizeOpen();
				});
	}else 
	if (/\/companies/.test(window.location)) {
   		  jQuery("span:contains('Companies')").parent().find('ul').slideDown('fast',function(){
  				checkSBSizeOpen();
				});
	}else 
	if (/\/taglines/.test(window.location)) {
   		  jQuery("span:contains('Taglines')").parent().find('ul').slideDown('fast',function(){
  				checkSBSizeOpen();
				});
	}else 
	if (/\/Books/.test(window.location)) {
   		  jQuery("span:contains('Books')").parent().find('ul').slideDown('fast',function(){
  				checkSBSizeOpen();
				});
	}else 
	if (/\/cheeky/.test(window.location)) {
   		  jQuery("span:contains('Cheeky')").parent().find('ul').slideDown('fast',function(){
  				checkSBSizeOpen();
				});
	}else 
	if (/\/corporate/.test(window.location)) {
   		  jQuery("span:contains('Corporate')").parent().find('ul').slideDown('fast',function(){
  				checkSBSizeOpen();
				});
	}else 
	if (/\/cpg/.test(window.location)) {
   		  jQuery("span:contains('CPG')").parent().find('ul').slideDown('fast',function(){
  				checkSBSizeOpen();
				});
	}else 
	if (/\/electronics/.test(window.location)) {
   		  jQuery("span:contains('Electronics')").parent().find('ul').slideDown('fast',function(){
  				checkSBSizeOpen();
				});
	}else 
	if (/\/food-bev/.test(window.location)) {
   		  jQuery("span:contains('Food &amp; Bev')").parent().find('ul').slideDown('fast',function(){
  				checkSBSizeOpen();
				});
	}else 
	if (/\/flavors/.test(window.location)) {
   		  jQuery("span:contains('Flavors')").parent().find('ul').slideDown('fast',function(){
  				checkSBSizeOpen();
				});
	}else 
	if (/\/kids/.test(window.location)) {
   		  jQuery("span:contains('Kids')").parent().find('ul').slideDown('fast',function(){
  				checkSBSizeOpen();
				});
	}else 
	if (/\/music/.test(window.location)) {
   		  jQuery("span:contains('Music')").parent().find('ul').slideDown('fast',function(){
  				checkSBSizeOpen();
				});
	}else 
	if (/\/online/.test(window.location)) {
   		  jQuery("span:contains('Online')").parent().find('ul').slideDown('fast',function(){
  				checkSBSizeOpen();
				});
	}else 
	if (/\/pets/.test(window.location)) {
   		  jQuery("span:contains('Pets')").parent().find('ul').slideDown('fast',function(){
  				checkSBSizeOpen();
				});
	}else 
	if (/\/retail/.test(window.location)) {
   		  jQuery("span:contains('Retail')").parent().find('ul').slideDown('fast',function(){
  				checkSBSizeOpen();
				});
	}else 
	if (/\/socially-minded/.test(window.location)) {
   		  jQuery("span:contains('Socially Minded')").parent().find('ul').slideDown('fast',function(){
  				checkSBSizeOpen();
				});
	}
} 
jQuery(checkURLexpand);



//Append social icons to any #primaray sidebar.
function add_social_emw(){

		var code2append = '<div id="sn">' +
		'<a href="http://twitter.com/eatmywords" target="_blank"><img alt="twitter" style="margin: 0pt 10px 0pt 0pt;" class="alignleft" src="http://66.147.244.210/~eatmywor/emw/wp-content/themes/emw/images/twitter.png"></a>' +
		'<a href="http://www.facebook.com/pages/Eat-My-Words-Super-Sticky-Consumer-Brand-Names-Taglines-To-Go-Yum/201280232690" target="_blank"><img alt="facebook" style="margin: 0pt 10px 0pt 0pt;" class="alignleft" src="http://66.147.244.210/~eatmywor/emw/wp-content/themes/emw/images/facebook.png"></a>' +
		'<a href="http://www.linkedin.com/company/1471096?trk=pro_selfown_cmpy" target="_blank"><img alt="linkedin" style="margin: 0pt 10px 0pt 0pt;" class="alignleft" src="http://66.147.244.210/~eatmywor/emw/wp-content/themes/emw/images/linkedin.png"></a>' +
	'</div>';
		
	if (jQuery('[className^="primary"]')) {
		jQuery('.primary-home').append(code2append);
	} 
	if (jQuery('[id^="primary"]')) {
		jQuery('#primary').append(code2append);
	}
}
add_social_emw();

//prepare the lightbox for the form terms..

	
});


