/*****************************************************
* Comments:
* 20111017 SDZ: Created SaxoTechMisc.js
* 20111017 SDZ: Created GetElementID function
* 20111028 SDZ: Added gallery slider: (used to slide gallery boxes)
*               function Gallery_Initialize 
*               function Gallery_ChangeImage 
*               function Gallery_autoPlay
* 20111028 SDZ: Google maps:(used to display google maps)
*               function GMaps_ShowAddress
*               function GMaps_Initialize 
* 20111028 SDZ: Added gmaps dropdown:(used in guides to view gmaps)
*               function GMapsDropDown_ShowHideGMap
* 20111201 SDZ: Added Check on fields:(used in story sumbit, to preprocess fields before submit)
*               function Check_Fields
*               function ResetColor
* 20111201 SDZ: Added Ajax funtion (used for captcha in story submit)
*               function GetIt
******************************************************/

// gets element on a page
function GetElementID(PId){
  return document.getElementById(PId);
}
// end of function GetElementID


// reset color on element
function ResetColor(PId){
  return document.getElementById(PId).style.backgroundColor = '';
}
// end of function ResetColor

// Change fontsize on p tags
function ChangeFontSize(ASize) {
 
   var p = document.getElementsByTagName('p');
   for(i=0;i<p.length;i++) {
      if(p[i].className == "body")
        p[i].style.fontSize = ASize+"px"
   }
}
// end of function ChangeFontSize

// ajax call with prototype.js
function GetIt(PId,PUrl)
{
  $(PId).update("Loading...");
  new Ajax.Request(PUrl,
  {
    method:'get',
    onSuccess: function(transport){
    var response = transport.responseText || "no response text";
    $(PId).update(response);
    },
    onFailure: function(){ alert('Something went wrong...') }
  });
}
//end of Getit

// *************************** Google Maps starts ***************************
var GMap = null;
var GGeocoder = null;

function GMaps_Initialize(ADiv) 
{
  if (GBrowserIsCompatible()) 
  {
    GMap = new GMap2(document.getElementById(ADiv));
    GMap.removeMapType(G_SATELLITE_MAP);
    GMap.addControl(new GSmallMapControl());
    GMap.addControl(new GMapTypeControl());
    GGeocoder = new GClientGeocoder();
  }
}

function GMaps_ShowAddress(address) 
{
  if (GGeocoder) 
  {
    GGeocoder.getLatLng(address,function(point) 
    {
      if (!point) 
        alert(address + " not found");
      else 
      {
        GMap.setCenter(point, 10);
        var marker = new GMarker(point);
        GMap.addOverlay(marker);
        GEvent.addListener(marker, "click", function(){marker.openInfoWindowTabsHtml([new GInfoWindowTab("Adresse:",address)]);});
      }
    }
    );
  }
}
// *************************** Google Maps stop ***************************



// *************************** Gallery slider Starts ***************************
var GGalleryMap = [];

function Gallery_Initialize(AId){
  GGalleryMap[AId]      = new Array(2)
  //CurrentPosition
  GGalleryMap[AId][0]   = 1;
  //AutoPlayWindow
  GGalleryMap[AId][1]   = "";    
}

function Gallery_ChangeImage(AslideDivIdPrefix, AslideCountDivId, AslideInfoDivIdPrefix, AtotalNumberOfImages,AId,AAction)
{
  var Lfirst = 1;
  var Llast = AtotalNumberOfImages;
  
  if(!GGalleryMap[AId])
    Gallery_Initialize(AId);
  
  // Hide current picture
  var LslideDiv = GetElementID(AslideDivIdPrefix + GGalleryMap[AId][0]);
  LslideDiv.style.display = 'none';
  
  // Hide current picture information
  if(AslideInfoDivIdPrefix != '')
  {
    var LslideInfoDiv = GetElementID(AslideInfoDivIdPrefix + GGalleryMap[AId][0]);
    LslideInfoDiv.style.display = 'none';
  }
  
  if(AAction == "next")
  {
    // Show next picture, if Llast, loop back to front
    if (GGalleryMap[AId][0] == Llast)
      GGalleryMap[AId][0] = 1;
    else
      GGalleryMap[AId][0]++;
  }
  else
  {
    // Show previous picture, if first, loop back to end
    if (GGalleryMap[AId][0] == Lfirst)
      GGalleryMap[AId][0] = Llast;
    else
      GGalleryMap[AId][0]--;
  }
  // show next/previous picture
  LslideDiv = GetElementID(AslideDivIdPrefix + GGalleryMap[AId][0]);
  LslideDiv.style.display = 'block';
  
  // show next/previous picture information
  if(AslideInfoDivIdPrefix != '')
  {
    LslideInfoDiv = GetElementID(AslideInfoDivIdPrefix + GGalleryMap[AId][0]);
    LslideInfoDiv.style.display = 'block';
  }
  var LslideCountDiv = GetElementID(AslideCountDivId);
  LslideCountDiv.innerHTML = 'Bilde '+ GGalleryMap[AId][0] +' av '+AtotalNumberOfImages;
}


