function setDefaultCommercial(id) {
    var current = $('default_commercial').value;
    
    if (current != id) {
        // Remove old default
        if ($('commercial_' + current)) {
            $('commercial_' + current).setAttribute("class", 'commercial');
            $('commercial_' + current).className = 'commercial';
//            display.hide('default_label_' + current);
            $('default_' + current).getElementsByTagName('input')[0].checked = false;
            //display.show('default_' + current);
        }
    
        // Set new default
        if ($('commercial_' + id)) {
            $('commercial_' + id).setAttribute("class", 'commercial default');
            $('commercial_' + id).className = 'commercial default';
            $('default_' + id).getElementsByTagName('input')[0].checked = true;
//            display.show('default_label_' + id);
            //display.hide('default_' + id);
        }
    }
    
    $('default_commercial').value = id;
}

function showEditCommercial(video_id) {
    if ($('edit_' + video_id) && 'details_' + video_id) { 
        $('edit_title_' + video_id).value = $('title_' + video_id).innerHTML;
        $('edit_description_' + video_id).value = $('description_' + video_id).innerHTML;
        $('edit_keywords_' + video_id).value = $('keywords_' + video_id).innerHTML;
        $('default_' + video_id).checked = false;
        
        display.show('edit_' + video_id);
        display.hide('details_' + video_id);
    }
}

function cancelEditCommercial(video_id) {
    if ($('edit_' + video_id) && 'details_' + video_id) { 
        display.hide('edit_' + video_id);
        display.show('details_' + video_id);
    }
}

function addCommercial(file) {
    if ($('uploading').value == 0) {
        var errors = "";
        var count = 0;
        var extensions = new Array("mpeg", "mpg", "wmv", "mov", "avi", "asf");
        filename = file.value.toLowerCase();
        if (filename != '') { 
            for (var i = 0; i < extensions.length; i++) {
                var regex = new RegExp( "\." + extensions[i] + "$", 'g');
                var match = filename.match( regex );
                if (match) { count++;	}
            }
            if (count == 0) { errors = "You are attempting to upload an unsupported file. Shopit supports .mpeg, .mpg, .wmv, .asf, .avi  or .mov filenames.\nPlease select another file."; }
        } else {
            errors = "You have not selected a file to upload.\n";
        }
        if (trim($('commercial_title').value) == '') { errors = 'Please enter a title.\n'; }
        if (errors != "") {
            alert(errors);
        } else {
            $('uploading').value = 1;
            $('loading_video').style.display = 'block';
            document.uploadvideo.submit();
            toggleForm(document.uploadvideo, true);
        }
    } else {
        alert('You are already uploading a commercial.  Please wait until the upload process has completed before trying to upload another commercial.');
    }
}

function toggleForm(form, action) {
    form.commercial_filename.disabled = action;
    form.commercial_title.disabled = action;
    form.commercial_description.disabled = action;
    form.commercial_keywords.disabled = action;
}

function editCommercial(video_id, user_id) {
	var url = 'http://www.shopit.com/index.php';
	var default_commercial = $('default_' + video_id).getElementsByTagName('input')[0].checked ? 1 : 0;
	var title = encodeURIComponent($('edit_title_' + video_id).value);
	var description = encodeURIComponent($('edit_description_' + video_id).value);
	var keywords = encodeURIComponent($('edit_keywords_' + video_id).value);
	var pars = 'Action=Ajax.EditCommercial&video_id=' + video_id + '&user_id=' + user_id + '&title=' + title + '&description=' + description + '&keywords=' + keywords + '&default=' + default_commercial;
	var myAjax = new Ajax.Request(
		url, 
		{
			method: 'post',
			parameters: pars,
			onSuccess: function(request) {
				var response = trim(request.responseText);
				switch (response) {
					case '1':
                        $('title_' + video_id).innerHTML = $('edit_title_' + video_id).value;
                        $('description_' + video_id).innerHTML = $('edit_description_' + video_id).value;
                        $('keywords_' + video_id).innerHTML = $('edit_keywords_' + video_id).value;

                        if (default_commercial == 1) {
                            setDefaultCommercial(video_id);
                        } else if (default_commercial == 0 && video_id == $('default_commercial').value) {
                            setDefaultCommercial('');                        
                        }

						message = '<img src="http://images.shopit.com/img/success_symbol_small.gif" border="0" \/><p>Successfully updated video information.<\/p>';
						break;
					default:
						message = '<img src="http://images.shopit.com/img/error_symbol_small.gif" border="0" \/><p>There was an error uploading your video.<\/p>';
				}
                display.hide('edit_' + video_id);
                display.show('details_' + video_id);
			}
		});	
}

