// Engagement Measurement Functionality
// (c) Satama Interactive 2007
// 2007-09-20
//
// Do not use special characters like quotes or double quotes in varible values if possible
// All variables necessary can be set in one segment below. Read the descriptions carefully
//
// Note: For engagement level to rise, both conditions must match. For example a visitor with over 10 pageviews, but
// only 160 seconds on site will still be in "Medium" engagement category.
//
// For additional segments, please contact Satama
//
/* Configuration start */
var urchinSegmentActive = 'true'; // Enable engagement segmentation, set 'true' to enable
var urchinSegment1Name = 'Engagement Index Light'; // Name of segment one (as shown in GA)
var urchinSegment1TimeLimit = '60' // Minimum value for segment one (in seconds)
var urchinSegment1PageLimit = '3' // Minimum value for segment one (in pages)
/*var urchinSegment2Name = 'Engagement Index Medium'; // Name of segment two (as shown in GA)
var urchinSegment2TimeLimit = '180' // Minimum value for segment two (in seconds), all values larger than this are part of segment two
var urchinSegment2PageLimit = '5' // Minimum value for segment two (in pages), all values larger than this are part of segment two
var urchinSegment3Name = 'Engagement Index Heavy'; // Name of segment three (as shown in GA)
var urchinSegment3TimeLimit = '300' // Minimum value for segment three (in seconds), all values larger than this are part of segment three
var urchinSegment3PageLimit = '10' // Minimum value for segment three (in pages), all values larger than this are part of segment three*/
var urchinSitePath = '/'; // Path on site where to apply, default '/' for the whole site
/* Configuration end 

 Code section, no need to modify below this line */
function urchinGetCookie(name) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1; 
	if ((!start) &&(name!=document.cookie.substring(0, name.length))){
		return null;
	}
	if (start == -1) return null;
	var end = document.cookie.indexOf( ";", len );
	if (end == -1) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

function urchinSegment(){
  if(urchinSegmentActive == "true") {
    var currentPageViews = new Number(urchinGetCookie("__utmsgc"));
    var timeSpent = new Number(urchinGetCookie("__utmsgs"));
    var timeToday = new Date();
    var expires = 1; // By default we do want the cookie to timeout, default 30 minutes
    var timeExpire = new Date();
    var tmpDomain = document.domain.split(".");
    if(document.domain.search(".co.") == -1) {
      tmpDomain = tmpDomain.slice(-2);
    } else {
      tmpDomain = tmpDomain.slice(-3);
    }
    urchinDomain = tmpDomain.join(".");
    timeExpire.setTime(timeToday.getTime() + 630720000000);
    
    if(currentPageViews&&currentPageViews>0)
    {
      currentPageViews = currentPageViews + 1;
    } else {
      currentPageViews = 1;
    }
    
    if(!urchinGetCookie("__utmsgs")) {
      var currentTimeSpent = 1;
    } else {
      var currentTimeSpent = new Number(parseInt((timeToday.getTime() - timeSpent)/1000));
    }
    
    document.cookie = "__utmsgc=" +escape(currentPageViews) +
    ( ( expires ) ? ";expires=" + timeExpire.toUTCString() : "" ) + 
    ( ( urchinDomain ) ? ";domain=" + urchinDomain : "" ) +
      ( ( urchinSitePath ) ? ";path=" + urchinSitePath : "/" );
    
    if (!urchinGetCookie("__utmsgs")) {
      document.cookie = "__utmsgs=" +escape(timeToday.getTime()) +
      ( ( expires ) ? ";expires=" + timeExpire.toUTCString() : "" ) + 
      ( ( urchinDomain ) ? ";domain=" + urchinDomain : "" ) +
      ( ( urchinSitePath ) ? ";path=" + urchinSitePath : "/" );
    } 
    // Modified to only use segment1 27.09.2007
    if((currentPageViews >= (urchinSegment1PageLimit)) && (currentTimeSpent >= urchinSegment1TimeLimit)) {
      /*if((currentTimeSpent > urchinSegment3TimeLimit) && (currentPageViews >= (urchinSegment3PageLimit)))
      {
        segmentGroup = urchinSegment3Name;
      } else if((((currentTimeSpent >= urchinSegment1TimeLimit) && (currentTimeSpent <= urchinSegment2TimeLimit)) && ((currentPageViews >= urchinSegment1PageLimit) && (currentPageViews <= urchinSegment2PageLimit))) || (((currentTimeSpent >= urchinSegment1TimeLimit) && (currentTimeSpent <= urchinSegment2TimeLimit)) || ((currentPageViews >= urchinSegment1PageLimit) && (currentPageViews <= urchinSegment2PageLimit))))	{
        segmentGroup = urchinSegment1Name;
      } else {
        segmentGroup = urchinSegment2Name;
      }*/
      segmentGroup = urchinSegment1Name;
      __utmSetVar('Segment:'+segmentGroup);
    } 
    
  }
}
