$(document).ready(function() {
	$('.couponSPpup').popupWindow({ 
	centerBrowser:1,
	resizable:1,
	scrollbars:1,
	height:700,
	width:490
	});
	$('.couponPup').popupWindow({ 
	centerBrowser:1,
	toolbar:1,
	resizable:1,
	scrollbars:1,
	menubar:1 ,
	height:650,
	width:$(document).width()-50
	});  							 
    var m_names = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
    var d = new Date();
    var curr_date = d.getDate();
    var curr_month = d.getMonth();
    var curr_year = d.getFullYear();
    var display_date = m_names[curr_month] + " " +curr_date + ", " + curr_year;
    $('.national-weather .current-date').text(display_date);
    //$('.facebook-cta p').truncate(55);
    //$('.twitter-cta p').truncate(55);
    $(".popular-categories li a.more").click(function(){
        $(".additional-categories").fadeIn();
        return false;
    });
    $("popular-categories li.more a").click(function(){
        $(".additional-categories").fadeOut();
        return false;
    });
	$(".additional-categories #category-list a.close").click(function(){
        $(".additional-categories").fadeOut();
        return false;
    });
    var weatherSearchValue = $("#weathercity").attr("title");
    $("#weathercity").val(weatherSearchValue);
    $("#weathercity").focus(function(){
        if ($("#weathercity").val() == weatherSearchValue) {
            $("#weathercity").val('');
        }
    });
    $("#weathercity").blur(function(){
        if ($("#weathercity").val() == '') {
            $("#weathercity").val(weatherSearchValue);
        }
    });

    $('#CoupListing li').live('mouseenter', function() {
        pin_number = $(this).attr('class').substring(3);
        spmap_hlPin(pin_number);
		$(this).css("background-color","#DBEBF2");
       // $(this).addClass('over');
    }).live('mouseleave', function() {
	$(this).css("background-color","#FFF");
        spmap_hlPin(0);
      //  $(this).removeClass('over');
    });

	$(".popular-categories-ft a.morepc").click(function(){
	if ($('.additional-categories-ft').length > 0){
  // div with id divID is present 
	var p = $(".popular-categories-ft a.morepc");
	var position = p.position();
        $(".additional-categories-ft").css( { 
                position: 'absolute',
                zIndex: 5000,
                left: position.left - 70, 
                top: position.top - 0
	});
	$(".additional-categories-ft").fadeIn();
        return false;
    
	}
	else{
  		return;
	}
	});
    $(".additional-categories-ft #category-list a.close").click(function(){
        $(".additional-categories-ft").fadeOut();
        return false;
    });
	$(function () {
			var tabContainers = $('div#tabs > div');
			tabContainers.hide().filter(':first').show();
			$('div#tabs ul.tabs a').click(function () {
				tabContainers.hide();
				tabContainers.filter(this.hash).show();
				$('div#tabs ul.tabs a').removeClass('activelft');
				$('div#tabs ul.tabs li').removeClass('active');
				$(this).addClass('activelft');
				$(this).parent('li').addClass('active');
				return false;
			}).filter(':first').click();
	});
	
    kw();
	facebookContent();
	twitterContent();
});

	
function getDefimg(t){
 t.src = 'http://img.superpages.com/images-yp/sp/images/bp/gray_dollar_sign.png';  
 }


function getpage(page,usediv) {
 
    // Set up request varible
    try {
        xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
    }
	  
    catch (e) { 
        alert("Error: Could not load page.");
    }
    //Show page is loading
    //document.getElementById(usediv).innerHTML = ' Loading...';
    //scroll to top
    scroll(0,0);
    xmlhttp.onreadystatechange = function(){
        //Check page is completed and there were no problems.
        // alert('xmlhttp readyState '+xmlhttp.readyState);
        // alert('xmlhttp status '+xmlhttp.status);
        if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
            //Write data returned to page
            document.getElementById(usediv).innerHTML = xmlhttp.responseText;
            newMapPins();
        }
    }
    xmlhttp.open("GET", page);
    xmlhttp.send(null);
    //Stop any link loading normaly
    return false;
}

