$(function() {
		
		$('#playBigBtn').button({
			text: true
		})
		.click(function() {
			$('#play').button().click();
		});
		
		$('#play').button({
			text: false,
			icons: {
				primary: 'ui-icon-play'
			},
		})
		.click(function() {
			var options;
			if ($(this).text() == 'play') {
				options = {
					label: 'pause',
					icons: {
						primary: 'ui-icon-pause'
					}
				};
                if (currentFilename == null)
                {
                    $('#videoitem1').click();
                }
                else
                {
                    if (isUsingFlash)
                    {
                        if (!isAudio && $('#infoContainer').is(':hidden'))
                        {
                            $('#infoContainer').slideToggle(200);
                            setTimeout(function() { $('#infoContainer').slideToggle(200); }, 4000);
                        }
                        flashMediaPlayer.sendEvent('PLAY');
                    }
                    else
                    {
                        $("#progressbar").slider("option", "value", 0);
                        mediaPlayer.load();
                        mediaPlayer.play();
                        if (!isAudio)
                        {
                            imagePlayer.style.display = "none";
                            videoPlayer.style.display = "";
                        }
                    }
                    isStarted = false;
                }
			} else {
				options = {
					label: 'play',
					icons: {
						primary: 'ui-icon-play'
					}
				};
                if (isUsingFlash)
                {
                    flashMediaPlayer.sendEvent('PLAY'); // toggles play/pause
                }
                else
                {
                    mediaPlayer.pause();
                }
				togglePlayOverlay(true);
			}
			$(this).button('option', options);
		});
		$('#stop').button({
			text: false,
			icons: {
				primary: 'ui-icon-stop'
			}
		})
		.click(function() {
			$('#play').button('option', {
				label: 'play',
				icons: {
					primary: 'ui-icon-play'
				}
			});
            if (isUsingFlash)
            {
                flashMediaPlayer.sendEvent('STOP');
                flashDuration = -1;
            }
            else
            {
                mediaPlayer.pause();
                mediaPlayer.currentTime = 0;
            }
            isStarted = false;
			resetPositionValues();
			togglePlayOverlay(true);
		});
        
        $('#morevidbtn').button({
			text: true,
			icons: {
				primary: 'ui-icon-circle-arrow-s'
			},
		});
        $('#prevvidbtn').button({
			text: true,
			icons: {
				primary: 'ui-icon-circle-arrow-n'
			},
		});
        $('#moreaudiobtn').button({
			text: true,
			icons: {
				primary: 'ui-icon-circle-arrow-s'
			},
		});
        $('#prevaudiobtn').button({
			text: true,
			icons: {
				primary: 'ui-icon-circle-arrow-n'
			},
		});
        
		$("#progressbar").slider({
			value: 0,
			animate: false,
			start: function(event, ui) { updateProgress = false; },
			slide: function(event, ui) 
            {
                if (isUsingFlash)
                {
                    togglePlayOverlay(false);
                    if (flashMediaPlayer.getConfig().state == "IDLE")
                    {
                        flashSeek = (ui.value / 100);
                        flashMediaPlayer.sendEvent('PLAY');
                    }
                    else if (flashDuration > 0)
                    {
                        flashMediaPlayer.sendEvent('SEEK', (ui.value / 100) * flashDuration);
                        var playOptions = { label: 'pause', icons: { primary: 'ui-icon-pause' }};
                        $('#play').button('option', playOptions);
                    }
                }
                else
                {
                    mediaPlayer.currentTime = (ui.value / 100) * mediaPlayer.duration;
                }
            },
			stop: function(event, ui) 
            { 
                updateProgress = true;
                if (!isUsingFlash && $('#play').button().text() == 'play')
                {
                    $('#play').button().click();
                }
            }
		});
		
        $("#tabs").tabs();
        $(".scrollable").scrollable({ vertical: true, mousewheel: true });
        videoPlayer = $('#videoPlayerNode').get(0);
        audioPlayer = $('#audioPlayerNode').get(0); 
        imagePlayer = $('#playerViewImage').get(0);
        flashInstalled = FlashDetect.installed && navigator.userAgent.indexOf('Safari') == -1;
	});

