	
	
	$(document).ready(function() {
		
		$('#events_lister input').change(function(){
			s = 0;
			$('#events_lister input').each(function(){
				if ( $(this).attr('checked') && events[$(this).val()] )	{
					s += events[$(this).val()];
				}
				if ( s > 0 ){
					$('#multiple_buy_form').show();
					$('#select_event_to_buy').hide();
				}else{
					$('#multiple_buy_form').hide();
					$('#select_event_to_buy').show();
				}
			});
		});
		
		$('#buy_multiple_tickets').click(function(){
			s = 0;
			$('#events_lister input').each(function(){
				if ( $(this).attr('checked') && events[$(this).val()] )	{
					s += events[$(this).val()];
					v = $('#multiple_buy_form input[name=custom]').val();
					$('#multiple_buy_form input[name=custom]').val( v + $(this).val() + '|' );
					v = $('#multiple_buy_form input[name=item_name]').val();
					$('#multiple_buy_form input[name=item_name]').val( v + '`' + $(this).attr('rel') + '`,' );
				}
			});
			v = $('#multiple_buy_form input[name=item_name]').val();
			$('#multiple_buy_form input[name=item_name]').val( v.substring(0, v.length-1) );
			$('#multiple_buy_form input[name=amount]').val( s );
			
			return true;
		});
		
		i = 1;
		
		// user rated
		if ( typeof(user_rated) == 'undefined' ) 
			user_rated = 0;
		
		if ( user_rated > 0 ) {
			user_rated = parseInt( user_rated );
			
			tmp = 1;
			$('a[rel="vote_img"]').each(function() {
				if ( tmp <= user_rated  ){
					$(this).attr('class', 'starF' );
					tmp++;
				}
				$(this).click(function(){return false;});
			});
		}else{
			// vote images
			$('a[rel="vote_img"]').each(function() {
				$(this).attr('id', i );
				i++;
				
				// on mouse over
				$(this).mouseover(function(){
					id = $(this).attr('id');
					for ( j = 1 ; j <= id ; j++ ) {
						$('a[id="' + j + '"]').attr('class', 'starF' );
					}
				});
				// on mouse out
				$(this).mouseout(function(){
					id = $(this).attr('id');
					for ( j = 1 ; j <= id ; j++ ) {
						$('a[id="' + j + '"]').attr('class', 'starE' );
					}
				});
				
				// on click, save the vote
				$(this).click(function(){
					no_history_server_post( { m : 'show_vote' , n : $(this).attr('id') , id : show_id } , user_voted );
					return false;
				});
			});
		}
		
		$('#flag_reason_trigger').click(function(){
			$('#flag_reason').toggle();
			return false;
		});
		
		$('#flag_send').click(function(){
			no_history_server_post( { m : 'show_flag', id : $('[name=flag_reason]').val(), show_id:show_id }, user_flaged );
			return false;
		});
		
		$('#bbutyes').click(function(){ $('#bbut_frm').submit(); return false;});
		$('#bbutno').click(function(){ $('#bbut').show(); $('#buy_confirm').hide();	return false;});
		$('#bbut').click(function(){ $(this).hide(); $('#buy_confirm').show('fast');return false;});
		
		// new comment
		$('#add_new_comment').click(function(){
			v = $('#new_comment').val();
			if ( $('#new_comment').val() == '' )
				display_error( 'Please write a comment.' );
			else if ( v.length > 500 )
				display_error( 'Comment length must have MAXIMUM 500 chars.' );
			else{
				$('#comments_loader').show();
				no_history_server_post( { m : 'show_comments', comment : $('#new_comment').val() , id : show_id, a:'add' } , add_show_comment );
				$('#comments_loader').hide();
			}
			return false;
		});
		
		// follow
		$('#follow').click(function(){
			no_history_server_post( { m : 'show_follow', action : 'follow', id : show_id }, follow_callback );
			return false;
		});
		$('#stop_follow').click(function(){
			no_history_server_post( { m : 'show_follow', action : 'stop_follow', id : show_id }, stop_follow_callback );
			return false;
		});
		
		$('#new_comment').keyup(function(){
			v = $(this).val(); x = v.length; 
			if ( x < 1 ) x = '0';
			$('#no_chars').html(x);
		});
		
		// Show tooltip for live event.
		$('#scheduledLiveEvent .title').tooltip({ 
			tip: '#tooltip',
			opacity: 0.95,
			position: 'top left',
			offset: [10, 334],
			predelay: 500
		});

		$('#scheduledLiveEvent .title').hover(function (){
			$(this).addClass('titleActive')
		}, function() {
			$(this).removeClass('titleActive');
		});
		
		$("div.rightColumn").tabs("div.rightColumn div.contentBox div.accordion", { tabs: 'div.topLeft h2.accordion', effect: 'slide', initialIndex: 0 });

		$('#show_embed_resolutions').click(function() {
			if ($(this).hasClass('embedSelected')) {
				closeShowEmbedResolutions();
			} else {
				$(this).removeClass('embed').addClass('embedSelected');
				$('#customizeEmbedStream').addClass('customizeEmbedStreamSelected');
				$('#closeShowEmbedResolutions').css('visibility', 'visible');
				$('#customEmbedResolutions').show(300);
			}
			return false;
		});
		$('#closeShowEmbedResolutions').click(function() {
			closeShowEmbedResolutions();
			return false;
		});
		
		function closeShowEmbedResolutions() {
			$('#show_embed_resolutions').removeClass('embedSelected').addClass('embed');
			$('#customizeEmbedStream').removeClass('customizeEmbedStreamSelected');
			$('#closeShowEmbedResolutions').css('visibility', 'hidden');
			$('#customEmbedResolutions').hide(300);
		}
        
        $('#show_js_embed_code').click(function() {
            if ($(this).hasClass('embedSelected')) {
                closeShowJSEmbed();
            } else {
                $(this).removeClass('embed').addClass('embedSelected');
                //$('#customizeEmbedStream').addClass('customizeEmbedStreamSelected');
                $('#closeShowJSEmbed').css('visibility', 'visible');
                $('#customJSEmbed').show(300);
            }
            return false;
        });
        $('#closeShowJSEmbed').click(function() {
            closeShowJSEmbed();
            return false;
        });
        
        function closeShowJSEmbed() {
            $('#show_js_embed_code').removeClass('embedSelected').addClass('embed');
            //$('#customizeEmbedStream').removeClass('customizeEmbedStreamSelected');
            $('#closeShowJSEmbed').css('visibility', 'hidden');
            $('#customJSEmbed').hide(300);
        }
	});
	
	// follow
	function follow_callback( data ) {
		
		if ( data['err'] ){
			display_error( data['err'] );
		}else{
			$('#follow_div').hide();
			$('#stop_follow_div').show();
		}
	}
	function stop_follow_callback( data ) {
		
		if ( data['err'] ){
			display_error( data['err'] );
		}else{
			$('#follow_div').show();
			$('#stop_follow_div').hide();
		}
	}
	
	// the user has flaged a show. change the page .. .
	function user_flaged( data )
	{
		if ( data['err'] )
			display_error( data['err'] );
		else {
			// do html changes
			//$('#flag_reason_trigger').remove();
			$('#flag_reason').empty().append('Thank you for your interest.');
		}
	}
	
	// the user has voted. change the page .. .
	function user_voted( data )
	{
		if ( data['err'] )
			display_error( data['err'] );
		else {
			// do html changes
			$('.img_show_rate').empty().append('Thank you for your vote.');
			$('.img_show_rate_d').empty().append( data['content'] );
			
		}
	}
	
	// build comments section
	function build_box_content( lpage , type )
	{
		page = lpage;
		
		$('#' + type).empty().append( loader() );
		
		no_history_server_post( { m : type , page : lpage , show_id : show_id, user_id:user_id } , function( data ) {
			if ( data['err'] ) {
				display_error( data['err'] );
			}else if ( data['content'] && data['content'] ){
				$('#' + type).empty().append( data['content'] );
			}
			
			if ( typeof do_footer == 'function' )
				do_footer();
		} );
	}
	
	//keep compatibility with pagination
	function show_comments(lpage){ build_box_content( lpage , 'show_comments' ) }
	function show_uploaded(lpage){ build_box_content( lpage , 'show_uploaded' ) }
	function show_recorded(lpage){ build_box_content( lpage , 'show_recorded' ) }
	function show_other_channels(lpage){ build_box_content( lpage , 'show_other_channels' ) }

	
	// adds a comment to the comment section of the show, and refreshes the comments
	function add_show_comment( data )
	{
		if ( data['err'] )
			display_error( data['err'] );
		else {
			build_box_content( 1, 'show_comments' );
			$('#new_comment').val('');
		}
	}
	
	function delete_comment(id)
	{
		if ( confirm('Are you sure you want to delete this comment ? ') ) {
			no_history_server_post( { m:'show_comments', id:id, show_id:show_id, a:'delete' },function() {
				build_box_content( page, 'show_comments' );
			});
		}
		return false;
	}
	
	function showJustStarted(){
		$('#schdule_annonce').next().hide();
		$('#schdule_annonce').hide();
	}
	
	function do_footer()
	{
	}

	$(document).ready(function(){
		if ( typeof build_show_comments != 'function'  )
			do_footer();
			
		$('#broadcast_now').mouseover(function(){ $(this).attr('src', www_root + 'public/' + template_path + '/images/design-show/broadcast_on.png'); });
		$('#broadcast_now').mouseout(function(){ $(this).attr('src', www_root + 'public/' + template_path + '/images/design-show/broadcast.png'); });
		
		$('#user_action').mouseover(function(){ $(this).attr('src', www_root + 'public/' + template_path + '/images/design-show/' + $(this).attr('title') + '_on.png'); });
		$('#user_action').mouseout(function(){ $(this).attr('src', www_root + 'public/' + template_path + '/images/design-show/' + $(this).attr('title') + '.png'); });
	});
	
  function findPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
		while(1) {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }

