var baseURL="http://www.redcmarketing.net/redctv/";

window.addEvent('domready', function(){
	theSlider= new Slider('sliderHolder','slider', {
			steps: 255,  // Steps from 0 to 255
			range: [0,$('tabContent').getElement('.active').getSize().x-($('tabContent').getSize().x-5)],
			wheel: true, // Using the mousewheel is possible too
			onChange: function(step){
			    $('tabContent').getElement('.active').setStyle('left', -step);
			}
		});
		
	$$('#tabMenu a').each(function(el) {
	    el.addEvent('click',function(e){
	        var ev=new Event(e).stop();
	        changeTab(el);
	    });
	});
	
	
	$$('.videoTitle').each(function(el) {
	    el.addEvent('click',function(e){
	        var ev=new Event(e).stop();
            setPlayer(el,e);
	    });
	});
	
	if ($('intialPlay')) {runPlayer($('IPURL').get('text'),$('IPname').get('text'),$('IPtitle').get('text'),$('IPdescription').get('text'));}
	
	$('searchForm').addEvent('submit', function(e) {
        var ev=new Event(e).stop();
        var searchRequest=new Request.HTML({url:'/wp-content/themes/redc/search.php', 
                                            onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript){
                                                if (responseHTML=="Empty") {
                                                    alert("No videos were found to match your search");
                                                } else {
                                                    $$('#tabMenu a').each(function (el) {
                                                        if(el.hasClass('active')) { el.removeClass('active'); }  
                                                        })
                                                    $$('.content').each(function(el) { 
                                                        el.setStyle('display','none');
                                                        el.removeClass('active');
                                                    })
                                                   
                                                   var searchResults=new Element('div', {'html': responseHTML});
                                                   searchResults.inject($('tabContent'), 'top');
                                                   
                                                    //responseTree.replaces($('tab0'));
                                                    //$('tab0').set('html',responseHTML);
                                                    $('tab0').setStyle('display','block');
                                                    $('tab0').addClass('active');
                                                    $$('.searchTitle').each(function(el) {
                                                        el.addEvent('click',function(e){
                                                            var ev=new Event(e).stop();
                                                            setPlayer(el,e);
                                                        });
                                                    });
                                                    resetSlider();
                                                }
                                            }}).post($('searchForm'));
    });
});

function changeTab(ael) {
    $$('#tabMenu a').each(function (el) {
        if(el.hasClass('active')) { el.removeClass('active'); }  
    })
    ael.addClass('active');
    $$('.content').each(function(el) { 
        el.setStyle('display','none');
        el.removeClass('active');
        })
    $('tab'+ael.id).setStyle('display','block');
    $('tab'+ael.id).addClass('active');
    resetSlider();
};

function resetSlider() {
    if ($('tabContent').getElement('.active').getSize().x>($('tabContent').getSize().x-5)) { 
		theSlider.max = $('tabContent').getElement('.active').getSize().x-($('tabContent').getSize().x-5);
		theSlider.range = theSlider.max - theSlider.min;
		theSlider.steps = theSlider.options.steps || theSlider.full;
		theSlider.stepSize = Math.abs(theSlider.range) / theSlider.steps;
		theSlider.stepWidth = theSlider.stepSize * theSlider.full / Math.abs(theSlider.range) ;
    } else { 
        theSlider.max = 0;
		theSlider.range = theSlider.max - theSlider.min;
		theSlider.steps = theSlider.options.steps || theSlider.full;
		theSlider.stepSize = Math.abs(theSlider.range) / theSlider.steps;
		theSlider.stepWidth = theSlider.stepSize * theSlider.full / Math.abs(theSlider.range) ;
    }
}
      
function setPlayer(el,e){
    var ev=new Event(e).stop();
    element=$(el).get('name');
    //description=$(el).getPrevious('videoDescription');
    runPlayer($(el).get('href'),$(el).get('name'),$(el).get('text'),$(element).getParent().getElement('.videoDescription').get('text'));
}

