var favicon = "http://www.waffar.com/skin/frontend/default/modern/favicon.ico";
var alerticon = "/skin/frontend/default/modern/iejs/alert.ico";
var video;

/*------------------ INITIALIZATION --------------------*/
function initApp() {

    if (window.external.msIsSiteMode()) {

        // Hook up handlers that need to change with focus changes.  This is
        // necessary to get correct behavior when switching between browser tabs.
        window.onfocus = initFocus;

        // Add the overlay play and pause buttons for video content
        //addButtons();

    }
    else {

        // Turn on the add to start menu button - note this is not on by default to avoid
        // having it flash on the screen when in site mode
        var btnAddWebApp = document.getElementById("btnAddWebApp");
        if (btnAddWebApp)
            btnAddWebApp.style.display = 'block';
    }
}

function initFocus() {

    // Add local page category and items to the jumplist
    addLocalJumpList();

}

/*------------------ ADD SITE TO START MENU --------------------*/
function addWebApp() {

    // Add to start menu
    try {
        window.external.msAddSiteMode();
    }
    catch (ex) {
        // Site Mode not supported.
    } 
}

/*------------------ OVERLAY ICON ALERTING --------------------*/
function notify() {

    // Add overlay icon to alert the user 
    window.external.msSiteModeSetIconOverlay(alerticon, 'notify');

    // Set the var to tell the new items page what to display.  Simulates new items being
    // added to this page.
    window.localStorage.NewItems++;
}

function clearAlertIcons() {
    // Clear overlay icon
    window.external.msSiteModeClearIconOverlay();
}

/*------------------ ADD LOCAL JUMPLIST ITEMS -----------------*/
function addLocalJumpList() {
	//window.external.msSiteModeClearJumpList();
    var cl = document.getElementById("nav");
    if (cl) {
        var anchors = cl.getElementsByClassName("level-top");

        // Add local category to the jumplist.  Note that this also clears any existing jumplist.
        // Adding the currently focused page type (e.g. Blog, Article, etc.)
        // create category
        //window.external.msSiteModeCreateJumpList("Shortcuts");
        
        // For each page entry in current page, create a JumpList Item with item title and linking to item URL
        for (cnt = 0; cnt < anchors.length; cnt++) {
        	//alert( anchors[cnt].children[0].href);
            //window.external.msSiteModeAddJumpListItem(anchors[cnt].children[0].innerText, anchors[cnt].children[0].href, favicon);
        	cnt++;
        }
        //window.external.msSiteModeShowJumplist();
    }
    else {
       // window.external.msSiteModeClearJumplist();
    }
}


/*------------------ THUMBNAIL BUTTONS --------------------*/
var btnPlayVideo, stylePlay, stylePause;

function addButtons() {

    video = document.getElementsByTagName('video')[0];

    if (video) {

        // Create the Thumbbar.  If it already exists for this session, this will set the 
        // correct reference for the button.
        btnPlayVideo = window.external.msSiteModeAddThumbBarButton('images/play.ico', 'Play');

        // Set the enabled and visible properties to true in case this thumbar has been deactivated in
        // this session
        window.external.msSiteModeUpdateThumbBarButton(btnPlayVideo, true, true);

        // Then we add the Pause and Play styles to toggle between Play / Pause
        // requires full URL to icon file
        stylePause = window.external.msSiteModeAddButtonStyle(btnPlayVideo,
                  'http://localhost/sitemodesamplecomplete/images/Pause.ico',
                 'Pause');

        stylePlay = window.external.msSiteModeAddButtonStyle(btnPlayVideo,
                  'http://localhost/sitemodesamplecomplete/images/Play.ico',
                  'Play');

        // Then we setup the listener to handle a button click
        if (document.addEventListener) {
            document.addEventListener('msthumbnailclick', thumbnailclick, false);
        }
        // Finally we set the buttons to show on the Thumbnail
        window.external.msSiteModeShowThumbBar();

        window.onunload = hideButtons;
    }
}

// Hide the buttons every time we leave the page.
function hideButtons() {

    window.external.msSiteModeUpdateThumbBarButton(btnPlayVideo, false, false);

}

function thumbnailclick(btn) {

    if (btn.buttonID == btnPlayVideo) {
        if (video.paused) {
            video.play();
            window.external.msSiteModeShowButtonStyle(btnPlayVideo, stylePause);
        }
        else {
            video.pause();
            window.external.msSiteModeShowButtonStyle(btnPlayVideo, stylePlay);
        }
    } 
}

//initApp();


//btn1 = window.external.msSiteModeAddThumbBarButton("/skin/frontend/default/modern/iejs/twitter.ico","Twitter");
jQuery(document).ready(function(){ 
	
	/*FB.api('/me/likes/118839294822651',function(response) {
		//alert(response.error.message);
		if( response.data ) {
			if( !isEmpty(response.data) )
				alert('You are a fan!');
			else
				alert('Not a fan!');
		} else {
			alert('ERROR!');
		}
	});
	
	// function to check for an empty object
	function isEmpty(obj) {
		for(var prop in obj) {
			if(obj.hasOwnProperty(prop))
				return false;
		}
	
		return true;
	}*/
  
jQuery('head').pinify({   
         applicationName: 'waffar',
         favIcon: '/skin/frontend/default/modern/favicon.ico',
         startUrl: '/',
         tooltip: 'waffar',
         tasks: [{
               'name': 'Olympic Voucher',
               'action': 'http://waffar.com/olympic.html',
               'icon': favicon,
                'target':'parent'
            }]
   });
   
   jQuery('#topHatContainer').pinify('pinTeaser', {
               icon: '/skin/frontend/default/modern/favicon.ico',
               pinText: 'Drag this image or the tab to the taskbar to pin this site',
               addStartLink: false,
               //linkText: 'Click here to add this site to the start menu',
               mainContentSelector: '#body'
            });
            
       
      var cl = document.getElementById("nav");
      var anchors = jQuery("#nav >li.level0");
          //alert(anchors.length);   
    var itemList = [];

   jQuery.each(anchors, function (key, val) {
   		
      var item = {
         'name': anchors[key].children[0].innerText,
         'url': anchors[key].children[0].href,
         'icon': favicon
      };

      itemList.push(item);
  });

  jQuery.pinify.addJumpList({
        title: 'Shortcuts',
      items: itemList
  });
  

   /*if (data.length > commentCount) {
   jQuery.pinify.flashTaskbar();
   jQuery('#commentCount').val(data.length);

   jQuery.pinify.addOverlay({
      title: 'New comment: ' + data[0].text + ' (' + data[0].date + ')',
      icon: '/Content/Images/comment.ico'
   });
	}*/
            	
});	
