$(document).ready(function(){	
	if(navigator.platform.indexOf('Linux') == 0 || navigator.platform.indexOf('Mac') == 0) {
		$('#channels  .ul_1').css('margin-left', '74px');  // upsizing gear
		$('#channels  .ul_2').css('margin-left', '140px'); // upsizing brands
		$('#channels  .ul_3').css('margin-left', '202px'); // upsizing yourzelf
		$('#channels  .ul_4').css('margin-left', '296px'); // upsizing vids
		$('#channels  .ul_5').css('margin-left', '360px'); // upsizing talk
		$('#channels  .ul_6').css('margin-left', '446px'); // upsizing skills
		$('#channels  .ul_7').css('margin-left', '514px'); // upsizing events
		$('#channels  .ul_8').css('margin-left', '575px'); //GTV
		$('#channels  .ul_9').css('margin-left', '671px'); //upsizingn fun
		
		$('#channels li span').css('padding', '0 6px'); //channels
		$('#ticker-top p').css('width', '1090px'); //partner title
	}
	
	//Textarea counter		
	var counter = {
					'maxCharacterSize': 500,
					'originalStyle': 'originalTextareaInfo',
					'warningStyle' : 'warningTextareaInfo',
					'warningNumber': 40,
					'displayFormat' : '#input of the #max characters left'
	};
	
	var counterupload = {
					'maxCharacterSize': 300,
					'originalStyle': 'originalTextareaInfo',
					'warningStyle' : 'warningTextareaInfo',
					'warningNumber': 20,
					'displayFormat' : '#input of the #max characters left'
	};

	$('#counterarea').textareaCount(counter);
	$('#counterupload').textareaCount(counterupload);
	
	marqueeInit({
		uniqueid: 'logos',
		style: {
            'width': '100%',
            'height': '40px'
        },
		inc: 1,
		mouse: 'pause'
	});      
	
	$("#channels li").dropdown();
	
	var href= '';
	if(readCookie('theme') == 'white'){href = url_base+'resources/css/white.css';}
	
	$("<link/>").attr({'rel': 'stylesheet','type': 'text/css','href': href,'id': 'theme'}).appendTo($("head"));
	
	$('#switch-theme').css('cursor', 'pointer');
	$('#switch-theme').click(function(event) {
		event.preventDefault();
		if($('link#theme').attr('href') == '') {
			$('link#theme').attr('href', url_base+'resources/css/white.css');
			$('#switch-theme').attr('src', url_base+'resources/images/switch-theme_white.png');
			$('#logo img').attr('src', url_base+'resources/images/logo_white.jpg');
			$('#gitnaarnet').attr('src', url_base+'resources/images/powered-by_white.jpg');
			createCookie('theme', 'white', 7);
		} else {
			$('link#theme').attr('href', '');
			$('#switch-theme').attr('src', url_base+'resources/images/switch-theme.png');
			$('#logo img').attr('src', url_base+'resources/images/logo.jpg');
			$('#gitnaarnet').attr('src', url_base+'resources/images/powered-by.jpg');
			eraseCookie('theme');
		}
	});
	
	$('form > fieldset > a.submit').click(function(event){
		event.preventDefault();
		$(this).parents('form').submit();
	});
	
	$('form > fieldset > a.form-sub').click(function(event){
		event.preventDefault();
		$(this).parents('form').submit();
	});
	
	$('#sitesearch > #search').focus(function(){
		if($(this).val() == 'Enter keyword'){
			$(this).val('');
		}
	});
	
	$('#sitesearch > #search').blur(function(){
		if($(this).val() == ''){
			$(this).val('Enter keyword');
		}
	});
	
	$('#newsletter > #opt-in').focus(function(){
		if($(this).val() == 'Enter E-mail'){
			$(this).val('');
		}
	});
	
	$('#newsletter > #opt-in').blur(function(){
		if($(this).val() == ''){
			$(this).val('Enter E-mail');
		}
	});
	
	$('#vids').css('width', ($('.featured-vid').length * 240) +'px');
	
	//scrollpane parts
	var scrollPane = $('#featured-vids'),
	scrollContent = $('#vids');

	//build slider
	var scrollbar = $('#scrollbar').slider({
		slide: function( event, ui ){
			if(scrollContent.width() > scrollPane.width()){
				scrollContent.css( "margin-left", Math.round(
				ui.value / 100 * ( scrollPane.width() - scrollContent.width() )
				) + "px" );
			} else {
				scrollContent.css( "margin-left", 0 );
			}
		}
	});

	//append icon to handle
	var handleHelper = scrollbar.find( ".ui-slider-handle" )
	.mousedown(function() {
	scrollbar.width( handleHelper.width() );
	})
	.mouseup(function() {
	scrollbar.width( "100%" );
	})
	.append( "<span class='ui-icon ui-icon-grip-dotted-vertical'></span>" )
	.wrap( "<div class='ui-handle-helper-parent'></div>" ).parent();

	//size scrollbar and handle proportionally to scroll distance
	function sizeScrollbar() {
		var remainder = scrollContent.width() - scrollPane.width();
		var proportion = remainder / scrollContent.width();
		var handleSize = scrollPane.width() - ( proportion * scrollPane.width() );
		scrollbar.find( ".ui-slider-handle" ).css({
			width: handleSize,
			"margin-left": -handleSize / 2
		});
		handleHelper.width( "" ).width( scrollbar.width() - handleSize );
	}

	//reset slider value based on scroll content position
	function resetValue() {
		var remainder = scrollPane.width() - scrollContent.width();
		var leftVal = scrollContent.css( "margin-left" ) === "auto" ? 0 :
		parseInt( scrollContent.css( "margin-left" ) );
		var percentage = Math.round( leftVal / remainder * 100 );
		scrollbar.slider( "value", percentage );
	}

	//if the slider is 100% and window gets larger, reveal content
	function reflowContent() {
		var showing = scrollContent.width() + parseInt( scrollContent.css( "margin-left" ), 10 );
		var gap = scrollPane.width() - showing;
		if ( gap > 0 ) {
			scrollContent.css( "margin-left", parseInt( scrollContent.css( "margin-left" ), 10 ) + gap );
		}
	}
	
	//init scrollbar size
	setTimeout( sizeScrollbar, 10 );//safari wants a timeout
	
	//Make rel="new" go target="_blank"
	$('a[rel*=new]').attr('target', 'blank');
	
	//make sure all cols have the same height
	var col_height = 0
	for(var i = 0; i <= $('.col').length -1; i++)
	{
		col_height = ($('.col:eq('+ i +')').height() > col_height)? $('.col:eq('+ i +')').height() : col_height;
	}
	$('.col').height(col_height);
	
	//Voting
	var text;

	$('.video-voted > img').live('mouseover', function(){
		text = $(this).parent().children('.vote-value').text();
		$(this).parent().children('.vote-value').text('You already voted!');
	});

	$('.video-voted > img').live('mouseout', function(){
		$(this).parent().children('.vote-value').text(text);
	});

	$('.video-vote > img').live('mouseover', function(){
		$(this).nextAll('img').attr('src', url_base +'resources/images/vote.png');
		$(this).prevAll('img').andSelf().attr('src', url_base +'resources/images/vote-hover.png');

		var values = ['Terrible', 'Bad', 'Moderate', 'Good', 'Fantastic!'];
		var count = $(this).prevAll('img').andSelf().size();
		text = $(this).parent().children('.vote-value').text();
		$(this).parent().children('.vote-value').text(values[(count - 1)]);
	});

	$('.video-vote > img').live('mouseout', function(){
		if(!$(this).hasClass('voted')){
			$(this).parent().children('img:not(.voted)').attr('src', url_base +'resources/images/vote.png');
		}
		$(this).parent().children('.voted').attr('src', url_base +'resources/images/vote-hover.png');
		$(this).parent().children('.vote-value').text(text);
	});

	$('.video-vote > img').live('click', function(event){
		if(event.button === 0) {
			$(this).parent().children('.vote-value').text('');

			var id = $(this).parent().attr('id');
				id = id.split('_');
				id = id[1];

			$(this).nextAll('img').removeClass('voted');
			$(this).prevAll('img').andSelf().addClass('voted');

			var count = $(this).prevAll('img').andSelf().size();
			var element = $(this);

			$.ajax({
				type: 'post',
				url: url_base +'video/vote',
				data: 'rating='+ count +'&video='+ id,
				success: function(msg){
					text = msg;
					$(element).parent().children('.vote-value').text(msg);

					$(element).parent().removeClass('video-vote');
					$(element).parent().addClass('video-voted');
				}
			});
		}
	});
	
	$('.video_url').change(function(){
		alert($(this).value());
		var url = $(this).value();
			url = url.split('/');
		$(this).value(url[url.length -1]);
	});
	
	$('.delete').click(function(event){
	event.preventDefault();
	if(confirm('Do you really wanna delete this item?'))
	{
		window.location = $(this).attr('href');
	}
	})
	
	
	$('#ticker-tape').corner("bottom");
	$('.sub-channel').corner("bottom");
	
});

