var last_quality = '';
var last_file    = '';
var player       = null;
var meta         = {};
var fp           = null;

function spawnFlash(uid, quality) {
    quality = parseInt(quality);
    if (quality != 'undefined' && quality > 0 && !isNaN(quality)) {
        useQuality = quality;
    } else {
        useQuality = parseInt(readCookie('flashquality'));
    }
    if (parseInt(useQuality) < 1 || parseInt(useQuality) > 2 || isNaN(useQuality)) {
        useQuality = 1;
    }
    
    createCookie('flashquality', useQuality, 90);

    file = meta[uid].q[useQuality];
    
    if (file == last_file) {
        return false;
    }
    if (player == true) {
        fp.play([
                {
                    url: meta[uid].t,
                    scaling: 'orig'
                },
                
                {
                    autoPlay: false,
                    url: file,
                    // autoBuffering: true,
                    scaling: 'orig'
                }
        ]);        
    } else {
        document.getElementById('flash').href = file;
        if (typeof(flowplayer) != 'undefined') {
            fp = flowplayer('flash', flashplayer, { 
                key: '$0253113dc777dd05696',
				play:{
					label: null, 
					replayLabel: null },
                playlist: [
                    {
                        url: meta[uid].t,
                        scaling: 'orig'
                    },
                    
                    {
                        autoPlay: false,
                        url: file,
                        // autoBuffering: true,
                        scaling: 'orig'
                    }
                ],
                
                plugins: { 
                 
                    // The controlbar is called "controls". By tweaking this, you can modify its look and feel 
                    controls: { 
                        timeColor: '#ff6600', 
                        progressColor: '#ff6600',
                        sliderColor: '#4A4541'
                    } 
                }

            });
        }

        player = true;
    }
	
	/*
    if (document.getElementById('bandwidth_' + uid + '_1')) {
        document.getElementById('bandwidth_' + uid + '_1').className = 'normal internal-link';
        document.getElementById('bandwidth_' + uid + '_2').className = 'normal internal-link';
        document.getElementById('bandwidth_' + uid + '_3').className = 'normal internal-link';
    }

    if (document.getElementById('bandwidth_' + uid + '_' + useQuality)) {
        document.getElementById('bandwidth_' + uid + '_' + useQuality).className = 'active internal-link';
    }*/

    last_quality = useQuality;
    last_file    = file;
    
    document.getElementById('videoinfo').innerHTML = document.getElementById('window' + uid).innerHTML;

    return false;
}

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 setThumb(el, url) {
    linkId = el.id;
    if (url == '') {
        $('#JT').remove();
        return true;
    }

	title="&nbsp;";
	var de = document.documentElement;
	var w = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var hasArea = w - getAbsoluteLeft(linkId);
	var clickElementy = getAbsoluteTop(linkId) - 80; //set y position
	
    params = {};
	params['width'] = 200;
	
	$("body").append("<div id='JT' style='xwidth:"+params['width']*1+"px'><img src='" + url + "' /></div>");
	var arrowOffset = -getElementWidth(linkId) + 11;
	//var clickElementx = getAbsoluteLeft(linkId) + arrowOffset; //set x position //
	
	$('#JT').css({left: clickElementx+"px", top: clickElementy+"px"});
	$('#JT').show();
}

function getElementWidth(objectId) {
	x = document.getElementById(objectId);
	return x.offsetWidth;
}

function getAbsoluteLeft(objectId) {
	// Get an object left position from the upper left viewport corner
	o = document.getElementById(objectId)
	oLeft = o.offsetLeft            // Get left position from the parent object
	while(o.offsetParent!=null) {   // Parse the parent hierarchy up to the document element
		oParent = o.offsetParent    // Get parent object reference
		oLeft += oParent.offsetLeft // Add parent left position
		o = oParent
	}
	return oLeft
}

function getAbsoluteTop(objectId) {
	// Get an object top position from the upper left viewport corner
	o = document.getElementById(objectId)
	oTop = o.offsetTop            // Get top position from the parent object
	while(o.offsetParent!=null) { // Parse the parent hierarchy up to the document element
		oParent = o.offsetParent  // Get parent object reference
		oTop += oParent.offsetTop // Add parent top position
		o = oParent
	}
	return oTop
}

function blockEvents(evt) {
    if(evt.target){
        evt.preventDefault();
    }else{
        evt.returnValue = false;
    }
}