	var IS_IPHONE = ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) ? true : false;

	var streamBoxPreload = [
		"/images/t2/images/popup-top.png",
		"/images/t2/images/popup-middle.png",
		"/images/t2/images/popup-bottom.png",
		"/images/t2/images/popup-button.png",
		"/images/t2/images/popup-button-selected.png",
		"/images/t2/images/popup-check.png"
	];
    var webAppPreload = [
        ""//"/images/t2/std-button-blue.png"
    ];

	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 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 null;
	}
	function eraseCookie(name) {
		createCookie(name,"",-1);
	}
	function streamBoxShow() {
		if ($('#streamBox').css('display') == 'none') {
			$('#streamBoxUnderlay').css('display', 'block');
			$('#streamBoxUnderlay').css('height', '' + (window.innerHeight + window.pageYOffset) + 'px');
			$('#streamBox').css('top', window.pageYOffset + (window.innerHeight/5 - $('#streamBox').height()/2) + 'px');
			$('#streamBox').css('display', 'block');
		}
		else
			$('#streamBox').css('display', 'none');
	}
	function streamBoxHideUnderlay() {
		$('#streamBoxUnderlay').css('display', 'none');
		$('#streamBox').css('display', 'none');
	}
	function streamBoxSet(type) {
		$('#streamBox .streamOptions a').each(function() {
			if ($(this).attr('rel') == type)
				$(this).addClass('selected');
			else {
				if ($(this).hasClass('selected'))
					$(this).removeClass('selected');
			}
		});
	}	
	function setupStreamBox() {
		var underlayHTML = "<div style='display: none' id='streamBoxUnderlay'></div";
		var imgs = new Array();

		for (var i=0, l=streamBoxPreload.length;i<l;i++) {
			imgs.push(new Image());
			imgs[i].src = streamBoxPreload[i];
		}

		$('body').append(underlayHTML);
		if (IS_IPHONE) {
			$('#streamBoxUnderlay')[0].ontouchstart = function(e) {
				e.preventDefault();
				streamBoxHideUnderlay();
			}
		}
		else
			$('#streamBoxUnderlay').click(streamBoxHideUnderlay);

		$('#streamBox .streamOptions a').each(function(i) {
			if (IS_IPHONE) {
				this.ontouchstart = function(e) {
					e.preventDefault();
					streamBoxSet($(this).attr('rel'));
				}
				this.ontouchend = function(e) {
					e.preventDefault();
					createCookie('viewspeed', $(this).attr('rel'), 365);
					streamBoxSet($(this).attr('rel'));
					setTimeout('streamBoxHideUnderlay()', 100);
				}
			}
			else {
				$(this).click(function() {
					createCookie('viewspeed', $(this).attr('rel'), 365);
					streamBoxSet($(this).attr('rel'));
					setTimeout('streamBoxHideUnderlay()', 100);

				});
			}
		});


		$('.linkStreamBox').click(function() { streamBoxShow(); });

		// if the cookie is not set for streaming bitrate ask for it

		if (readCookie('bitrate') != null)
			streamBoxSet(readCookie('bitrate'));
		else if ($('#streamBox').hasClass('initSetting'))
			streamBoxShow();
	}

	function setupSearchBox() {
		// setup cancel button inside the search box
		$('#searchCancelButton').click(function() {
			searchCleanupHandlers();
			$('#searchBox .searchInput').val('');
			$('#searchBox').css('display', 'none');
		});

		// hook the search functionality up
		$('#searchBox .searchInput').keypress(function(e) { if (e.which == 13) searchDoSubmit(); });
		$('#searchSubmitButton').click(searchDoSubmit);

		function searchDoSubmit(e) {
			$('#searchBox').css('display', 'none');
			$('#searchBox form[name=searchBoxForm]')[0].submit();
		}

		// attach to the doSearchButton(s) on the page 
		$('.linkSearchButton').click(function(){
			$('#searchBox').css('display', 'block');
			var el = $('#searchBox .searchInput');
			el.css('color', '#999').val('search...').focus();
			el[0].cleanup = true;
			el.click(searchClearContent);
			el.keydown(searchClearContent);

		});
	}

	function searchClearContent() {
		searchCleanupHandlers();
		var el = $('#searchBox .searchInput');
		el.val('');
		el.css('color', '#000');
	}
	function searchCleanupHandlers() {
		var el = $('#searchBox .searchInput');
		if (el[0].cleanup) {
			el[0].cleanup = false;
			el.unbind('click', searchClearContent);
			el.unbind('keydown', searchClearContent);
		}
	}


	function jsddmToggle() {
		$d = $(this).find('ul').eq(0);
		if (window.jsddmMenuShowing && window.jsddmMenuShowing !== $d[0])
			$(window.jsddmMenuShowing).css('visibility', 'hidden');

		if ($d.css('visibility') == 'hidden') {
			$d.css('visibility', 'visible');
			window.jsddmMenuShowing = $d[0];
		}
		else {
			$d.css('visibility', 'hidden');
			window.jsddmMenuShowing = undefined;
		}
	}
    
function webappClick()
{
    $('#installAppBoxUnderlay').css('height', '' + (window.innerHeight + window.pageYOffset) + 'px');
    $('#installAppBoxUnderlay').css('display', 'block');
    $('#installAppBox').css('top', window.pageYOffset + 30 + 'px');
    $('#installAppBox').css('display', 'block');
}    
    /*** INSTALL WEB APP POPUP ***/
function setupWebAppPopup() {
    var imgs = new Array()
    for (var i=0, l=webAppPreload.length;i<l;i++) {
        imgs.push(new Image());
        imgs[i].src = webAppPreload[i];
    }

    var underlayHTML = "<div style='display: none' id='installAppBoxUnderlay'></div";
    $('body').append(underlayHTML);
    if (IS_IPHONE) {
        $('#installAppBoxUnderlay')[0].ontouchstart = function(e) {
            e.preventDefault();
            installAppBoxHideUnderlay();
        }
    }
    else
        $('#installAppBoxUnderlay').click(installAppBoxHideUnderlay);
    $('#installAppBox .closeButton').click(installAppBoxHideUnderlay);
    $('.linkToWebApp').click(function() {
        webappClick();
    });
}



function installAppBoxHideUnderlay() {
    $('#installAppBoxUnderlay').css('display', 'none');
    $('#installAppBox').css('display', 'none');
}

function setupMenus() {
	//mobile menu test
    $('.butn_menu select').change(function(i){
        var link = $(this).val();
        if (link.match(/^javascript:/)) {
            eval(link);
            return;
        }

        var option = $(this).find('option:selected')
        $(this).find('option:first').attr('selected', 'selected');
        if(link != ''){
            if($(option).hasClass('win')){
                window.open(link, 'newwin');
            }else{
                document.location = link;
            }
        }
    });
}    
    
$(document).ready(function() {
    setupSearchBox();
    setupStreamBox();
    setupWebAppPopup();
    setupMenus();
    $('select').focus().blur();
});
