<?php
function getDealSurfDeals() {
	$dealsurf_city_codes = array(
			'ABQ' => 'Albuquerque',
			'ADL' => 'Adelaide',
			'AIXX' => 'Aix-en-Provence',
			'ALB' => 'Albany',
			'ATL' => 'Atlanta',
			'AU' => 'National Australia',
			'AUS' => 'Austin',
			'BAK' => 'Bakersfield',
			'BHM' => 'Birmingham',
			'BHX' => 'Birmingham',
			'BKL' => 'Brooklyn',
			'BN' => 'Nashville',
			'BNE' => 'Brisbane',
			'BOD' => 'Bordeaux',
			'BOI' => 'Boise',
			'BOS' => 'Boston',
			'BRS' => 'Bristol',
			'BSH' => 'Brighton',
			'BUF' => 'Buffalo',
			'BWI' => 'Baltimore',
			'CAK' => 'Akron',
			'CDG' => 'Paris',
			'CEQ' => 'Cannes',
			'CHI' => 'Chicago',
			'CHS' => 'Charleston',
			'CLE' => 'Cleveland',
			'CLT' => 'Charlotte',
			'CMH' => 'Columbus',
			'COS' => 'Colorado Springs',
			'CVG' => 'Cincinnati',
			'CWL' => 'Cardiff',
			'DAL' => 'Dallas',
			'DAY' => 'Dayton',
			'DC' => 'Washington',
			'DEN' => 'Denver',
			'DET' => 'Detroit',
			'DFW' => 'Fort Worth',
			'DSM' => 'Des Moines',
			'DUB' => 'Dublin',
			'FAT' => 'Fresno',
			'FLL' => 'Fort Lauderdale',
			'FR' => 'National France',
			'FSD' => 'Sioux Falls',
			'FW' => 'Fort Wayne',
			'GL' => 'Glasgow',
			'GNB' => 'Grenoble',
			'GRR' => 'Grand Rapids',
			'GSO' => 'Greensboro',
			'HAR' => 'Hampton Roads',
			'HNL' => 'Honolulu',
			'HOU' => 'Houston',
			'ICT' => 'Wichita',
			'IND' => 'Indianapolis',
			'JAX' => 'Jacksonville',
			'JBK' => 'Berkeley',
			'JFK' => 'Queens',
			'LAS' => 'Las Vegas',
			'LAX' => 'Los Angeles',
			'LB' => 'Leeds',
			'LHR' => 'London',
			'LIDX' => 'Long Island',
			'LIL' => 'Lille',
			'LPL' => 'Liverpool',
			'LYN' => 'Lyon',
			'MAN' => 'Manchester',
			'MCI' => 'Kansas City',
			'MCO' => 'Orlando',
			'MEM' => 'Memphis',
			'MI' => 'Miami',
			'MKE' => 'Milwaukee',
			'MLB' => 'Melbourne',
			'MPL' => 'Montpellier',
			'MRS' => 'Marseille',
			'MSN' => 'Madison',
			'MSP' => 'Minneapolis',
			'MSY' => 'New Orleans',
			'NCE' => 'Nice',
			'NCL' => 'Newcastle',
			'NJYX' => 'North Jersey',
			'NTE' => 'Nantes',
			'NYC' => 'New York',
			'OKC' => 'Oklahoma City',
			'OM' => 'Omaha',
			'OOL' => 'Gold Coast',
			'PDX' => 'Portland',
			'PER' => 'Perth',
			'PHL' => 'Philadelphia',
			'PHX' => 'Phoenix',
			'PIT' => 'Pittsburgh',
			'PVD' => 'Providence',
			'RDU' => 'Raleigh-Durham',
			'RNS' => 'Rennes',
			'SAN' => 'San Diego',
			'SAT' => 'San Antonio',
			'SDF' => 'Louisville',
			'SE' => 'Seattle',
			'SFO' => 'San Francisco',
			'SJC' => 'San Jose',
			'SLC' => 'Salt Lake City',
			'SMF' => 'Sacramento',
			'SN' => 'Orange County',
			'STL' => 'St Louis',
			'SXB' => 'Strasbourg',
			'SYD' => 'Sydney',
			'TLS' => 'Toulouse',
			'TP' => 'Tampa',
			'TUL' => 'Tulsa',
			'TUS' => 'Tucson',
			'UK' => 'National UK',
			'URO' => 'Rouen',
			'US' => 'National USA',
			'YEG' => 'Edmonton',
			'YVR' => 'Vancouver',
			'YYC' => 'Calgary',
			'YYZ' => 'Toronto',
	);
	$dealsurf_categories = array(
			'sports-and-fun-activities'=> 'Sports & Fun Activities',
			'beauty-and-wellness'=> 'Beauty & Wellness',
			'cllothes-and-accessories'=> 'Clothes & Accessories',
			'education-and-training'=> 'Education & Training',
			'electronics'=> 'Electronics',
			'arts-and-entertainment'=> 'Arts & Entertainment',
			'labor-and-services'=> 'Labor & Services',
			'for-pets'=> 'For Pets',
			'products'=> 'Products',
			'food-and-drinks'=> 'Food & Drinks',
			'tickets'=> 'Tickets',
			'travel-and-getaways'=> 'Travel & Getaways'
	);
	$dealsurf_categories_map = array(
			'sports-and-fun-activities'=> 'ACTIV',
			'beauty-and-wellness'=> 'BEAU',
			'cllothes-and-accessories'=> 'CLOTH',
			'education-and-training'=> 'EDUC',
			'electronics'=> 'ELEC',
			'arts-and-entertainment'=> 'EVENT',
			'labor-and-services'=> 'LABOR',
			'for-pets'=> 'PETS',
			'products'=> 'PROD',
			'food-and-drinks'=> 'REST',
			'tickets'=> 'TICKS',
			'travel-and-getaways'=> 'TRAV'
	);

	$order_by = "rand()";
	$current_time = time();
	$criteria = " end_date > $current_time";
	$offset = 0;

	$ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
	$db = ConnectToRead();
	$envoy_result = EnvoyIp2location($ipaddress, $db);
	$city = $envoy_result['city'];
	$state = $envoy_result['state'];
	if($key = array_search(ucwords(str_replace("-", " ", $city)), $dealsurf_city_codes)) {
		$criteria .= " AND city_code='$key'";
	}
	$order_by = "end_date DESC";

	//$DealData = array();
	$sql = "SELECT * FROM dealsurf_data WHERE $criteria ORDER BY $order_by LIMIT $offset, 50";
	
	if(!$DealData){
		$ldb = ConnectToSlaveLP();
		$rs = mysql_query($sql, $ldb);
		if($count = mysql_num_rows($rs)>0) {
			$i=0;
			while ($row = mysql_fetch_assoc($rs)) {
				$row['end_date'] = date("d M Y", $row['end_date']);
				if(!strstr($row['image_url'], 'http://'))
					$row['image_url'] = 'http://statics.dealsurf.com'.$row['image_url'];
				$DealData[$i][id] = $row[id];
				$DealData[$i][title] = $row[title]==""?" ":$row[title];
				$DealData[$i][end_date] = $row[end_date]==""?" ":$row[end_date];
				$DealData[$i][link] = 'http://click.cheapstuff.com/auth.php?url='.urlencode($row[url]==""?" ":$row[url]);
				$DealData[$i][link_deal] = 'http://click.cheapstuff.com/auth.php?url='.urlencode($row[url]==""?" ":$row[url]);
				$DealData[$i][images] = $row[image_url]==""?"":$row[image_url];
				$DealData[$i][discount] = $row[discount]==""?" ":$row[discount];
				$DealData[$i][discount_value] = $row[discount_value]==""?" ":$row[discount_value];
				$DealData[$i][price_text] = $row[discount_value]==""?" ":$row[discount_value];
				$DealData[$i][original_value] = $row[original_value]==""?" ":$row[original_value];
				$DealData[$i][merchant] = $row[merchant]==""?" ":$row[merchant];
				$DealData[$i][primary_store_name] = $row[merchant]==""?" ":$row[merchant];
				$DealData[$i][provider] = $row[provider]==""?" ":$row[provider];
				$DealData[$i][description] = $row[description]==""?" ":$row[description]." ";
				$DealData[$i][city_code] = $row[city_code]==""?" ":$row[city_code];
				$DealData[$i][category] = $row[category]==""?" ":$row[category];
				$DealData[$i][currency] = $row[currency]==""?" ":$row[currency];
				$DealData[$i][avg_rating] = $row[avg_rating]==""?" ":$row[avg_rating];
				$DealData[$i][num_of_votes] = $row[num_of_votes]==""?" ":$row[num_of_votes];
				$i++;
			}
		}
		else
			$DealData = "none";
		mysql_close($ldb);
	}
	
	if(is_array($DealData))
		return $DealData;
	else
		return false;
}
?>