// JavaScript Document
function Initialize()
{
	//Menu(1);
	//ShowNextDance();
	LoadGraphic();
	//ShowNextInstructor();
	LoadTopNews();
	LoadDance();
	LoadInstructor();
	
}
//Top Menu Functions
var xmlDocSubmenu=null;
var prevMenu = null;
function LoadMenu()
{
	xmlDocSubmenu = CreateXMLDocument("xmlFiles/submenu.xml")	
}
function Menu(index)
{
	 var divSubmenu = document.getElementById("sub_menu");
	 if(xmlDocSubmenu == null) {LoadMenu();}
	 var submenu = SelectSingleNode(xmlDocSubmenu,"//menu[@ID="+index+"]//submenu");


	 if(submenu != null)
	 {
		 divSubmenu.innerHTML = submenu.firstChild.nodeValue;
		 //selLink.className = "active";
	 }
	 if(prevMenu != null) prevMenu.className ="";
	 var a = document.getElementById("menu" + index);
	 prevMenu = a;
	 a.className = "active";
}


//Left Column Dance Section
var danceIndex= 1;
var danceCount = 0;
var xmlDocDance=null;

function LoadDances()
{
	xmlDocDance = CreateXMLDocument("xmlFiles/dances.xml")
	if(xmlDocDance != null)
	{
		 var dances = SelectNodes(xmlDocDance,"//dance");	
		 danceCount = dances.length;
	}
}


function ShowNextDance()
{
	var divDance = document.getElementById("divDanceLeftMenu");
	danceIndex++;
	if(divDance != null)
	{
		if(xmlDocDance == null) {LoadDances();}
		if(danceIndex  > danceCount) {danceIndex = 1;}
		//get next dance description
		 var dances = SelectSingleNode(xmlDocDance,"//dance[@ID="+danceIndex+"]//ShortDescription");
		 if(dances != null)
		 {
			divDance.innerHTML = dances.firstChild.nodeValue;
		 }
	}
}
function ShowPrevDance()
{
	var divDance = document.getElementById("divDanceLeftMenu");
	danceIndex--; 
	if(divDance != null)
	{
		if(xmlDocDance == null) {LoadDances();}
		if(danceIndex == 0) {danceIndex = danceCount;}
		
		//get next dance description
		 var dances = SelectSingleNode(xmlDocDance,"//dance[@ID="+danceIndex+"]//ShortDescription");
		
		 if(dances != null)
		 {
			divDance.innerHTML = dances.firstChild.nodeValue;
		 }
	}
}
function LoadDance()
{
	var divDance = document.getElementById("divDanceLeftMenu");
	if(divDance != null)
	{
		if(xmlDocDance == null) {LoadDances();}
		danceIndex = Math.round(Math.random()*danceCount);
	
		
		//get random dance
		 var dances = SelectSingleNode(xmlDocDance,"//dance[@ID="+danceIndex+"]//ShortDescription");
		
		 if(dances != null)
		 {
			divDance.innerHTML = dances.firstChild.nodeValue;
		 }
	}
}
//Left Column Dance Section
var d = new Date();
var dayIndex =0 ;
var xmlDocDays=null;
var weekdays = new Array("понеделник", "вторник", "сряда", "четвъртък", "петък", "събота", "неделя");
var months = new Array("януари", "февруари", "март", "април", "май", "юни", "юли", "август", "септември", "октомври", "ноември", "декемри");


