//var adsource = "http://adverts.skegness.com/getAdverts.php";
var domain = "http://adverts.skegness.com";
var adsource = domain + "/getAdverts.php";
var contactsource = domain + "/contact.php?id=";
var tracksource = domain + "/trackAdverts.php?id=";


jQuery.fn.decHTML = function() { 
  return this.each(function(){ 
    var me   = jQuery(this); 
    var html = me.html(); 
    me.html(html.replace(/&amp;/g,'&').replace(/&lt;/g,'<').replace(/&gt;/g,'>')); 
  }); 
}; 

function getAds(group,target,count)
{   
    
    if(!count) count = -1;
    
    
    $.getJSON(adsource+"?jsoncallback=?", {"group":group,"count":count}, function(data) { 

        $('#'+target).html(data.source.replace(/&amp;/g,'&').replace(/&lt;/g,'<').replace(/&gt;/g,'>').replace(/&quot;/g,'"').replace(/\//g,'/'));
        
    
    $('#'+target+ " a").each(function(){
        
        if($(this).attr('href').toLowerCase().indexOf("mailto")!=0)
        {
            var url = $(this).attr('href');
            var id = url.substring(url.lastIndexOf("#")+1);
	    var blnDisplayChange = $(this).text().lastIndexOf("#");
            url = url.substring(0,url.lastIndexOf("#"));
            $(this).attr('rel',id);
            $(this).attr('href',url)
;
	    if(blnDisplayChange>0)
	    {
	        $(this).text($(this).text().substring(0,$(this).text().lastIndexOf("#")));
	    }
        }
    });
    
    $('#'+target+ " a").click(function() {
        
        if($(this).attr('href').toLowerCase().indexOf("mailto")==0)
        {
            //hijack the click and display a dialog with an email form
        //    var url = $(this).attr('href');
            
        }   
        else
        {
            var id = $(this).attr('rel') ;

            $.ajax({
                url: tracksource+"?jsoncallback=?",
                dataType: 'json',
                data: {"id":id},
                success: function(){},
                async: false
                });
        }
                                   
    });
    
    });
}

function emailForm(id)
{
    $.modal('<iframe src="' + contactsource + id + '" width="100%" height="100%" style="border:0">', {
        closeHTML: "<a href='#' title='Close' class='modal-close modalCloseImg' id=''></a>", 
        containerCss:{
        backgroundColor:"#fff",
        borderColor:"#fff",
        height:335,
        padding:0,
        width:500
        },
        overlayClose:true
    });   
   return false;    
}

