/*Hides cf (server side) only built table
 *Gets region data as JSON and builds client side table 
 * fades in client side table
 * 
 */



$(document).ready(function(){
});//moved the function oustide $(document).ready(){}; becuase i could not call it from HTML or flash (coming up function undefined)
	


function getRegion(regionId)
         {
        	
        	 $("#loading").ajaxStart(function(){
        		   $(this).show();
        		 });

        	 $("#loading").ajaxStop(function(){
      		   $(this).hide();
      		 });

        	 $.getJSON('/model/franchisors.cfc?method=getBasicAdvertiserDetailsJSON&regionId=' + regionId, function(data)
			 {
		      var region = data.REGION;
		      var regionDescription = data.REGIONDESCRIPTION;
		      var articlesExist = data.ARTICLESEXIST;
		      
		      var articlesExistHTML = ''; 
		      
		      //If region has related articles then build link
		      if(articlesExist)
		      {
		    	 articlesExistHTML += '<a href="index.cfm?event=regionalArticles&regionId=' + regionId + '">Read ' + region + ' related franchise articles</a>';
		      }
		      
		      //change page title
		      var pageTitle = 'Franchising in ' + region;
		      document.title = pageTitle;
		      
		      var regionInfo = '<h2>Franchising in ' + region + '</h2><P>' + regionDescription + '</p>';
		      
		     jsTable = '<table id="jsBuilt" width="100%"  cellspacing="0" cellpadding="0" class="companyTable"><thead><tr><th id=""></th><th id=""></th><th id=""></th></tr></thead><body>';
			  
		      //start first row
		      jsTable += '<tr>';
		      //need one outer loop with 3 seperate inner loops
		      $.each(data.FRANDETAILS.DATA, function(intIndex, objValue){
		      
		    	  //dividing index by 3 (i.e the number of cells we want in a row) will every 3 loops give a whole number. 
		    	  //We can check this number to see if loop is at an interval of 3. 
                  var isWholeNumber = (intIndex / 3);
		    	  
		    	/*
		    	 * if not a whole number build a <td></td> else
		    	 * every whole number (every 3 three loops) build a new row and a new <td></td>, 
		    	 * because building a new row only will cause a record to be missed. As building a new row is counted as a loop by $.each()
		    	 */
		    	  
		    	  if (isWholeNumber != parseInt(isWholeNumber))
		    	  {
		    		  //alert(isWholeNumber + ' is not a whole number');
		    		  jsTable += '<td width="33%"><a href="index.cfm?event=franchise&franchisorId=' + objValue[0] + '"><img src="images/category_logos/' + objValue[3] +  '" alt="' + objValue[3] + '" border="0"/></a>' + '<h4>' + objValue[2] + '</h4><p>' + objValue[1] + '<br><a href="index.cfm?event=franchise&franchisorId=' + objValue[0] + '" class="highlight">Read more </a></p></td>';
		    	  }
		    	  else
		    	  {  
		    		  //alert(isWholeNumber);
		    		  jsTable += '</tr><tr>' + '<td width="33%"><a href="index.cfm?event=franchise&franchisorId=' + objValue[0] + '"><img src="images/category_logos/' + objValue[3] +  '" alt="' + objValue[3] + '" border="0"/></a>' + '<h4>' + objValue[2] + '</h4><p>' + objValue[1] + '<br><a href="index.cfm?event=franchise&franchisorId=' + objValue[0] + '" class="highlight">Read more </a></p></td>';
		    	  }
		    	
		   
		     });
		      
		      //end last row and table 
		     jsTable += '</tr></tbody></table>';
			 
		     //hide server side built table and fade in client side built table.
		     
		     //If region has related articles then build link
		     
		      $('#regionArticlesLink').hide(); 
		     if(articlesExist)
		      {
		    	 	  
		    	  $('#regionArticlesLink').empty().append(articlesExistHTML).hide().fadeIn('slow');
		      }
		      
		     
		     $('#serverSide').hide();
		     $('#serverSideRegionInfo').hide();
		     $('#displayRegionInfo').empty().append(regionInfo).hide().fadeIn('slow');
			 $('#display').empty().append(jsTable).hide().fadeIn('slow');
		
			
			 
			 });
	
	        	        
	    };	
       //temp link - had used when not using flash map - they poit to commented out code on dspRegional
	   //$('#nsw').click(function(){getRegion(1);}); 
	   //$('#act').click(function(){getRegion(2);}); 
	   //$('#vict').click(function(){getRegion(3);});
	   //$('#queens').click(function(){getRegion(4);});
	   //$('#south').click(function(){getRegion(5);});
	   //$('#nt').click(function(){getRegion(6);});
	   //$('#wa').click(function(){getRegion(7);});
	   //$('#tas').click(function(){getRegion(8);});