function Gallery_autoPlay(AslideDivIdPrefix, AslideCountDivId, AslideInfoDivIdPrefix,
AautoButtonImageId, AplayButtonUrl, ApauseButtonUrl,
AtotalNumberOfImages,AId)
{
  var LautoButtonImage = GetElementID(AautoButtonImageId);
  
  if(!GGalleryMap[AId])
    Gallery_Initialize(AId);
  
  if(GGalleryMap[AId][1]=="") {
    Gallery_ChangeImage(AslideDivIdPrefix, AslideCountDivId, AslideInfoDivIdPrefix, AtotalNumberOfImages,AId,"next");
    GGalleryMap[AId][1] = setInterval("Gallery_ChangeImage('"+AslideDivIdPrefix+"','"+AslideCountDivId+
      "','"+AslideInfoDivIdPrefix+"','"+AtotalNumberOfImages+"','"+AId+"','next'"+");", 2500);
    LautoButtonImage.src = ApauseButtonUrl;
  }
  else {
    clearInterval(GGalleryMap[AId][1]);
    GGalleryMap[AId][1] = "";
    LautoButtonImage.src = AplayButtonUrl;
  }
}
// *************************** Gallery slider stops ***************************

// *************************** Gmaps dropdown Starts ***************************
function GMapsDropDown_ShowHideGMap(AId,AAddress) 
{
  var LCMapLinkElement = GetElementID('DivLink_' + AId);
  var LMapDivId = 'DivMap_' + AId;
  
  if(isDivHidden(LMapDivId)) 
  {
    showDiv(LMapDivId);
    LCMapLinkElement.style.backgroundImage = "url(/css/forum_arrow_down.png)";
    LCMapLinkElement.style.color = "#666666";
    LCMapLinkElement.style.cursor = "default";
 
    if (GBrowserIsCompatible())
    {
      GMaps_Initialize('DivGeoMap_' + AId);
      GMaps_ShowAddress(AAddress);
    }
  }
  else 
  {
    hideDiv(LMapDivId);
    LCMapLinkElement.style.backgroundImage = "url(/css/listing_icon.png)";
    LCMapLinkElement.style.color = "#004276";
    LCMapLinkElement.style.cursor = "pointer";
  }
}
// *************************** Gmaps dropdown stops ***************************


// *************************** CheckFields starts ***************************
function Check_Fields(PWhat)
{
  var LFields=new Array();
  var LNrFields=new Array();
  var LResult = true;
  var LNrResult = true;
  var LColor = '#FFFFCC';
  
  // PreProcces
  switch(PWhat)
  {
    case 1: //
    	// ids of the fileds, that are going to be checked for content
      LFields[0]="taxonomy";
      LFields[1]="body1";
      LFields[2]="fulltnavn";
      LFields[3]="telefon";
      LFields[4]="email";
      LFields[5]="byline";
      LFields[6]="retningslinier";
      LFields[7]="captcha";
      // ids of the fileds, that are going to be valided for numbers
      //LNrFields[0]="field_AAR";
      //LNrFields[1]="date";
      
      GetElementID('title').value = "Innsendt artikel til: "                     + GetElementID('taxonomy').options[GetElementID('taxonomy').selectedIndex].text;
      GetElementID('body2').value = "Fulltnavn: "                                + GetElementID('fulltnavn').value 
                                  + "<br> Adresse : "                            + GetElementID('adresse').value
                                  + ", "                                         + GetElementID('adresse2').value
                                  + "<br> Telefon : "                            + GetElementID('telefon').value
                                  + "<br> E-post : "                             + GetElementID('email').value
                                  + "<br> Informasjon til redaksjonen : "        + GetElementID('informationtilreadksjonen').value;
    break;
  }
  // Reset Color
  for (var cnt=0; cnt < LFields.length; cnt++)
    ResetColor(LFields[cnt]);
  
  // check if the fields are empty
  for (cnt=0; cnt < LFields.length; cnt++)
  {
    if(GetElementID(LFields[cnt]).value == '')
    {
      GetElementID(LFields[cnt]).style.backgroundColor = LColor;
      if(LResult)
      {
      	alert("Felt markert med gul farge må fyllast ut, og retningslinjer for innsendt stoff må aksepterast.");
        GetElementID(LFields[cnt]).focus();
      }
      LResult = false;	
    }
  }
  
  // check if the fields contain a number
  if(LResult)
  {
    for (cnt=0; cnt < LNrFields.length; cnt++)
    {
      if(isNaN(GetElementID(LNrFields[cnt]).value))
      {
        GetElementID(LNrFields[cnt]).style.backgroundColor = LColor;
        if(LNrResult)
        {
          alert("Felt markert med gul farge må fyllast ut, og retningslinjer for innsendt stoff må aksepterast.");	
          GetElementID(LNrFields[cnt]).focus();
        }
        LNrResult = false;
      }
    }	  
  }
 
  // AfterProcces
  switch(PWhat)
  {
    case 1:  
    break;
  }
  
  if(!LResult || !LNrResult)
    return false;
  
}
// *************************** CheckFields stops ***************************

