/*
Source: $Source$
Version: $Revision: 14509 $
*/

/**
 * The core.js provides several tools, interactive functionalities etc. used on many pages
 * @requires yui 2.5.0, (yahoo, event, dom, connection)
 */

// create the jcore namecpace
/*global YAHOO */
YAHOO.namespace ("jcore");

/**
 * The Popup function provides an easy way to open popup links with unobtrusive javascript
 * Usage: add the className 'popuplink' to the <a/> html element (class="popuplink" or class="serviceChat popuplink" ...)
 *        add the link to the href-attribute
 *        add the target-attribute to the <a/>-element. the value of this attribute must have the following syntax:
 *            example: target="AGB_640x480"
 *            syntax:  target="[popup-name]_[size-of-the-popup]" (popup-name and size-of-the-popup divided by "_")
 *            (popup-name: the name of the popup for the window.open-method)
 *            (size-of-the-popup: the width and heigt divided by 'x')
 * @module Popup
 * @param (Event) e
 * @requires yui 2.5.0, (yahoo, event, dom)
 */
YAHOO.jcore.Popup = function(e) {
  // get the events target
  var t = (e?YAHOO.util.Event.getTarget(e):null);

if ( !YAHOO.util.Dom.hasClass(t, "popuplink") ) {
  	var p = t.parentNode;
  	if (YAHOO.util.Dom.hasClass(p, "popuplink") ){
  	 t = p;
  	}else{
   	 return;
  	}
  }

  YAHOO.util.Event.stopEvent(e);
  var wName = "popupWin", pWidth = "600", pHeight = "400";

  // get the 'target'-attribute of the html element
  var attrTarget = t.target;
  // sample target value: "Ringtones_688x393"
  if ( attrTarget && attrTarget.indexOf("_") )
  {
    // get the name of the popup window
    wName = attrTarget.substring(0, attrTarget.indexOf("_"));
    // get width and height values
    if ( attrTarget.indexOf("x") != -1 )
    {
      pWidth = attrTarget.substring(attrTarget.indexOf("_")+1, attrTarget.indexOf("x"));
      pHeight = attrTarget.substring(attrTarget.indexOf("x")+1, attrTarget.length);
    }
  }

  var popupPar = 'width=' + pWidth + ',height=' + pHeight + ',top=100,left=100,resizable=yes,scrollbars=yes,menubar=no,toolbar=no,status=no,location=no';

  /*global window */
  window.open(t.href, '' + wName + '', '' + popupPar + '');
};

/**
 * Assumption: The clicked link is inside a popup!
 * In this case this functions opens the clicked link in the opener window of
 * the popup 
 */
YAHOO.jcore.PopDown = function(e) {
  // get the events target
  var t = (e?YAHOO.util.Event.getTarget(e):null);

if ( !YAHOO.util.Dom.hasClass(t, "popdownlink") ) {
  	var p = t.parentNode;
  	if (YAHOO.util.Dom.hasClass(p, "popdownlink") ){
  	 t = p;
  	}else{
   	 return;
  	}
  }

  YAHOO.util.Event.stopEvent(e);
  opener.location = t.href;

  

};

/**
 *The TogglePanel Function provides an easy way to open/close layer by a given id
 *  just add one of the following classes to the link: "fmd-openPanel" or "fmd-closePanel"
 *  in the target you can define the id of the layer you want to toggle.
 *
 *  example: <a class="fmd-closePanel" target="id.fmd-gamesDemo" href="#">
 *
 */
YAHOO.jcore.TogglePanel = function(e){
  var trigger = (e?YAHOO.util.Event.getTarget(e):null);

  if (!YAHOO.util.Dom.hasClass(trigger, "fmd-closePanel") && !YAHOO.util.Dom.hasClass(trigger, "fmd-openPanel"))
  {
    var p = trigger.parentNode;
    if (YAHOO.util.Dom.hasClass(p, "fmd-closePanel") || YAHOO.util.Dom.hasClass(p, "fmd-openPanel"))
    {
      trigger = p;
    }else{
      return;
    }
  }

  YAHOO.util.Event.stopEvent(e);

  var attrTarget = trigger.target;
  // sample target value: "id.fmd-gamesDemo"
  if ( attrTarget && attrTarget.indexOf(".") )
  {
    // get the id of the layer to be closed
    pid = attrTarget.substring(attrTarget.indexOf(".")+1, attrTarget.length);
    element = document.getElementById(pid);
    if(element)
    {
      if (element.getAttribute('id') === 'fmd-morePanel') {
        YAHOO.mp.MorePanel.hideMorePanel();
      } else {
        if (YAHOO.util.Dom.hasClass(trigger, "fmd-closePanel"))
        {
          YAHOO.jcore.switchVisibility(element, 'hide');
        }
        if (YAHOO.util.Dom.hasClass(trigger, "fmd-openPanel"))
        {
          YAHOO.jcore.switchVisibility(element, 'show');
        }
      }
    }
  }
};

YAHOO.jcore.switchVisibility = function(element, visibility){
  if(element)
  {
    if (visibility == 'hide')
    {
        YAHOO.util.Dom.setStyle(element, 'display', 'none');
        YAHOO.util.Dom.setStyle(element, 'visibility', 'hidden');
    }
    if (visibility == 'show')
    {
        YAHOO.util.Dom.setStyle(element, 'display', 'block');
        YAHOO.util.Dom.setStyle(element, 'visibility', 'visible');
    }
  }
}

