document.write("<script type='text/javascript' src='js/jquery-1.2.6.min.js'></script>");
document.write("<script type='text/javascript' src='js/jquery.pngFix.pack.js'></script>");
document.write("<script type='text/javascript' src='js/jquery.fancybox-1.0.0.js'></script>");

    var totalCount = 0;
    var totalCountTimers = 0;
	var curalarmtypeIndex=4;
	var specialAlarmIndex=0;
    var arrAlarmTime = new Array(3);
    var arrAlarmTimeoutID = new Array(3);
	var twitter_alarm_time = "";
	var twitter_alarm_sound = "";

    function setAlarm(alarmHours,alarmMinutes,alarmMeredien) {
		if (totalCountTimers < 3) {
			//alert(alarmHours + ":" + alarmMinutes + " " + alarmMeredien)
			alarmHours=parseInt(alarmHours);
			alarmMinutes=parseInt(alarmMinutes);
            showAlarmHours = (alarmHours<10) ? "0" + alarmHours : alarmHours;
			showAlarmMinutes = (alarmMinutes<10) ? "0" + alarmMinutes : alarmMinutes;
			arrAlarmTime[totalCountTimers] = showAlarmHours + ":" + showAlarmMinutes + " " + alarmMeredien;
			
			twitter_alarm_time=alarmHours + "" + ((alarmMinutes==0)?"":showAlarmMinutes) + alarmMeredien.toLowerCase();
			
            if (alarmMeredien == "PM") {
                if (alarmHours == 12) {
                    alarmHours = 0;
                }
                alarmHours = parseInt(alarmHours) + parseInt(12);
            } else if (alarmMeredien == "AM") {
                if (alarmHours == 12) {
                    alarmHours = 0;
                }
            }

            var d = new Date;
            var currentHours = d.getHours();
            var currentMinutes = d.getMinutes();
            var currentSeconds = d.getSeconds();
            var remaininghrs = 23 - currentHours;
            var remainingmins = 60 - currentMinutes;
            var remainingsecs = 60 - currentSeconds;
            var totalAlarmSecondsTemp = (parseInt(alarmHours) * 3600) + parseInt(alarmMinutes) * 60;
            var remainingtotalsecs = (parseInt(remaininghrs) * 3600) + parseInt(remainingmins) * 60 + remainingsecs;
            var totalCurrentSecondsTemp = (parseInt(currentHours) * 3600) + parseInt(currentMinutes) * 60 + parseInt(currentSeconds);
            var totalSeconds = totalAlarmSecondsTemp - totalCurrentSecondsTemp;
            
			//alert(arrAlarmTime[totalCountTimers]);
			if (totalSeconds < 0) {
                totalAlarmSecondsTemp = totalAlarmSecondsTemp + remainingtotalsecs;
                totalSeconds = totalAlarmSecondsTemp;
                _L("totalAlarmSecondsTemp " + totalAlarmSecondsTemp);
                _L("totalSeconds " + totalSeconds);
            }
            if (totalSeconds >= 0) {
                //alert("fire back after secs " + totalSeconds);
                totalDiffMiliseconds = parseInt(totalSeconds) * parseInt(1000);
                arrAlarmTimeoutID[totalCount] = setTimeout("showFlash('" + totalCount +  "','" +  curalarmtypeIndex +  "','" + specialAlarmIndex + "')", parseInt(totalDiffMiliseconds));
                var showAlarmTime = "";
				var i=totalCount;
				
				 showAlarmTime += "<div class='alarmdiv'><div id='alarmli_" + i + "'><span class='alarm_time'>" +arrAlarmTime[totalCountTimers] + "</span><span class='link'>[ <a style='color:#ffffff' href='javascript:void(0);' onclick='clearTimer(" + i + ");'>x</a> ]</span> <span class='alarm_description'>(alarm set) </span> </div></div>";
                totalCount++;
                totalCountTimers++;
				
        		document.getElementById("alarmTimeDisp").innerHTML = document.getElementById("alarmTimeDisp").innerHTML + showAlarmTime;
            }
        } else {
			alert("You can not set more than 3 alarms.");
		}
    }
	
	