function LoadGraphic()
{
	xmlDocDays = CreateXMLDocument("xmlFiles/graphic.xml");
	SetGraphicTitle(d);
}
function SetGraphicTitle(selectedDate)
{
	var graphicTitle = document.getElementById("graphicTitle");
	if (graphicTitle != null)
	{
		index = (selectedDate.getDay() ==0)? 6: selectedDate.getDay() - 1;

		graphicTitle.innerHTML = weekdays[index] + " | " + selectedDate.getDate() + " " + months[selectedDate.getMonth()];
	}	
	var graphic =  document.getElementById("graphic");
	if(graphic!= null)
	{
		var ID = (selectedDate.getDay() == 0)? 7: selectedDate.getDay();
		var dayGraphic = SelectSingleNode(xmlDocDays,"//day[@ID="+ID+"]//desc");
		if(dayGraphic != null)
			graphic.innerHTML = dayGraphic.firstChild.nodeValue;
	}
					  
}
function ShowNextDay()
{
	dayIndex++;
	if(xmlDocDays == null) {LoadGraphic();}
	selectedDay = new Date();
	selectedDay.setDate(selectedDay.getDate() + dayIndex);
	SetGraphicTitle(selectedDay);
}
function ShowPrevDay()
{
	dayIndex--;
	if(xmlDocDays == null) {LoadGraphic();}
	selectedDay = new Date();
	selectedDay.setDate(selectedDay.getDate() + dayIndex);
	SetGraphicTitle(selectedDay);
}


//Left Column Instructor Section
var instructorIndex= 1;
var instructorCount = 0;
var xmlDocInstructor=null;

function LoadInstructors()
{
	xmlDocInstructor = CreateXMLDocument("xmlFiles/instructors.xml");
	if(xmlDocInstructor != null)
	{
		 var instructor = SelectNodes(xmlDocInstructor,"//instructor");	
		 instructorCount = instructor.length;
	}
}


function ShowNextInstructor()
{
	var divInstructor = document.getElementById("instructors");
	instructorIndex++;
	if(divInstructor != null)
	{
		if(xmlDocInstructor == null) {LoadInstructors();}
		if(instructorIndex  > instructorCount) {instructorIndex = 1;}
		//get next dance description
		 var instructors = SelectSingleNode(xmlDocInstructor,"//instructor[@ID="+instructorIndex+"]//ShortDescription");
		 if(instructors != null)
		 {
			divInstructor.innerHTML = instructors.firstChild.nodeValue;
		 }
	}
}
function ShowPrevInstructor()
{
	
	var divInstructor = document.getElementById("instructors");
	instructorIndex--;
	if(divInstructor != null)
	{
		if(xmlDocInstructor == null) {LoadInstructors();}
		if(instructorIndex  ==0 ) {instructorIndex = instructorCount;}
		//get next dance description
		 var instructors = SelectSingleNode(xmlDocInstructor,"//instructor[@ID="+instructorIndex+"]//ShortDescription");
		 if(instructors != null)
		 {
			divInstructor.innerHTML = instructors.firstChild.nodeValue;
		 }
	}
		
}

function LoadInstructor()
{
	var divInstructor = document.getElementById("instructors");
	if(divInstructor != null)
	{
		if(xmlDocInstructor == null) {LoadInstructors();}
		instructorIndex = Math.round(Math.random()*instructorCount);

		//show random instructor
		 var instructors = SelectSingleNode(xmlDocInstructor,"//instructor[@ID="+instructorIndex+"]//ShortDescription");
		 if(instructors != null)
		 {
			divInstructor.innerHTML = instructors.firstChild.nodeValue;
		 }
	}	
}
//Right Menu NEWS
function LoadTopNews()
{
	 var params = "";
	 var newsDiv = document.getElementById("news");
	 var docXML = CreateXMLDocument("xmlFiles/news.xml");
	
	if(bVersion.indexOf('safari')+1)
    {
		LoadServerTransformedDoc('index.htm', 
									newsDiv,
									escape(OuterXML(docXML)),
									"",
									escape("xmlFiles/newsTransformation.xsl"),
									params,
									null);
		
   	}
   else
   	{
	   newsXSL = CreateXMLDocument("xmlFiles/newsTransformation.xsl");
	   var processorObj = CreateXSLTProcessor(newsXSL);
  
	   newsDiv.innerHTML = XSLTProcessorTransform(processorObj, docXML);
	  
	   processorObj.reset();
	      
   	}

	var divContent = newsDiv.innerHTML;
	divContent = divContent.replace(/&lt;/g, "<");
	divContent = divContent.replace(/&gt;/g ,">");
	newsDiv.innerHTML = divContent;
	
}

