var picnet = picnet || {};
picnet.visitorContext = {  
  entryPage:'',
  referrerPage:'',  
  // __utma exp after 2 years
  timeOfInitialVisit:'', // (__utma[2])
  timeOfPrevSession:'', // (__utma[3])
  timeOfCurrSession:'', // (__utma[4])
  numOfVisits:'', // (__utma[5])
  // __utmz exp after 6 months
  // numOfVisits:'', // (__utmz[2])
  // diffCampaignsNum: Inc everytime u arrive with different search or campaign
  diffCampaignsNum:'', // (__utmz[3]) 
  navSource:'', //__utmz[utmcsr]
  navSourceCampaign:'', //__utmz[utmccn]
  navSourceMedium:'', //__utmz[utmcmd]
  navSourceKeyword:'', //__utmz[utmctr]
  navSourceAdContent:'' //__utmz[utmcct]
};

picnet.visitorContext.toString = function() {
  var lines = [];
  lines.push('Visit Time: ' + picnet.visitorContext.timeOfCurrSession);
  lines.push('Num of times this user seen recently: ' + picnet.visitorContext.numOfVisits);
  lines.push('Num of diff campaigns used recently: ' + picnet.visitorContext.diffCampaignsNum);
  lines.push('Source: ' + picnet.visitorContext.navSource);
  lines.push('Campaign: ' + picnet.visitorContext.navSourceCampaign);
  lines.push('Medium: ' + picnet.visitorContext.navSourceMedium);
  lines.push('Search Keyword: ' + picnet.visitorContext.navSourceKeyword);
  lines.push('Ad Content: ' + picnet.visitorContext.navSourceAdContent);
  
  return lines.join('[|]');
};

(function() {
  var fullCookie = document.cookie;  
  var cookies = fullCookie.split(';');
  for (var i = 0, c; c = cookies[i++];) {
    c = trim(c);
    if (c.indexOf('__utmz=') === 0) {
      parseUtmzCookie(c.substring(7));
    } else if (c.indexOf('__utma=') === 0) {
      parseUtmaCookie(c.substring(7));
    } 
  }
  
  function parseUtmzCookie(utmz) {    
    var vals = utmz.split('.');
    var srcDetails = vals[4].split('|');
    
    picnet.visitorContext.diffCampaignsNum = vals[3];
    picnet.visitorContext.navSource = getUtmzVal(srcDetails, 'utmcsr');
    picnet.visitorContext.navSourceCampaign = getUtmzVal(srcDetails, 'utmccn');
    picnet.visitorContext.navSourceMedium = getUtmzVal(srcDetails, 'utmcmd');
    picnet.visitorContext.navSourceKeyword = getUtmzVal(srcDetails, 'utmctr');
    picnet.visitorContext.navSourceAdContent = getUtmzVal(srcDetails, 'utmcct');
  };
  
  function parseUtmaCookie(utma) {    
    var vals = utma.split('.');
    picnet.visitorContext.timeOfInitialVisit = getTimeFromSeconds(vals[2]);
    picnet.visitorContext.timeOfPrevSession = getTimeFromSeconds(vals[3]);
    picnet.visitorContext.timeOfCurrSession = getTimeFromSeconds(vals[4]);
    picnet.visitorContext.numOfVisits = vals[5];
  };
  
  function getUtmzVal(vals, key) {
    for (var i = 0, v; v = vals[i++];) {
      if (v.indexOf(key) === 0) {
        return v.substring(key.length + 1);
      }
    }
    return '';
  };
  
  function getTimeFromSeconds(seconds) {
    return new Date(seconds * 1000);
  }; 

  function trim(str) {
    return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
  };  
})();


// If the search keywords matches the regular expression, show the content (third parameter) in the dom object
// passed as second parameter. If it doesn't match the dom object will be hidden. Returns true if match.
picnet.showHideInIfMatchSearch = function(regexp, objToShowContentIn, objContainingContent) {
    if (!picnet.showInIfMatchSearch(regexp, objToShowContentIn, objContainingContent)) {
        objToShowContentIn.hide();
        return false;
    }
    return true;
};

// If the search keywords matches the regular expression the third parameter will be used, otherwise parameter four. 
// The content (parameter three or four) will be shown in the dom object passed as parameter two. Returns true if match.
picnet.showInIfMatchSearch = function(regexp, objToShowContentIn, objContainingContentIfTrue, objContainingContentIfFalse) {
    if (objContainingContentIfTrue == undefined && objContainingContentIfFalse == undefined) {
        objContainingContentIfTrue = objToShowContentIn;
    } 
    if (picnet.visitorContext.navSourceKeyword.match(regexp)) {
        objToShowContentIn.html(objContainingContentIfTrue.html());
        objToShowContentIn.show();
        return true;
    }
    else {
        if (objContainingContentIfFalse != null && objContainingContentIfFalse != undefined) {
            objToShowContentIn.html(objContainingContentIfFalse.html());
            objToShowContentIn.show();
        }
        return false; 
    }
};

// If the search keywords matches the regular expression the content of the objects will swap.
picnet.swapIfMatchSearch = function(regexp, objContainingContentOne, objContainingContentTwo) {
    var htmlOne = objContainingContentOne.html();
    if (picnet.visitorContext.navSourceKeyword.match(regexp)) {
        objContainingContentOne.html(objContainingContentTwo.html());
        objContainingContentTwo.html(htmlOne);
        return true;
    }
    else {
        return false; 
    }
};


// Examples
//================================================
// <div id="targetDiv"></div> 
//
// <div id="contentDivOne"></div>
// <div id="contentDivTwo"></div>
//
// // Example 1
// // Show the targetDiv if search keyword matches 'superman' ('display' on targetDiv is set to visible).
// // If no match, targetDiv is left as before ('display' on targetDiv is not changed)
// picnet.showInIfMatchSearch(/superman/, $('#targetDiv'));
//
// // Example 2
// // Show the content of the contentDivOne in the targetDiv if search keyword matches 'superman' ('display' on targetDiv is set to visible).
// // If no match, targetDiv is left as before ('display' on targetDiv is not changed)
// picnet.showInIfMatchSearch(/superman/, $('#targetDiv'), $('#contentDivOne'));
//
// // Example 3
// // Show the content of the contentDivOne in the targetDiv if search keyword matches 'superman'.
// // Otherwise show the content of contentDivTwo. 'display' on targetDiv is set to visible.
// picnet.showInIfMatchSearch(/superman/, $('#targetDiv'), $('#contentDivOne'), $('#contentDivTwo'));
//
//  // Example 4
// // Show the targetDiv if search keyword matches 'superman' ('display' on targetDiv is set to visible).
// // If no match, targetDiv 'display' on targetDiv is set to hide the div.
// picnet.showHideInIfMatchSearch(/superman/, $('#targetDiv'));
//
//  // Example 5
// // Show the content of the contentDivOne in the targetDiv if search keyword matches 'superman' ('display' on targetDiv is set to visible).
// // If no match, targetDiv 'display' on targetDiv is set to hide the div.
// picnet.showHideInIfMatchSearch(/superman/, $('#targetDiv'), $('#contentDivOne'));

//console.log(picnet.visitorContext.toString());
