$(function(){
    modernizr();
    required();
    tabable();
    beautifier();
    lightbox();
    debug();
});

function debug(){
    if($("#codeigniter_profiler").length>0){
	$("#codeigniter_profiler").css({
	    "position": "absolute",
	    "top": "100px",
	    "left": "50px",
	    "width": "60%",
	    "height": "500px",
	    "margin-left": "250px",
	    "overflow": "auto",
	    "display": "none"
	});
	$("<a>",{ "id": "codeigniter_profiler_btn", "href":"#", "class":"button"})
	    .css({
		"z-index":"99",
		"position":"fixed",
		"top":"0",
		"left":"0"})
	    .addClass('profiler-off')
	    .html('Show Profiler')
	    .click(function(ev){
		$("#codeigniter_profiler").toggle();
		if($(this).hasClass("profiler-off"))
		    $(this)
			.removeClass("profiler-off")
			.html('Close Profiler');
		else
		    $(this)
			.addClass("profiler-off")
			.html('Show Profiler')
		})
	    .appendTo("body");
    }
}

function required() {
    $(".delete").click(function(ev){
	return confirm('Please click OK to confirm deleting the data.');
    });
    $('.required').each(function(){
        $(this).prev('label').append('<span class="required">*</span>');
    })
}

function tabable(){
    $(".tabable:gt(0)").hide();
    $(".tab li:eq(0)").addClass('selected');
    $(".tab a").click(function(ev){
	ev.preventDefault();
	$(ev.currentTarget.hash).show().siblings(".tabable").hide();
	$(this).parents('li').addClass('selected').siblings().removeClass('selected');
    });
}

function beautifier(){
    // bind click action to radio and chkbox
    $("input:radio, input:checkbox").each(function(){
        var target = $(this);
    	if(target.is(":checked"))
    	    target.css('display', 'none').parent('label').addClass('button inp-chkbox active');
    	else
    	    target.css('display', 'none').parent('label').addClass('button inp-chkbox');
    });
    $("input:radio").change(function(){
	var target = $(this);
	if(target.is(":checked"))
	    target.parent('label').addClass('active').siblings().removeClass('active');
	else
	    target.parent('label').removeClass('active');
    });
    $("input:checkbox").live('change', function(ev){ changeCheckboxState(ev) });
    // apply style to first and last radio & chkbox
    $("input:radio, input:checkbox").parent("label:first-child").each(function(){
	$(this).addClass('first');
    });
    $("input:radio, input:checkbox").parent("label:last-child").each(function(){
	$(this).addClass('last');
    });   
    
    $("input:file").change(function(ev){
	if (window.File && window.FileReader && window.FileList && window.Blob) {
	    files = ev.target.files;
	    var target=ev.target;
	    for (var i = 0, f; f = files[i]; i++) {
		var reader = new FileReader();
		reader.onload = (function(theFile) {
		    return function(e) {
			$(target).parents('.file_container').siblings('.file_size').remove();
			$(target).siblings('.file_label').text(theFile.name);
			$("<span></span>", {"class": "file_size"} )
			    .text([Math.round((theFile.fileSize/1024), 2), 'KB'].join(" "))
			    .insertAfter($(target).parents('.file_container'));
		    };
		})(f);
		reader.readAsDataURL(f);
	    }
	}
	else{
	    var label=$(this).val()
	    $(this).siblings('.file_label').text(label);
	}
    }).wrap(function(idx){
	return '<span class="file_container field"></span>';
    }).before('<label for="'+$(this).attr('id')+'"class="file_label"></label>');
}

function changeCheckboxState(ev){
    var target = $(ev.target);
    if(target.is(":checked"))
	target.parent('label').addClass('active');
    else
	target.parent('label').removeClass('active');
}

function lightbox(){
    $("a.lightbox").colorbox({});
    $("a.lightbox2").colorbox({maxWidth:"970px"});
}

function modernizr(){
    // Test for input placeholder attribute, apply the script if the browser does not support
    if(!Modernizr.input.placeholder){
	$('[placeholder]').focus(function() {
	  var input = $(this);
	  if (input.val() == input.attr('placeholder')) {
		input.val('');
		input.removeClass('placeholder');
	  }
	}).blur(function() {
	  var input = $(this);
	  if (input.val() == '' || input.val() == input.attr('placeholder')) {
		input.addClass('placeholder');
		input.val(input.attr('placeholder'));
	  }
	}).blur();
	$('[placeholder]').parents('form').submit(function() {
	  $(this).find('[placeholder]').each(function() {
		var input = $(this);
		if (input.val() == input.attr('placeholder')) {
		  input.val('');
		}
	  })
	});
    }
    if(!Modernizr.input.required){
	$("[required]").addClass('required');
    }
}