function clearTimer(clearID) {
	
	if(document.getElementById("alarmli_" + clearID)){
		// remove
			curval="#alarmli_" + clearID;
				
		var tmpelem=$("#alarmTimeDisp").find(curval);
				$(tmpelem).parent().remove()
					
	}
	clearTimeout(arrAlarmTimeoutID[clearID])
	totalCountTimers--;
	arrAlarmTime[clearID] = "";
	//arrAlarmTime.splice(clearID,1);
}
function showAlarmSoundInterface() { 

	document.getElementById("divAlarmSoundInterface").style.visibility = "visible";
	document.getElementById("divcheckbox").style.visibility = "visible";
	document.getElementById("divInstruct").style.visibility = "visible";
	document.getElementById("divInstruct-setalarm").style.visibility = "visible";
	//find active class in sound interface and set alarmtype
	elem=$("#divAlarmSoundInterface").find("a.active")[0]
	showdebug("showAlarmSoundInterface");
    	ChangemyAlarmtype($(elem).attr("name"));
}
function toggleDiv(id)
{
   var div = document.getElementById(id);
   var current = div.style.display;
   
   if (current == 'block')
      div.style.display = 'none';
   else
      div.style.display = 'block';
}

 function toggle_visibility(id) {
       var e = document.getElementById(id);
       if(e.style.display == 'block')
          e.style.display = 'none';
       else
          e.style.display = 'block';
    }

	
function closeAlarmSoundInterface(){
	document.getElementById("divAlarmSoundInterface").style.visibility = "hidden";
	document.getElementById("divAlarmSoundInterface2").style.visibility = "hidden";
	document.getElementById("divcheckbox").style.visibility = "hidden";
	document.getElementById("divInstruct").style.visibility = "hidden";
	document.getElementById("divInstruct-setalarm").style.visibility = "hidden";
}
function showWeatherOptionInterface() { 
	document.getElementById("divWeatherOptionInterface").style.visibility = "visible";
	document.getElementById("divAlarmTime").style.visibility = "hidden";
}
	
function closeWeatherOptionInterface(){
	document.getElementById("divWeatherOptionInterface").style.visibility = "hidden"; 
	document.getElementById("divAlarmTime").style.visibility = "visible";
}

function showFlightOptionInterface() { 
	document.getElementById("divFlightOptionInterface").style.visibility = "visible";
	document.getElementById("divAlarmTime").style.visibility = "hidden";
}

function closeFlightOptionInterface(){
	document.getElementById("divFlightOptionInterface").style.visibility = "hidden";
	document.getElementById("divAlarmTime").style.visibility = "visible";
}


function closeOptionInterface(){
	if(document.getElementById("divWeatherOptionInterface"))
	  document.getElementById("divWeatherOptionInterface").style.visibility = "hidden";
	  document.getElementById("divAlarmTime").style.visibility = "visible";
	if(document.getElementById("divFlightOptionInterface"))
	  document.getElementById("divFlightOptionInterface").style.visibility = "hidden";
	  document.getElementById("divAlarmTime").style.visibility = "visible";
	if(document.getElementById("divNanoOptionInterface"))
	  document.getElementById("divNanoOptionInterface").style.visibility = "hidden";
	  document.getElementById("divAlarmTime").style.visibility = "visible";
	if(document.getElementById("divTimeOptionInterface"))
	  document.getElementById("divTimeOptionInterface").style.visibility = "hidden";
      document.getElementById("divAlarmTime").style.visibility = "visible";	  
}


function showPreviewInterface() { 
	document.getElementById("divPreviewInterface").style.visibility = "visible";
	document.getElementById("divAlarmTime").style.visibility = "hidden";
}

function closePreviewInterface(){
	document.getElementById("divPreviewInterface").style.visibility = "hidden";
	document.getElementById("divAlarmTime").style.visibility = "visible";
}