function LoadSelectedNews()
{
	var args = getArgs(); 
	if(args.n)
	{
		var index = parseInt(args.n); 	
		
		var innerDiv = document.getElementById("inner");
		xmlDoc = CreateXMLDocument("xmlFiles/news.xml");

		if (xmlDoc != null)
		{
			 var node = SelectSingleNode(xmlDoc, "root//news[@id= '"+index+"']//text");
			
			 if(node != null)
			 {
				 if( node.firstChild != null)
				 {
					innerDiv.innerHTML = "<div>" + node.firstChild.nodeValue + "</div>";
				 }
	
			 }
		}
	}
}

function getArgs() { 
	var args = new Object(); 
	var query = location.search.substring(1); 
	var pairs = query.split("&"); 
	for(var i = 0; i < pairs.length; i++) { 
		var pos = pairs[i].indexOf('='); 
		if (pos == -1) continue; 
		var argname = pairs[i].substring(0,pos); 
		var value = pairs[i].substring(pos+1); 
		args[argname] = unescape(value); 
	} 
	return args; 
}
//Load previous News when "новини" submenu clicked
function LoadPrevNews()
{}
//Function for loading inner Content
function LoadNode(xmlFile, nodeXPath)
{
	var innerDiv = document.getElementById("inner");
	xmlDoc = CreateXMLDocument(xmlFile);

    if (xmlDoc != null)
	{
		 var node = SelectSingleNode(xmlDoc, nodeXPath);
		 if(node != null)
		 {
			 if( node.firstChild != null)
				innerDiv.innerHTML = node.firstChild.nodeValue;

		 }
	}
}

function LoadNodes(xmlFile, nodeXPath)
{
	var innerDiv = document.getElementById("inner");
	innerDiv.innerHTML 
	xmlDoc = CreateXMLDocument(xmlFile);
	if (xmlDoc != null)
	{
		var nodes = SelectNodes(xmlDoc, nodeXPath);
		if(nodes.length > 0) {innerDiv.innerHTML = ""; }
		for(i = 0; i< nodes.length; i++)
		{
			innerDiv.innerHTML += nodes[i].firstChild.nodeValue;
		}
	}
}





function MM_CheckFlashVersion(reqVerStr,msg){

  with(navigator){

    var isIE  = (appVersion.indexOf("MSIE") != -1 && userAgent.indexOf("Opera") == -1);

    var isWin = (appVersion.toLowerCase().indexOf("win") != -1);

    if (!isIE || !isWin){  

      var flashVer = -1;

      if (plugins && plugins.length > 0){

        var desc = plugins["Shockwave Flash"] ? plugins["Shockwave Flash"].description : "";

        desc = plugins["Shockwave Flash 2.0"] ? plugins["Shockwave Flash 2.0"].description : desc;

        if (desc == "") flashVer = -1;

        else{

          var descArr = desc.split(" ");

          var tempArrMajor = descArr[2].split(".");

          var verMajor = tempArrMajor[0];

          var tempArrMinor = (descArr[3] != "") ? descArr[3].split("r") : descArr[4].split("r");

          var verMinor = (tempArrMinor[1] > 0) ? tempArrMinor[1] : 0;

          flashVer =  parseFloat(verMajor + "." + verMinor);

        }

      }

      // WebTV has Flash Player 4 or lower -- too low for video

      else if (userAgent.toLowerCase().indexOf("webtv") != -1) flashVer = 4.0;



      var verArr = reqVerStr.split(",");

      var reqVer = parseFloat(verArr[0] + "." + verArr[2]);

  

      if (flashVer < reqVer){

        if (confirm(msg))

          window.location = "http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash";

      }

    }

  } 

}