﻿// JavaScript Document
var lst = new Array();
lst[0] = {
    file: "./objects/Azan_FerozAlam.mp3"
}
lst[1] = {
    file: "./objects/Fajr.mp3"
}

var player;
var currentItem = -1;
var previousItem = -1;
var singleItem = 0;
var switching = 0;
var currentState;
function playerReady(thePlayer){
    player = document.getElementById(thePlayer.id);
    addListeners();
}

function addListeners(){
    if (player) {
        player.addControllerListener("ITEM", "itemListener");
        player.addModelListener("STATE", "stateListener");
        player.sendEvent('LOAD', lst)
    }
    else {
        setTimeout("addListeners()", 100);
    }
}

function itemListener(obj){
    if (obj.index != currentItem) {
        previousItem = currentItem;
        currentItem = obj.index;
    }
    if (previousItem == 9 || (singleItem == 1 && switching != 1)) {
        document.getElementById("Arabic" + previousItem).style.background = "";
        player.sendEvent('STOP');
        previousItem = -1;
        singleItem = 0;
        switching = 0;
    }
    else {
        //highlight(currentItem);
    }
}

function highlight(currentItem){
    if (previousItem != -1) {
        document.getElementById("Arabic" + previousItem).style.background = "";
    }
    document.getElementById("Arabic" + currentItem).style.background = "#C9EAC4";
    //window.location.hash="Arabic"+currentItem;
}


function stateListener(obj){ //IDLE, BUFFERING, PLAYING, PAUSED, COMPLETED
    currentState = obj.newstate;
    previousState = obj.oldstate;
    if (currentState == "COMPLETED") {
        player.sendEvent('STOP');
    }
}

var item;
function myPlayItem(myItem) {	
	item = myItem;
	lazyPlayItem();
}

function lazyPlayItem(){    
    try {
        playItem();
    } 
    catch (err) {
        setTimeout("lazyPlayItem()", 250);
    }
}

function playItem(){
    if (currentState == "PLAYING" && item == currentItem) { //Do nothing...
        //			player.sendEvent('STOP');
        //			singleItem = 0;
        //			switching = 0;
    }
    else 
        if (currentState == "PLAYING") {
            switching = 1;
            player.sendEvent('ITEM', item);
            singleItem = 1;
        //highlight(item);
        }
        else {
            switching = 0;
            player.sendEvent('ITEM', item);
            singleItem = 1;
        //highlight(item);
        }
}

function createPlayer(){
    var flashvars = {
        file: "",
        autostart: "false",
        skin: "./objects/swift.swf"
    }
    var params = {
        allowfullscreen: "false",
        allowscriptaccess: "always",
        repeat: "none"
    }
    var attributes = {
        id: "player1",
        name: "player1"
    }
    swfobject.embedSWF("./objects/player.swf", "placeholder1", "400", "38", "9.0.115", false, flashvars, params, attributes);
}