function showTwitterInterface(tm,ind) { 
	document.getElementById("divTwitterInterface").style.visibility = "visible";
	//twitter_alarm_time=tm;
    if(ind==4) twitter_alarm_sound="weather";
    else if(ind==1) twitter_alarm_sound="time";
	else if(ind==6 && specialAlarmIndex==1) twitter_alarm_sound="rooster";
}

function closeTwitterInterface(){
	document.getElementById("divTwitterInterface").style.visibility = "hidden";	
}
function getSelectedItem() {	
	out="";e=document.social_form.soc_cat;
	len=e.length;
	if(len==undefined){
		if(e.checked)out=e.value;
	}else{
		for (i=0; i <len; i++)
	  		if (e[i].checked) out=e[i].value
	}
	return out;
}

function submitToTwitter(){
	var s=getSelectedItem();
	if(s!="")
		window.location = "twitter/index.php?time=" + twitter_alarm_time + "&sound=" + twitter_alarm_sound + "&cat=" + getSelectedItem();
	else
		alert("Please select the alarm type.");
}
function submitToFb(){
	var s=getSelectedItem();
	if(s!="")
		window.location = "fb/index.php?time=" + twitter_alarm_time + "&sound=" + twitter_alarm_sound + "&cat=" + getSelectedItem();
	else
		alert("Please choose type of wake up.");
}


function _L(logstr) {
	return 0;
	try {
		console.log(logstr)
	} catch (err) {
	}
}


var mymovie;
var isWindows = navigator.appName.indexOf("Microsoft") != -1;

function onFlashLoad() {
}




function InitializePage() {
	var dDefault = new Date;
	var currentHoursDefault = dDefault.getHours();
	var currentMinutesDefault = dDefault.getMinutes();
	if (currentHoursDefault > 12) {
		currentHoursDefault = parseInt(currentHoursDefault) - 12;
		document.getElementById("cmbMeredien").value = "PM";
	} else if (currentHoursDefault == 12) {
		document.getElementById("cmbMeredien").value = "PM";
	} else {
		document.getElementById("cmbMeredien").value = "AM";
	}
	document.getElementById("cmdHours").value = currentHoursDefault;
	document.getElementById("cmdMinute").value = currentMinutesDefault;
	setTimeout("preloadswfs()", 1000);
	setVal("isLoud", 0);
}


function setParent(el, newParent) {
	newParent.appendChild(el);
}


function getSWF(movieName) {
	return document.getElementById(movieName);
}


function StartplayingfromScript(trframeno) {
	mymovie = getSWF("infotimer");
	mymovie.StartplayingfromScript(trframeno);
}


function setLoudAlarm(myvalr) {
	mymovie = getSWF("infotimer");
	mymovie.setLoudAlarm(myvalr);
}


function obj(_id) {
	return document.getElementById(_id);
}


function $V(_id) {
	return document.getElementById(_id).value;
}


function setVal(_id, _val) {
	document.getElementById(_id).value = _val;
}