function newMapPins() {
    var a = new Array();
    var poi = "";
    var mappin = "";
    if (getcookie("POI1") != null && getcookie("POI1") != ""){
        poi = getcookie("POI1").split("|");
        a.push([parseFloat(poi[0]),parseFloat(poi[1]),"1",1]);
    }
    poi = "";
    if (getcookie("POI2") != null && getcookie("POI2") != ""){
        poi = getcookie("POI2").split("|");
        a.push([parseFloat(poi[0]),parseFloat(poi[1]),"2",1]);
    }
    poi = "";
    if (getcookie("POI3") != null && getcookie("POI3") != ""){
        poi = getcookie("POI3").split("|");
        a.push([parseFloat(poi[0]),parseFloat(poi[1]),"3",1]);
    }
    poi = "";
    if (getcookie("POI4") != null && getcookie("POI4") != ""){
        poi = getcookie("POI4").split("|");
        a.push([parseFloat(poi[0]),parseFloat(poi[1]),"4",1]);
    }
    spmap_LoadPIOs(a);
}

function changeValue(selectBox,T,S) {
    selectedItem = selectBox[selectBox.selectedIndex];
    var cp1 = '/CouponListings.php?cpurl='+selectedItem.value+'&popCat='+escape(selectedItem.text)+'&T='+T+'&S='+S;
    getpage(cp1,'CoupListing');
}

function hlListing(n) {
    $('#CoupListing li.over').removeClass('over');
    $('#CoupListing li#listing'+n).addClass('over');
}

function facebookContent(){
$.ajax({url : "/spsocial_feed.php?sc=fb",success : function (data) {$("#facebookCnt").html(data);}});
}
function twitterContent(){
$.ajax({url : "/spsocial_feed.php?sc=tw",success : function (data) {$("#twitterCnt").html(data);}});
}

(function($){ 		  
	$.fn.popupWindow = function(instanceSettings){
		
		return this.each(function(){
		
		$(this).click(function(){
		
		$.fn.popupWindow.defaultSettings = {
			centerBrowser:0, // center window over browser window? {1 (YES) or 0 (NO)}. overrides top and left
			centerScreen:0, // center window over entire screen? {1 (YES) or 0 (NO)}. overrides top and left
			height:600, // sets the height in pixels of the window.
			left:0, // left position when the window appears.
			location:0, // determines whether the address bar is displayed {1 (YES) or 0 (NO)}.
			menubar:0, // determines whether the menu bar is displayed {1 (YES) or 0 (NO)}.
			resizable:0, // whether the window can be resized {1 (YES) or 0 (NO)}. Can also be overloaded using resizable.
			scrollbars:0, // determines whether scrollbars appear on the window {1 (YES) or 0 (NO)}.
			status:0, // whether a status line appears at the bottom of the window {1 (YES) or 0 (NO)}.
			width:600, // sets the width in pixels of the window.
			windowName:null, // name of window set from the name attribute of the element that invokes the click
			windowURL:null, // url used for the popup
			top:0, // top position when the window appears.
			toolbar:0 // determines whether a toolbar (includes the forward and back buttons) is displayed {1 (YES) or 0 (NO)}.
		};
		
		settings = $.extend({}, $.fn.popupWindow.defaultSettings, instanceSettings || {});
		
		var windowFeatures =    'height=' + settings.height +
								',width=' + settings.width +
								',toolbar=' + settings.toolbar +
								',scrollbars=' + settings.scrollbars +
								',status=' + settings.status + 
								',resizable=' + settings.resizable +
								',location=' + settings.location +
								',menuBar=' + settings.menubar;

				settings.windowName = this.name || settings.windowName;
				settings.windowURL = this.href || settings.windowURL;
				var centeredY,centeredX;
			
				if(settings.centerBrowser){
						
					if ($.browser.msie) {//hacked together for IE browsers
						centeredY = (window.screenTop - 120) + ((((document.documentElement.clientHeight + 120)/2) - (settings.height/2)));
						centeredX = window.screenLeft + ((((document.body.offsetWidth + 20)/2) - (settings.width/2)));
					}else{
						centeredY = window.screenY + (((window.outerHeight/2) - (settings.height/2)));
						centeredX = window.screenX + (((window.outerWidth/2) - (settings.width/2)));
					}
					window.open(settings.windowURL, settings.windowName, windowFeatures+',left=' + centeredX +',top=' + centeredY).focus();
				}else if(settings.centerScreen){
					centeredY = (screen.height - settings.height)/2;
					centeredX = (screen.width - settings.width)/2;
					window.open(settings.windowURL, settings.windowName, windowFeatures+',left=' + centeredX +',top=' + centeredY).focus();
				}else{
					window.open(settings.windowURL, settings.windowName, windowFeatures+',left=' + settings.left +',top=' + settings.top).focus();	
				}
				return false;
			});
			
		});	
	};
})(jQuery);