function runPlayer(URL,name,title,description){

    $('dynamicVideoTitle').set('text',title);
    $('dynamicVideoDescription').set('text',description);
    
    code=URL.match(/[0-9]{3}\/[0-9]{3}\/[0-9]{3}/);
    code2=code[0].replace(/\//g,"");
    $('shareLink').set('value',baseURL+"?a="+code2);
    
    
    var views = new Request({url: '/wp-content/themes/redc/incViews.php', method: 'get', link: 'cancel', onSuccess: function(text,XML) {
                            $('viewCount').set('text',text);
                            }}).send('URL='+URL);
                            
    

    flowplayer("innerMovieHolder", "/wp-content/themes/redc/7_swf/flowplayer.commercial-3.1.0.swf", {
        key: '#$cb09528cab73960316b',
        clip: {
            url: URL,
            onFinish: function (clip) { 
                    a=$('tabContent').getElement('.active').getProperty('class');
                    b=$('tabContent').getElement('.active').getProperty('id');
                    sectionCount=a.substring(0,a.indexOf(' '));
                    this.getScreen().animate({opacity: '0.3'},2000);
                    if ((sectionCount>1) && (b!="tab0")) {
                        plugin=$f().getPlugin('c1');
                        plugin.fadeIn(2000);
                        if (sectionCount>2) {
                            plugin=$f().getPlugin('c2');
                            plugin.fadeIn(2000);
                        }
                    }
                },
            onStart: function (clip) { 
                    this.getScreen().animate({opacity: '1'},2000);
                    b=$('tabContent').getElement('.active').getProperty('id');
                    if (b!="tab0") {
                        var HTMLinjection = new Request.HTML({onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript){
                                plugin=$f().getPlugin('c1');
                                plugin.fadeOut(2000, function() {
                                    this.setHtml(responseHTML);
                                    }
                                )
                            }}).post("/wp-content/themes/redc/videoselect.php?video="+name);
                        var HTMLinjection2 = new Request.HTML({onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript){
                                plugin=$f().getPlugin('c2');
                                plugin.fadeOut(2000, function() {
                                    this.setHtml(responseHTML);
                                    }
                                )
                            }}).post("/wp-content/themes/redc/videoselect.php?video="+name);
                    }
                    }     
        },
        canvas: {backgroundColor: "transparent"},
        play: {
            width:50,
            height:50
            },
        plugins: {
            controls: {
                buttonOverColor: '#728B94',
                backgroundGradient: [0.6,0.3,0,0,0],
                buttonColor: '#424242',
                timeColor: '#ffffff',
                backgroundColor: '#840115',
                bufferColor: '#3e040b',
                progressColor: '#2b0308',
                sliderColor: '#000000',
                volumeSliderGradient: 'none',
                borderRadius: '10',
                progressGradient: 'medium',
                tooltipTextColor: '#ffffff',
                tooltipColor: '#5F747C',
                durationColor: '#000000',
                bufferGradient: 'none',
                sliderGradient: 'none',
                timeBgColor: '#555555',
                volumeSliderColor: '#000000',
                opacity: 1.0
            },
            c1: {
                url:'flowplayer.content-3.1.0.swf',
                html: $('HTMLinjection').get('html'),
                backgroundColor: '#660000',
                backgroundGradient:'medium',	
                //border: 'none',
                top: '23%',
                width: 450,
                display: 'none',
                opacity: 0,
                height: 110,
                zIndex: 10,
                style: {'.tiny': {fontSize:1},
                        '.videoTitle': {fontSize:16, fontWeight: 'bold'}}
            },
            c2: {
                url:'flowplayer.content-3.1.0.swf',
                html: $('HTMLinjection2').get('html'),
                backgroundColor: '#660000',
                backgroundGradient:'medium',	
                //border: 'none',
                top: '70%',
                width: 450,
                display: 'none',
                opacity: 0,
                height: 110,
                zIndex: 20,
                style: {'.tiny': {fontSize:1},
                        '.videoTitle': {fontSize:16, fontWeight: 'bold'}}
            },
            c3: {
                url:'flowplayer.content-3.1.0.swf',
                html: $('noStream').get('html'),
                backgroundColor: '#660000',
                backgroundGradient:'medium',	
                //border: 'none',
                top: '45%',
                width: 300,
                display: 'none',
                opacity: 0,
                height: 140,
                zIndex: 20,
                style: {'.tiny': {fontSize:1},
                        '.noStreamHeader': {fontSize:16, fontWeight: 'bold'},
                        '.noStreamPara': {fontSize:12}}
            },
            c4: {
                url:'flowplayer.content-3.1.0.swf',
                html: $('otherError').get('html'),
                backgroundColor: '#660000',
                backgroundGradient:'medium',	
                //border: 'none',
                top: '45%',
                width: 300,
                display: 'none',
                opacity: 0,
                height: 140,
                zIndex: 20,
                style: {'.tiny': {fontSize:1},
                        '.noStreamHeader': {fontSize:16, fontWeight: 'bold'},
                        '.noStreamPara': {fontSize:12}}
            }
        },
        onError: function(code,message) {
            if ((code==200) || (code==201)) {
                plugin=$f().getPlugin('c3');
                plugin.fadeIn(2000);
            } else {
                plugin=$f().getPlugin('c4');
                plugin.fadeIn(2000);
            }
            return false;
            }   
    }); 
    
    }

