<?php

/***************************************************************************************/
/*	SURFLINE.COM  */
/***************************************************************************************/
//Point to XML File
$feed = "http://feeds.feedburner.com/surfline-rss-surf-report-rhode-island";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $feed);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$feed = curl_exec($ch);

	

//Create Object of SimpleXMLElement Class
//$sxml = simplexml_load_file($feed);
$sxml = simplexml_load_string($feed);
$surfline_title = $sxml->channel->title;
$surfline_build_date = $sxml->channel->lastBuildDate;
/*
date_default_timezone_set('GMT');
$current_time_GMT = date('G:i:s');
echo $current_time_GMT . "<br />";
*/
$date = date('Y-m-d G:i', strtotime($surfline_build_date));
//$date = "2010-10-15 19:14";
$time_since_build = timeSince($date); // 2 days ago
//PULL APART DATE

function timeSince($date)
{
    if(empty($date)) {
        return "No date provided";
    }
   
    $periods         = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
    $lengths         = array("60","60","24","7","4.35","12","10");
   
    $now             = time();
    $unix_date       = strtotime($date);
    
    // check validity of date
    if(empty($unix_date)) {   
        return "Bad date";
    }

    // is it future date or past date
    if($now > $unix_date) {   
        $difference     = $now - $unix_date;
        $tense         = "ago";
       
    } else {
        $difference     = $unix_date - $now;
        $tense         = "from now";
    }
   
    for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
        $difference /= $lengths[$j];
    }
   
    $difference = round($difference);
   
    if($difference != 1) {
        $periods[$j].= "s";
    }
   
    return "$difference $periods[$j] {$tense}";
}
    

$surfline_link = $sxml->channel->link;
//iterate through array items and search for Matunuck
$i = 1;
foreach ($sxml->channel->item as $item){
        $i++;
        $title = $item->title;
        $short_title = explode(':', $title);
        $strtemp_odd = "<span class=\"odd\" ><a href=\"$item->link\">" . $short_title[0] . "</a><p>$item->description</p></span>";
        $strtemp_even = "<span class=\"even\" ><a href=\"$item->link\">" . $short_title[0] . "</a><p>$item->description</p></span>";    
        if (($i % 2)==0){
            echo $strtemp_even;
        }
        else{
            echo $strtemp_odd;            
        }
        /*
        //See if Item Title Contains "Matunuck"
        $title = $item->title;
        $find = "MATUNUCK";
        
        if (strlen(strstr(strtolower($title),strtolower($find)))>0) {
                // string $find found in $title
                $matunuck_title = $item->title;
                $matunuck_report = str_replace("MATUNUCK : ", "", $matunuck_title);
                $matunuck_description = $item->description;
                $matunuck_pubDate = $item->pubDate;
                $matunuck_link = $item->link;
        }
        else {
                //Other RI Items
                //echo $strtemp;
        }
        */
}

?>