(function($){
	$.fn.bundles = function(options) {
			
		var defaults = {
			contentArea: $('#right-content')
			,offerHeaderText: ''
			,logoPath: '/bundles/images/logos/'
			,buyBtn: '/bundles/images/btn_buy_online.gif'
			,offertype: '3P'
		};

		var options = $.extend(defaults, options);
   		
		var contentArea = $(options.contentArea);
		
		if(options.offertype=='3P'||options.offertype=='2P'){
			contentArea.append('<ul class="tabNavigation"><li id="tp_tab"><a href="#3P">Triple Play</a></li><li id="dp_tab"><a href="#2P">Double Play</a></li></ul>');
			$('ul.tabNavigation li a').bind('click', function() {
				loadContent(this.hash);
			});
		} else {
			contentArea.append('<div id="offerHeaderText">'+options.offerHeaderText+'</div>');
		}
		
		contentArea.append('<div id="offers"><div id="secondaryOffers"></div></div>')
			
		loadContent(options.offertype);
		
		function loadContent(offertype) {
			$("#offers").empty();
			var offertype;
			var selectedTab;
			
			offertype = offertype.replace("#", "");
			
			if(options.offertype=='3P'||options.offertype=='2P'){
				$("ul.tabNavigation li").removeClass("active_tab");
				if(offertype=='3P') {
					selectedTab = $("#tp_tab");
				} else if (offertype =='2P') {
					selectedTab = $("#dp_tab");
				}
				selectedTab.addClass('active_tab');
			}
				
			$.ajax({
					type: "Post",
					url: "/bundles/php/data.php",
					dataType: "json",
					data: "offertype="+offertype,
					success: function(data){			
						var logoPath = options.logoPath;
						var offerHeaderLogo;
						var offerContent;
						var secondaryOfferContent = '';
						var content;
						
						if(offertype=='3P'){
							offerHeaderLogo = "/bundles/images/triple_play_image.gif";
							offerHeaderText = "Combine High Speed Internet,<br />Phone and Television";
							secondaryText = "triple play";
						}else if(offertype=='2P'){
							offerHeaderLogo = "/bundles/images/double_play_image.gif";
							offerHeaderText = "Build your own bundle...<br />combine any two services";
							secondaryText = "double play";
						} else if(offertype=='H') {
							offerHeaderLogo = "/bundles/images/big_internet.gif";
							offerHeaderText = "Find a Deal;<br />Browse the Web for Less";
							secondaryText = "high speed internet";
						} else if(offertype=='P') {
							offerHeaderLogo = "/bundles/images/big_phone.gif";
							offerHeaderText = "Unlimited Local & U.S. Long Distance + Calling Features Included in Most Plans";
							secondaryText = "home phone service";
						} else if(offertype=='T') {
							offerHeaderLogo = "/bundles/images/big_tv.gif";
							offerHeaderText = "Special Offers from Cable TV providers, Dish Network, DIRECTV and Phone Companies";
							secondaryText = "television service";
						}  else if(offertype=='M') {
							offerHeaderLogo = "/bundles/images/big_internet.gif";
							offerHeaderText = "Special Offers from Cable Companies";
							secondaryText = "cable modem service";
						} else if(offertype=='D') {
							offerHeaderLogo = "/bundles/images/big_internet.gif";
							offerHeaderText = "Special DSL Offers from Phone Companies";
							secondaryText = "DSL service";
						}
							
						offerContent = '<div id="offersHeader"><img src="'+offerHeaderLogo+'"><span>'+offerHeaderText+'</span></div>';
							
						$.each(data, function(i,val) {
						
							if(offertype=='M' || offertype=='D') {
								offerContent += '<div class="primaryOffer"><a href="'+val[3]+'"><img class="company_logo" src="'+logoPath+val[0]+'" alt="'+val[1]+'" title="'+val[1]+'"/></a><div class="priceinfo"><ul>'+val[2]+'</ul><img src="/bundles/images/green_arrow.gif" width="6" height="9" /><span><a href="'+val[3]+'">MORE</a></span></div><a href="'+val[3]+'"><img class="btn" src="'+options.buyBtn+'" width="71" height="23" /></a></div>';
							} else {
							
								if(val[4] == "P") {
									offerContent += '<div class="primaryOffer"><a href="'+val[3]+'"><img class="company_logo" src="'+logoPath+val[0]+'" alt="'+val[1]+'" title="'+val[1]+'"/></a><div class="priceinfo"><ul>'+val[2]+'</ul><img src="/bundles/images/green_arrow.gif" width="6" height="9" /><span><a href="'+val[3]+'">MORE</a></span></div><a href="'+val[3]+'"><img class="btn" src="'+options.buyBtn+'" width="71" height="23" /></a></div>';
								} else {
									secondaryOfferContent += '<a href="'+val[3]+'"><img src="'+logoPath+val[0]+'" alt="'+val[1]+'" title="'+val[1]+'"/></a>';
								}
								
							}
							
						});	
						
						if(secondaryOfferContent!=''){
							content = offerContent + '<div id="secondaryOffers"><span>Looking for a different provider? Check other '+secondaryText+' offers...</span>' + secondaryOfferContent + '</div>';
						} else {
							content = offerContent;
						}
						
						$("#offers").html(content);
						
					}
				});	
			}
		
			//contentArea.append('<div id="tripleSecondary" class="dots"><span>Looking for a different provider? More Triple Play offers:</span><div>');
			//contentArea.append('<div style="clear:both"></div>');
			
			
			
			
			//contentArea.append('<div id="triplePlay"><img src="/bundles/images/triple_play_image.gif"><div class="playText">Combine High Speed Internet,<br />Phone and Television</div><div id="tripleOffers"></div><div id="tripleSecondary" class="dots"><span>Looking for a different provider? More Triple Play offers:</span><div></div><div style="clear:both"></div></div></div>');
	
			//contentArea.append('<div id="doubleOffers"></div>');
			//contentArea.append('<div id="doubleSecondary" class="dots"><span>Looking for a different provider? More Double Play offers:</span><div>');
			
	
			//ontentArea.append('<div id="doublePlay"><img src="/bundles/images/double_play_image.gif"><div class="playText">Build Your Own Bundle;<br />Combine Any Two Services</div><div id="doubleOffers"></div><div id="doubleSecondary" class="dots"><span>Looking for a different provider? More Double Play offers:</span><div></div><div style="clear:both"></div></div></div>');
			
			//var tabContainers = $('#right-content > div');
		
			//$('#right-content ul.tabNavigation a').click(function () {
			//	tabContainers.hide().filter(this.hash).show();
			//	return false;
			//}).filter(':first').click();
			
		
		//else if(options.page=='sub') {
		//	contentArea.append('<div id="offerHeader">'+options.offerHeaderText+'</div>');
		//	contentArea.append('<div id="offers"></div>');
		//$('#offers').append('<div id="primaryOffers"></div>');
		//$('#offers').append('<div id="secondaryOffers" class="dots"><span>Looking for a different provider? More '+options.secondaryOfferText+' offers:</span></div>');
		//}
		};
})(jQuery);