function setHtml(_id, _txte) {
	document.getElementById(_id).innerHTML = _txte;
}


   


    function updatezipcode() {
//        selobj = document.getElementById("cmbalarmtype");
        if (document.getElementById("txtzip").value == "") {
           // selobj.selectedIndex = 0;
            document.getElementById("divzipcode").style.visibility = "hidden";
            return;
        }
     
	 	var oR  = /(^\d{5}$)|(^\d{5}-\d{4}$)|(^[A-Za-z]{4}\d+$)/;
	    if(!oR.test(document.getElementById("txtzip").value)){
			alert("Please enter a valid zip code.");
			return;
		}
	
	 //_myval = "WEATHER_" + document.getElementById("txtzip").value;
        createCookie("myLocId", $V('txtzip'));
  
  		createCookie("ChangeLocinfo", "true");
  

        document.getElementById("divzipcode").style.visibility = "hidden";
	//	setVal("alarmtype", "2") 
//		changeflashzipcode($V('txtzip'));
		
		 window.location.reload(  );

		
		

//        ChangemyAlarmtype("2");
    }


    function onUpdateFlightcode() {
       
        if (document.getElementById("txtflightno").value == "") {
       
		   alert("Please enter Valid Flight Number");
		   document.getElementById("txtflightno").focus();
            //document.getElementById("divflight").style.visibility = "hidden";
            return;
        }
		
       var curflightcode=$("#lstflight").val() + $("#txtflightno").val();
	   
	  
	   $("#flightcode").val(curflightcode)
        createCookie("myflightcode", curflightcode);
       
	   //document.getElementById("divflight").style.visibility = "hidden";
	   
	   setVal("alarmtype", "3") 
	    ChangemyAlarmtype("3");
		changeflashflightcode(curflightcode);
		
        closeFlightOptionInterface();
    }

    function ChangemyAlarmtype(_curindex) {
     
        _L("alarm type " + _curindex);
        mymovie = getSWF("infotimer");
        mymovie.changeAlarmtype(_curindex);
		curalarmtypeIndex=_curindex;
		$("#alarmtype").val(_curindex)
		
		
    }

    function ChangeAlarmtypeInd(_curindex) {
		if(_curindex==61){
		  _curindex=6;
		  specialAlarmIndex=1;
		}else if(_curindex==62){
		  _curindex=6;
		  specialAlarmIndex=2;
		}
		curalarmtypeIndex=_curindex;
		$("#alarmtype").val(_curindex);
	}
	
	
function getAlarmName(ind){
    if(ind==4) return "<a href='#' onClick='previewplay(4);'><img src='images/sun-small.png' style='padding-right:5px; position: relative; top: 0.5em;' width='39' height='26' border='0'/></a>";
    else if(ind==3) return "<a href='#' onClick='previewplay(3);'> <img src='images/plane-small.png' style='padding-right:5px; position: relative; top: 0.5em;' width='25' height='25' border='0'/></a>";
    else if(ind==1) return "<a href='#' onClick='previewplay(1);'><img src='images/clock-small.png' style='padding-right:5px; position: relative; top: 0.5em;' width='16' height='37' border='0'/></a>";
	else if(ind==2) return "<a href='#' onClick='previewplay(1);'><img src='images/storm-small.png' style='padding-right:5px; position: relative; top: 0.5em;' width='39' height='37' border='0'/></a>";
	else if(ind==5) return "<a href='#' onClick='previewplay(5);'><img src='images/storm-small.png' style='padding-right:5px; position: relative; top: 0.5em;' width='39' height='37' border='0'/></a>";
	else if(ind==6 && specialAlarmIndex==1) 
		return "<a href='#' onClick='changecurrentspecialid(1);previewplay(6); '><img src='images/funkyrooster-small.png' style='padding-right:5px; position: relative; top: 0.5em;' width='39' height='37' border='0' /></a>";
	else if(ind==6 && specialAlarmIndex==2) 
		return "<a href='#' onClick='changecurrentspecialid(2);previewplay(6); '><img src='images/vuvuzela.png' style='padding-right:5px; position: relative; top: 0.5em;' width='39' height='37' border='0' /></a>";
}


    function previewplay(_playID) {
		if(_playID=="61") {
			changecurrentspecialid(1);
			_playID=6;
		} else if(_playID=="62") {
			changecurrentspecialid(2);
			_playID=6;
		}
        if (_playID >= 0 &&  _playID  <= 7) {
			
			
            loadandplay(_playID);
            maximizeplayer();
        } else {
            alert("Please select a Valid option");
        }
    }


    function readCookie(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(";");
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == " ") {
                c = c.substring(1, c.length);
            }
            if (c.indexOf(nameEQ) == 0) {
                return c.substring(nameEQ.length, c.length);
            }
        }
        return "";
    }


    function createCookie(name, value, days) {
        if (days) {
            var date = new Date;
            date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
            var expires = "; expires=" + date.toGMTString();
        } else {
            var expires = "";
        }
        document.cookie = name + "=" + value + expires + "; path=/";
    }


    function changeflashzipcode(zipval) {
		setVal("zipcode", zipval) ;
        mymovie = getSWF("infotimer");
        mymovie.changeflashzipcode(zipval);
    }

    function changecurrentspecialid(_fcode) {
		
		ChangemyAlarmtype("6");
				specialAlarmIndex=_fcode;
        mymovie = getSWF("infotimer");
        mymovie.changecurrentspecialid(_fcode);
    }
	
	
    function changeflashflightcode(_fcode) {
				setVal("flightcode", _fcode) ;
        mymovie = getSWF("infotimer");
        mymovie.changeflashflightcode(_fcode);
    }


    function loadandplay(alrmvalue) {
        mymovie = getSWF("infotimer");
        if (mymovie == null) {
            alert("null");
        }
        mymovie.loadandplay(alrmvalue);
    }


    function LoadweatherAlarm() {
        mymovie = getSWF("infotimer");
        mymovie.initializeweatherinfo();
    }


    function LoadTimeAlarm() {
        mymovie = getSWF("infotimer");
        mymovie.resetpreviewflag();
        hrval = change24hr(parseInt(document.getElementById("cmdHours").value), document.getElementById("cmbMeredien").value);
        mymovie.loadTimeSounds(hrval, document.getElementById("cmdMinute").value);
    }


    function change24hr(alarmHours, alarmMeredien) {
        if (alarmMeredien == "PM") {
            if (alarmHours == 12) {
                alarmHours = 0;
            }
            alarmHours = parseInt(alarmHours) + parseInt(12);
        } else if (alarmMeredien == "AM") {
            if (alarmHours == 12) {
                alarmHours = 0;
            }
        }
        return alarmHours;
    }


    function callExternalInterface() {
        getSWF("infotimer").goHome();
    }


    function maximizeplayer() {
        mymovie = getSWF("infotimer");
        mymovie.width = "487";
        mymovie.height = "540";
    }


    function hideplayer() {
        mymovie = getSWF("infotimer");
        mymovie.width = "1";
        mymovie.height = "1";
    }


    function playsounds() {
        mymovie = getSWF("infotimer");
        mymovie.playsounds();
    }


    function callbackplay(playIndex,specialindex) {
		//changecurrentspecialid(specialindex);
		//call now alarm type
		 //ChangemyAlarmtype(playIndex)
		 setTimeout("previewplay(" + curalarmtypeIndex + ")",15);
		 //setTimeout("previewplay(" + playIndex + ")",15);
    }