function deleteCommercial(video_id, user_id) {
    
    if (confirm('Are you sure you want to delete "' + trim($('title_' + video_id).innerHTML) + '"?')) {
        var default_commercial = ($('default_commercial').value == video_id) ? 1 : 0;
        var url = 'http://www.shopit.com/index.php';
        var pars = 'Action=Ajax.DeleteCommercial&user_id=' + user_id + '&video_id=' + video_id + '&default=' + default_commercial;
        var myAjax = new Ajax.Request(
            url, 
            {
                method: 'post',
                parameters: pars,
                onSuccess: function(request) {
                    var response = trim(request.responseText);
                    switch (response) {
                        case '1':
                            message = '<img src="http://images.shopit.com/img/success_symbol_small.gif" border="0" \/><p>Successfully deleted video.<\/p>';
                            
                            if (default_commercial == 1) { $('default_commercial').value = ''; }
                            $('commercial_' + video_id).remove();
                            
                            var divs = $('commercials').getElementsByTagName('div');
                            var total_commercials = 0;
                            for (var i = 0; i < divs.length; i++) {
                                var id = divs[i].id;
                                if (id.match(/^commercial_/)) { total_commercials++; }
                            }
                            
                            if (total_commercials == 0) { 
                                display.show('no-commercials'); 
                                display.hide('pending_heading'); 
                            }
                            
                            break;
                        default:
                            message = '<img src="http://images.shopit.com/img/error_symbol_small.gif" border="0" \/><p>There was an error deleting your video.<\/p>';
                    }                    
                }
            });	
    }
};

function highlightStars(image, prefix) {
    for (var i = 1; i <= 5; i++) {
        var state = (i > image) ? 'in' : ''; 
        $(prefix + i).src = 'http://images.shopit.com/images/rating_star_' + state + 'active.gif';
    }
};

function rateVideo(userId, rate, comments) {
	var rating = 'NULL';
	for (var i = 0; i < rate.length; i++) {
	   if ( rate[i].checked ) {
		   rating = rate[i].value;
	   }
	}   

	if (rating != 'NULL') {
		var method = 'addRatingValue';
		var url = "http://www.shopit.com/ajaxWrapper.php";
		var pars = "method=" + method + "&userId=" + userId + "&rate=" + rating + "&comments=" + encodeURIComponent(comments.value);
			var myAjax = new Ajax.Request(
				url, 
				{
					method: 'post', 
					parameters: pars, 
					onComplete: rateVideoResponse
				});	
	} else {
		$('rate_video').innerHTML = "Please select a rating.";
		showElement($('rate_video'));
		setTimeout("hideElement( $('rate_video') )", 9000);
	}
};

function rateVideoResponse(req) {
	var message = trim(req.responseText);
	$('rate_video').innerHTML = message;
	showElement($('rate_video'));
	setTimeout("hideElement( $('rate_video') )", 9000);
};

function rateCommercial(user_id, video_id, rating) {
    var url = 'http://www.shopit.com/index.php';
	var pars = 'Action=Ajax.RateCommercial&user_id=' + user_id + '&video_id=' + video_id + '&rating=' + rating;
	var myAjax = new Ajax.Request(
		url, 
		{
			method: 'post',
			parameters: pars,
			onSuccess: function(req) {
				var response = trim(req.responseText);
				if (response != 'error') {
				  $('video-rating').innerHTML = response;
				  $('rating_response').innerHTML = $('rating_response_msg').value;
				  document.getElementById('rating_response').style.display = 'block';
				  setTimeout("document.getElementById('rating_response').style.display = 'none'", 5000);
				}
			}
		});	
};

function cancelUpload() {
    toggleForm(document.uploadvideo, false);
    document.uploadvideo.reset();
    display.hide('upload');
    $('loading_video').style.display = 'none';
    $('response_video').style.display = 'none';
    if ($('uploading').value == 1) {
        window.location.reload();
    }
    $('uploading').value = 0;
}