/* Panasonic : LiHD Phase 2 ~ JAVASCRIPT CORE

	Author: Joel Nagy | dotglu
	Last Modified: December 8th, 2008 5:02:47 PM EST

*/

// Globals
window.mac = navigator.platform.indexOf('Mac') >= 0? true: false;

// Social Marking Globals
addthis_pub             = 'dotglu'; 
//addthis_logo            = 'http://www.addthis.com/images/yourlogo.png';
addthis_logo_background = 'EFEFFF';
addthis_logo_color      = '666699';
addthis_brand           = 'Panasonic Living in HD';
// delicious, digg, email, favorites, facebook, fark, furl, google, live, myweb, myspace, newsvine, reddit, slashdot, stumbleupon, technorati, twitter, more
addthis_options         = 'favorites, email, digg, delicious, myspace, facebook, google, live, more';
addthis_offset_left     = 80;

// COREMETRICS
if (livinginhd != null && __subZone != null)
	cm_Category = livinginhd.siteName+':'+livinginhd.zoneName+__subZone;
	else cm_Category = '';

function getDeeplink() {
	var hash = window.location.href.slice(window.location.href.indexOf('#')+1);
	return hash != window.location.href? hash.toLowerCase(): '';
}

// UTILS
function noop() { return; }

// TRACKING
function track(page, category, step, points, pageOnlyPrefix) {
	try {
		if (category == null)
			category = page.replace(/[, ]/g, '');

		// Set Production Environment: staging and stagginggw are covered
		if (document.location.href.indexOf('kbp') < 0)
			cmSetProduction(); // call before pageView (or similar tags to set Prod)
		
		// Page View
		cmCreatePageviewTag(page, category);

		// Conversion
		if (step != null && parseInt(step) >= 0 && points != null && parseInt(points) >= 0)
			cmCreateConversionEventTag(page, step, category, points);
		// Error
		else if (page.toLowerCase().indexOf("error") >= 0)
			cmCreateErrorTag(page, category);
	} catch (e) { }
}

function elementTrack() { // called in footer
	// track any element that needs something special
	var lnk = '';

	var $title = document.getElementById('title');

	// Videos (General)
	var $vid = document.getElementById('vid_flash_player_container');
	if ($vid != null && $title != null)
		lnk = '/video/'+ $title.innerHTML;

	if (lnk != '')
		pageTracker._trackPageview(lnk);
}

function googleClickTrack(section, title) { // called in footer
	var lnk = '';

	if (section != null && section != '' && title != null && title != '')
		lnk = '/'+ section +'/'+ title;
	if (lnk != '')
		pageTracker._trackPageview(lnk);
}

function googleClickTracking(parent, section) {
	var A = null;
	parent = document.getElementById(parent);
	if (parent != null) {
		A = parent.getElementsByTagName('A');
		if (A != null && A.length > 0)
			for (var i = 0, l = A.length; i < l; i++)
				if (A[i] != null) {
					var a = A[i];
					addEvent(a, 'click', function() { googleClickTrack(section, rmHTML(a.innerHTML) +' .'+ a.className); });
				}
	}
}

function getFlashData(obj, param) {
	var data = '';
	if (typeof(obj) == 'object') {
		$.each(obj, function(key, value) {
			value = value.toString();
			if (value.indexOf(param) == 0)
				data = value.slice(param.length+1);
		});
	}
	return data;
}

gTrackAction = null;
gTrackVideo = null;
function trackFlash(obj) {
	try { 
		var timePaused = obj['timePaused'];
		var timeEnded = obj['timeEnded'];
		var videoTime = parseInt(obj['videoTime']);
		var videoSeconds = parseInt(obj['videoTime']);
		videoTime -= videoTime % 10;
		var video = obj['videoName'];

		var page = obj['pageId']? obj['pageId']: obj['pageID']? obj['pageID']: '';
		var category = cm_Category; ///page.replace(/[, ]/g, '');
		var clickAction = obj['action'];
		var clickURL = obj['clickUrl'];
		var familyId = obj['familyID'];

	if (clickAction != null) { // Click Tracking
		if (clickURL != null)
			 clickAction += '='+ clickURL;
		if (familyId != null)
			page += '::'+ familyId;
		cmCreatePageviewTag(page +':'+ clickAction, category);
	} else if (video != null) { // Video Tracking
		if (gTrackVideo != video) {
			gTrackVideo = video; // NEW Video
			gTrackAction = null;
		}

		var action = '', points = 0;
		if (timePaused == 'NA' && timeEnded == 'NA' && parseInt(videoTime) == 0 && gTrackAction == null) {
			action = 'Started';
			points = 20;
			cmCreateConversionEventTag(video, 1, category, points);
		} else if (timePaused == 'NA' && timeEnded == 'NA' && gTrackAction == 'Paused') {
			action = 'Restarted';
			cmCreatePageviewTag(video +':'+ action, category);
		} else if (timePaused == 'NA' && timeEnded == 'NA' && parseInt(videoTime) > 0 && gTrackAction != null) {
			action = videoTime +' seconds';
			cmCreatePageviewTag(video +':'+ action, category);
		} else if (timeEnded != 'NA') {
			action = null; // 'Finished';  // done here till user reactivates video
			points = 30;
			cmCreateConversionEventTag(video, 2, category, points);
		} else {
			action = 'Paused';
			if (gTrackAction == action)
				return; // don't record mulitple Pauses
			cmCreatePageviewTag(video +':'+ action, category);
		}
		gTrackAction = action;
	}
	} catch (e) { }
}

function trackLink(page, category, href, step, points) {
	// External Link
	try {
		category = category == null? "ExternalLink": category; // Needs to be changed for each language

		if (step != null && parseInt(step) >= 0 && points != null && parseInt(points) >= 0)
			cmCreateConversionEventTag(page, step, category, points);
		else if (href == null)
			cmCreatePageviewTag(page, category);
		else
			cmCreateManualLinkClickTag(href, page, category);
	} catch (e) { }
}

// UTILS
function rmHTML(s) {
	return s.toString().replace(/<[^>]+>/g, '');
}

function addEvent( obj, type, fn ) {
  if ( obj.attachEvent ) {
    obj['e'+type+fn] = fn;
    obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
    obj.attachEvent( 'on'+type, obj[type+fn] );
  } else
    obj.addEventListener( type, fn, false );
}

// EVENTS
addEvent(window, 'load', function() { googleClickTracking('lihdMenu', 'topnav') });

// LEGACY
function getParamFromURL(arg) {
   arg = arg.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
   var regexS = "[\\?&]"+ arg +"=([^&#]*)";
   var regex = new RegExp(regexS);
   var results = regex.exec(window.location.href);
   if(results == null) 
      return "";
   else
     return results[1];
}