function showdebug(txt){
	try{
console.log(txt);	
	}catch(exception){}
}
function startcurrentplay(){
			
}

function showFlash(srno,playindex,specialindex) {
	document.getElementById("divFlash").style.display = "inline";
	document.getElementById("divFlash").style.visibility = "visible";
	clearTimer(srno);
	$.ajax({
 		type: "POST",
 		url: "/social-alarm-impl.php?u=" + document.social_form.u.value + "&cat=" + document.social_form.cat.value,
 		data: {},
 		dataType: "html",
 		async: false,
 		success: function(odata){
			if(odata=="")
				callbackplay(playindex,specialindex);
			else{
				$("a[href='alarm-clock-message.php']").attr('href', "alarm-clock-message.php?u=" + document.social_form.u.value + "&cat=" + document.social_form.cat.value);
				$('#hidden_link').trigger('click');
			}
		},
 		error: function(){
			alert("error");
			callbackplay(playindex,specialindex);
		}
 	});
}

function setUserAutomateAlarm(userhrs,userminutes,meridien){
  mymovie = getSWF("infotimer");
  if (mymovie == null) {
	setTimeout("setUserAutomateAlarm(" + userhrs +"," + userminutes + ",'"+ meridien + "')", 3000);
	return;
  }
  document.getElementById("cmbMeredien").value = meridien;
  document.getElementById("cmdHours").value = userhrs;
  document.getElementById("cmdMinute").value = userminutes;
  setAlarm();
}

function setUserAutomateAlarmType(atype){
  ChangemyAlarmtype(atype);
}

function hideFlash()
		{
		hideplayer();
		}

