var __debug = true;

var $document = $(document);
var _interval_checkHash;
var _interval_checkIFrameSrc;
var $content_iframe;

function debug(str) {
	if (__debug == true) {
		if (typeof(console)=='object') {
			console.log(str);
		} else {
			__debug = false;	
		};
	}
}

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

$document.ready(function() {
	$content_iframe = $('#content_iframe');
	_iFrameSrc = $content_iframe.attr('src');
	_interval_checkHash = setInterval('checkHash()',100);
	_interval_checkIFrameSrc = setInterval('checkIFrameSrc()',100);
	// init_player(true);
	$('a.linkTopify').linkTopify();
  showRadioPlayer();
})

function showRadioPlayer() {
  var so = new SWFObject("/asset/swf/radio_v6.swf", "radioswf", "578", "152", "10", "#FFFFF");
	so.addParam('allowfullscreen','true');
  so.addParam('wmode','transparent');
  so.addVariable('path','/playlist/default');
  so.write("radio");
}

/* ==== */


function content_height(height) {
  if(height > 600) {
    height = height + 100;
    debug("Setting content_iframe height to "+height+" pixels");
    $content_iframe=$('#content_iframe');
    //alert(height);
    $content_iframe.height(height - 140);
    $("#header").height(height+40);
    $("#root_container").height(height+40);
    $("#footer").css('top', height);
  }
}

var _hash = '';
function checkHash() {
  	var _detected_hash = document.location.hash.slice(1);
  	if (_detected_hash == "" || _detected_hash == "/") {
			_detected_hash = "/home";
  	}
  	if (_detected_hash != _hash) {
  		debug('Hash changed to ' + _detected_hash);
  		_hash = _detected_hash;
  		$content_iframe.attr('src',_hash);
  	}
}

var _iFrameSrc = ' ';
function checkIFrameSrc() {
	var _detected_src = parent.frames['content_iframe'].location.pathname+parent.frames['content_iframe'].location.search;
	if(_detected_src != _iFrameSrc && _detected_src != "") {
		debug('Detected iFrame location changed to '+_detected_src);
		_iFrameSrc = _detected_src;
		if(window.location.hash != _detected_src) {
			window.location = '#'+_detected_src;
		}
	}
}

function divZindex(data) {
	if (data==2) {
		//$('#header #radio').css({overflow:'visible',height:'500px'});
	} else {
		//$('#header #radio').css({overflow:'hidden',height:'60px'});	
	}
}

$.iFrameLoaded = function () {
  //console.log("IFRAME LOADED");
  //$("#loading").hide();
}

///////

function player() {
	return _id('radioswf');
}

function setSound(Comedy_ID,Comedy_Title,Comedy_URL,Related_URL){
	// alert('jsjs');
  document.getElementById("radioswf").setSound(Comedy_ID,Comedy_Title,Comedy_URL,Related_URL);
}

function control_player_playnow(id,title,url_audio,url_web,hidenotification) {
	debug("Player_PlayNow ("+id+", "+title+"):("+url_audio+", "+url_web+")");
	if (hidenotification === undefined) {
		notify("Now playing", title);
	};
  setSound(id,title,url_audio,url_web);    
}

function control_player_playnow_playlist(id, title) {
	debug("Player_PlayNow ("+id+", "+title);
	if (hidenotification === undefined) {
		notify("Now playing", title);
	};
  so = player();
  so.addVariable('path', '/playlist/playnow/'+id);
}

function control_player_play() {
	debug("Player_Play");
}

function control_player_next() {
	debug("Player_Next");
	player().play_next();
}

function control_player_prev() {
	debug("Player_Prev");
	player().play_prev();
}

function control_player_pause() {
	debug("Player_Pause");
	player().soundPause();
	//player().soundUnPause();
}

function control_player_volume(amount) {
	debug("Player_Volume ("+amount+")");
	player().volume(amount);
}

// Notifier

function notify(title,body) {
	$.growl(title,body)	
}

//

$.fn.linkTopify = function() {
	return this.each(function() {
		$this = $(this);
		var _href = $this.attr('href');
		$this.click(function() {
			window.location = _href;
			scroll(0,0);
			return false;	
		});
	})	
}