function opencina() {
	window.open(www_root + 'popup_cina/show/' + show_name, 'cinapop'+show_id,'location=0,status=0,menubar=0,resizable=0,width='+screen.availWidth+',height='+screen.availHeight);
}

function change_input_values(width, height, autoplay) {
	if (width == 0) {
		width = $('#width_id').val();
	} else {
		$('#width_id').val(width);
	}

	if (height == 0) {
		height = $('#height_id').val();
	} else {
		$('#height_id').val(height);
	}

	if (autoplay == null) {
		autoplay = $('input:radio[name=autoplay]:checked').val();
	} else {
		if ($('input:radio[name=autoplay]:checked').val() != autoplay) {
			$('input:radio[name=autoplay]:checked').removeAttr('checked');
			$('input:radio[name=autoplay][value=' + autoplay + ']').attr('checked', true);
		}
	}

	embed_replaced = embed_txt.replace(/#width#/gi, width);
	embed_replaced = embed_replaced.replace(/#height#/gi, height);
	embed_replaced = embed_replaced.replace(/#autoplay#/gi, autoplay);

	$('input#video_embed_code_id').val(embed_replaced);
	$('input#code_embed_id').val(embed_replaced);
}

function change_jsembed_values() {

    autoplay = $('input:checkbox[name=autoplayjs]:checked').val();
    displaychat = $('input:checkbox[name=showchat]:checked').val();
    if(typeof autoplay == 'undefined') {
        autoplay = 'no';
    }
    if(typeof displaychat == 'undefined') {
        displaychat = 0;
    }

    embed_replaced = embed_js_txt.replace(/#displaychat#/gi, displaychat);
    embed_replaced = embed_replaced.replace(/#autoplay#/gi, autoplay);

    $('input#show_js_embed_code').val(embed_replaced);
}