/**
 *The SelectAccordion Function provides an easy way to select a tab of
 *the accordion component by setting a classname of fmd-accordionHighlight
 *for the selected list element.
 *
 *  example:
 *  <ul>
 *    <li class="fmd-accordionHighlight">...</li>
 *    <li></li>
 *    <li></li>
 *  </ul>
 *
 */
YAHOO.jcore.SelectAccordion = function(e){
   var t = (e?YAHOO.util.Event.getTarget(e):null);
   if (YAHOO.util.Dom.hasClass(t, "fmd-accordionHeading")) {
       YAHOO.util.Event.stopEvent(e);
       var selectedAccordionTab = YAHOO.util.Dom.getAncestorByTagName(t, "li");
       var allAccordionTabs = YAHOO.util.Dom.getChildren(YAHOO.util.Dom.getAncestorByTagName(t, "ul"));
       YAHOO.util.Dom.removeClass(allAccordionTabs, "fmd-accordionHighlight");
       YAHOO.util.Dom.addClass(selectedAccordionTab, "fmd-accordionHighlight");
   }
};

function selectAccordionTabById(){

    var body = document.getElementsByTagName("body")[0];
    var tabId = "fmd-teaserMusic";

    switch (body.id) {
       case "fmd-info":
        tabId = "fmd-teaserInfoservices";
        break;
      case "fmd-games":
        tabId = "fmd-teaserGames";
        break;
      case "fmd-applications":
        tabId = "fmd-teaserApps";
        break;
      default:
        tabId = "fmd-teaserRingtones";
        break;
    }

	 var t = YAHOO.util.Dom.get(tabId);
   if (t) {
       var selectedAccordionTab = YAHOO.util.Dom.getAncestorByTagName(t, "li");
       var allAccordionTabs = YAHOO.util.Dom.getChildren(YAHOO.util.Dom.getAncestorByTagName(t, "ul"));
       YAHOO.util.Dom.removeClass(allAccordionTabs, "fmd-accordionHighlight");
       YAHOO.util.Dom.addClass(selectedAccordionTab, "fmd-accordionHighlight");
   }
}
YAHOO.util.Event.onDOMReady(function(){selectAccordionTabById();});

/**
 * Switch the panel with the debug information on/off
 * Of course debug mode must be enabled
 */
YAHOO.jcore.ToggleDebugPanel = function(e){
    var t = (e?YAHOO.util.Event.getTarget(e):null);

    if ( !YAHOO.util.Dom.hasClass(t, "fmd-debugPopup") ) {
  	var p = t.parentNode;
  	if (YAHOO.util.Dom.hasClass(p, "fmd-debugPopup") ){
  	 t = p;
  	}else{
   	 return;
  	}
  }

     YAHOO.util.Event.stopEvent(e);

     var debugContainer = YAHOO.util.Dom.getAncestorByClassName(t,"fmd-debugInfo");
     var list = debugContainer.getElementsByTagName("ul")[0];

     if(list){
       if (YAHOO.util.Dom.hasClass(t, "fmd-closePanel")){
       	 YAHOO.util.Dom.setStyle(list, 'display', 'none');
         YAHOO.util.Dom.removeClass(t, 'fmd-closePanel');
       }else{
         YAHOO.util.Dom.setStyle(list, 'display', 'block');
         YAHOO.util.Dom.addClass(t, 'fmd-closePanel');
       }
    }



}
/**
 * Update a character counter
 * @param (String) id of the counter
 * @param (Object) the input element
 * @param (number) the maximum aloowed length of the text
 */
function updateCharCounter(counterId,obj,totalCount){
    var counter = YAHOO.util.Dom.get(counterId);
    var rest= totalCount-obj.value.length;
    counter.innerHTML=rest;
    if (rest < 0)
    {
      if (obj.value.length > totalCount-1)
      {
        obj.value = obj.value.substring(0,totalCount);
        updateCharCounter(counterId,obj,totalCount);
      }
    }

}

/**
 * Delete company addOn.
 * @param (String) document title
 */
function formatTitle(title) {
  if(title.indexOf('o2') != -1) return title.substr(0,title.length-5);
  return title;
}




/* set the click listener for the Panel Toggle-function */
YAHOO.util.Event.onDOMReady(function(){YAHOO.util.Event.addListener(/*global document */document.body, 'click', YAHOO.jcore.TogglePanel);});


/* set the click listener for the Popup-function */
YAHOO.util.Event.onDOMReady(function(){YAHOO.util.Event.addListener(/*global document */document.body, 'click', YAHOO.jcore.Popup);});

/* set the click listener for the for links that shoud be opend in opener window */
YAHOO.util.Event.onDOMReady(function(){YAHOO.util.Event.addListener(/*global document */document.body, 'click', YAHOO.jcore.PopDown);});

/* set the click listener for the accordion component(s) */
YAHOO.util.Event.onDOMReady(function(){YAHOO.util.Event.addListener(/*global document */document.body, 'click', YAHOO.jcore.SelectAccordion);});

/**
 * Remove the className 'jsdisabled' from <body/>-tag
 * @requires yui 2.5.0, (yahoo, event, dom)
 */
YAHOO.util.Event.onDOMReady(function(){ YAHOO.util.Dom.removeClass(document.body, 'jsdisabled'); });

/* set the click listener for the Panel ToggleDebugPanel-function */
YAHOO.util.Event.onDOMReady(function(){YAHOO.util.Event.addListener(/*global document */document.body, 'click', YAHOO.jcore.ToggleDebugPanel);});
