var critic_registeration_prompt_lang = "If you don't enter your address details then you won't be eligble to win an iPad.<br />  Would you like to complete your address details now?";

$(document).ready(function(){
	$('body').attr('class', 'js');
	
	//page functionality
	expandableList();
	
	//png fix 
	//alert(jQuery.browser.msie);
	//alert(jQuery.browser.version);
	if (jQuery.browser.msie && jQuery.browser.version <= 6.0) {
		correctPNG();
	}
	
	//effects
	if (!jQuery.browser.mozilla && !jQuery.browser.msie && !jQuery.browser.opera) {
		addRoundedCorners();
	}
	if (!jQuery.browser.msie && !jQuery.browser.version <= 6.0) {
		dropShadow();
	}
	
	//form validation
	if ($('#contact')) {
		//validation_contact();
	}
	if ($('#register')) {
		form_fieldset_slide();
		validation_register_critic();
	}
	if ($('#edit')) {
		//form_fieldset_slide();
		//validation_edit_critic();
	}
	
	//video upload
	if ($('#upload').attr('id') == 'upload') {
		swfu = new SWFUpload({ 
			upload_url : "http://vmshorts.local/upload.php", 
			flash_url : "http://vmshorts.local/assets/flash/swfupload.swf", 
			file_size_limit : "120 MB",
			
			button_placeholder_id : "upload",
			button_width: "100",
			button_height: "30",
			button_text: "<span>hello</span>",
			
			file_dialog_complete_handler : fileDialogComplete,
			upload_start_handler : uploadStart,
			upload_progress_handler : uploadProgress,
			upload_error_handler : uploadError,
			upload_success_handler : uploadSuccess,
			upload_complete_handler : uploadComplete,		
			
			custom_settings : { 
				cancelButtonId : 'button_cancel',
				progressTarget : "upload_progress"
			} 
		}); 
	}

	//tabs
	$('.tabs').tabs();
	
});

function expandableList() {
	//expand list items
	$('ol.expandable-list li a.list-heading').click(function() {
			var parent = $(this).parent('li');
			if (parent.children('.copy-text').css('display') == 'block') {
				parent.children('.copy-text').slideUp('fast');
				//parent.children('span').children('img').attr('src', '/images/close_content.png');
			}
			else {
				parent.children('.copy-text').slideDown('fast');
				//parent.children('span').children('img').attr('src', '/images/open_content.png');
			}
	});
}

//form validation
function validation_register_critic () {
	var validator = $("#register").validate({
		rules: {
		    critic_username: "required",
		    critic_email: {
				required: true,
				email: true
			},
		    critic_confirm_email: {
				required: true,
				email:true,
				equalTo: "#email"
		    },
		    critic_password: {
		    	required: true,
		    	minlength: 5
		    },
		    critic_confirm_password: {
		    	required: true,
		    	minlength: 5,
		    	equalTo: "#password"
		    },
		    critic_terms: "required"
		},
		messages: {
			critic_username: "Enter your username",
			critic_email:	{
				required: 'Please enter a valid email address'
			},
			critic_confirm_email: {
				required: 'Please enter a valid email address',
				equalTo: 'The email addresses are different'
			},
			critic_password: {
				required: 'Pleasse enter a password',
				minlength:  jQuery.format ('Enter at least {0} characters')
			},
			critic_confirm_password: {
				required: 'Please confirm your password',
				equalTo: 'The passwords are different'
			},
			critic_terms: {
				required: 'You must agree to the terms'
			}
		},
		errorPlacement: function (error, element){
			if (element.is(":checkbox")) 
				error.insertAfter (element.next());
			else error.appendTo(element.parent());
		},
		submitHandler: function () {
			$('#page').prepend('<div id="overlay" class="overlay-bg"></div>');
			$('#overlay').prepend('<div class="overlay"><p>'+critic_registeration_prompt_lang+'</p><p class="btn-container"><a href="javascript:void(0);" title="yes" id="critic-reigstration-continue">YES</a><a href="javascript:void(0);" title="no" id="critic-reigstration-submit">NO</a></p></div>')
			if ($('#register').attr('id') == 'register') {
				$('#critic-reigstration-continue').click( function (){
					$('#overlay').fadeOut('fast').remove();
				});
				$('#critic-reigstration-submit').click( function (){
					$('#register').prepend('<input type="hidden" id="js_validation_prompt_true" />')
					$('#critic_save_and_continue').trigger('click');
				});
			}
			return false;
		}
	});
}

function validation_edit_critic () {
	var validator = $("#edit").validate({
		rules: {
		    critic_password: {
		    	required: true,
		    	minlength: 5
		    },
		    critic_confirm_password: {
		    	required: true,
		    	minlength: 5,
		    	equalTo: "#password"
		    },
		    critic_terms: "required"
		},
		messages: {
			critic_password: {
				required: 'Pleasse enter a password',
				minlength:  jQuery.format ('Enter at least {0} characters')
			},
			critic_confirm_password: {
				required: 'Please confirm your password',
				equalTo: 'The passwords are different'
			},
			critic_terms: {
				required: 'You must agree to the terms'
			}
		},
		errorPlacement: function (error, element){
				if (element.is(":checkbox"))
					error.insertAfter ( element.next() );
		}
	});
}

//form functionalality
function form_fieldset_slide () {
	$('form .fieldset_status').unbind('click');
	$('form .fieldset_status').click( function () {
		if ($(this).html() == 'close') {
			$(this).next('js_slide open').slideUp('slow').attr('class', 'js_slide closed');
			$(this).html('open');
		}
		else {
			$(this).next('js_slide closed').slideDown('slow').attr('class', 'js_slide open');
			$(this).html('close');
		}
	});
}

function addRoundedCorners() {	
	$('.logged_in').prepend('<span class="roundedCorner roundedCornerBottomRight" />');
	$('.logged_in').append('<span class="roundedCorner roundedCornerBottomLeft"/>');		
}


function correctPNG() // correctly handle PNG transparency in Win IE 5.5 & 6.
{
   var arVersion = navigator.appVersion.split("MSIE")
   var version = parseFloat(arVersion[1])
   if ((version >= 5.5) && (document.body.filters)) 
   {
      for(var i=0; i<document.images.length; i++)
      {
         var img = document.images[i]
         var imgName = img.src.toUpperCase()
         if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
         {
            var imgID = (img.id) ? "id='" + img.id + "' " : ""
            var imgClass = (img.className) ? "class='" + img.className + "' " : ""
            var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
            var imgStyle = "display:inline-block;" + img.style.cssText 
            if (img.align == "left") imgStyle = "float:left;" + imgStyle
            if (img.align == "right") imgStyle = "float:right;" + imgStyle
            if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
            var strNewHTML = "<span " + imgID + imgClass + imgTitle
            + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
            + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
            + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
            img.outerHTML = strNewHTML
            i = i-1
         }
      }
   }    
}



function dropShadow() {
	$('.content').dropShadow({left:5, top:5, opacity:0.2, blur:0});
	//$('.content_form').dropShadow({left:5, top:5, opacity:0.2, blur:0});
}

