/*
	# Copyright 2009, Hoop Associates Ltd
	# Hoop Associates   www.thisishoop.com	 mail@hoopassociates.co.uk
*/

String.prototype.text = function(){
	var str = this;
	try { str = decodeURIComponent(str); } catch(e) {}
	str = (str && str.length) ? $.trim(str.replace(/<\S[^><]*>/g, '')) : '';
	return str;
};

String.prototype.safeEscape = function(){
	return encodeURIComponent($.trim(this)).replace(/%20/g, "+").toString();
};

var Cookie = {

	// default vars
	config : {
		name : 'pepsico_closed_cats',	// cookie name
		path : '/',			// cookie path
		delimiter : ',',		// its a csv string to hold multiple values
		expiredays : 1			// life of cookie in days
	},

	_set : function(name, val, expiredays){
		expiredays = expiredays || this.config.expiredays;
		var exdate=new Date();
		exdate.setDate(exdate.getDate()+expiredays);
		document.cookie = name+"="+escape(val)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString())+";path="+this.config.path;
	},

	_get : function(name){
		if (document.cookie.length > 0){
			var start = document.cookie.indexOf(name+"=");
			if (start != -1){
				start = start+name.length+1;
				var end = document.cookie.indexOf(";", start);
				if (end == -1){
					end = document.cookie.length;
				}
				return unescape(document.cookie.substring(start, end));
			}
		}
		return '';
	},

	addCat : function(cat_year){
		var ids = this._get(this.config.name).split(this.config.delimiter);

		// check id doesn't already exist in cookie list
		for(var i=0; i<ids.length; i++) if (ids[i] == cat_year) return; 
		// add parent id list
		ids.push(cat_year);
		// save csv string to cookie
		this._set(this.config.name, ids.join(this.config.delimiter));
	},

	removeCat : function(cat_year){
		var self = this, newids = [], 
		years = this._get(this.config.name).split(this.config.delimiter);

		// remove pages from cookie list 
		for(var i=0; i<years.length; i++) {
			(years[i] != cat_year) && newids.push(years[i]);
		}
		// save csv string to cookie
		this._set(this.config.name, newids.join(this.config.delimiter));
	}
};

var page = {

	init : function(){
		this.fixBugs();
		this.buildNav("nav");
		this.equalHeights();
	},

	fixBugs : function(){
		// FF2/Mac Opacity Bug
		($.browser.mozilla && parseFloat($.browser.version) < 1.9 && navigator.appVersion.indexOf('Mac') !== -1) && $('body').css('-moz-opacity',.999);
		if ($.browser.msie) {
			// IE6 background css flickering bug
			try{document.execCommand('BackgroundImageCache', false, true);} 
			catch(e){};
			// IE6 transaprent PNG background fix
			if (/MSIE ((5\.5)|6)/i.test(navigator.userAgent) && navigator.platform == "Win32"){
				$(".iecsspng").each(function(){
					var bgIMG = $(this).css('background-image');
					if (bgIMG.indexOf(".png")!=-1){
						var iebg = bgIMG.split('url("')[1].split('")')[0];
						$(this).css('background-image', 'none').css('display', 'block');
						$(this).get(0).runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + iebg + "',sizingMethod='scale')";
					}
				});
			}
		}
	},

	buildNav : function(elem){
		var span, li;
		$("#" + elem + " ul li").each(
		function(){
			if ($("ul:first", this).length) {
				var 
					ch = $("ul:first", this).css("display") != 'none' ? '&ndash;' : '+', 
					li = this;
				$("a:first", li).prepend('<span class="exp">' + ch + '&nbsp;</span>');
				$('span:first', this).click(function(){
					span = this;
					$("ul:first", li).animate({
						height: 'toggle'
					}, 200, function(){
						$(span).html(($(this).css("display") == "none") ? "+&nbsp;" : "&ndash;&nbsp;");
					});
					return false;
				});
			}
		});
	},

	equalHeights : function(){
		var $cols = $(".fb_container"), resizeTimer = null;
		if ($cols.length && $().equalHeights) {
			$cols.equalHeights(".fb_content", true);		
			$(window).bind('resize', function() {
				(resizeTimer) && clearTimeout(resizeTimer);
				resizeTimer = setTimeout(function() {
					$cols.equalHeights(".fb_content", true); 
				}, 100);
			});
		}
	}
}

$(function(){
	page.init();
});
