var site={
	loadCommon:function(){
        $('div').pngFix();
        $('.webform').validate({
    		rules: {
    			// the errorPlacement has to take the table layout into account
    			errorPlacement: function(error, element) {
    				if(element.is(":radio"))
    					error.appendTo(element.parent().next().next());
    				else if(element.is(":checkbox"))
    					error.appendTo (element.next());
    				else
    					error.appendTo(element.parent().next());
    			},
    			// set this class to error-labels to indicate valid fields
    			success: function(label) {
    				// set &nbsp; as text for IE
    				label.html("&nbsp;").addClass("checked");
    			}
    		}
    	});
    	$('ul.sf-menu').superfish();
        $('#dialog-login').dialog({
			autoOpen: false,
			height: 250,
			width: 350,
			modal: true,
			buttons: {
				'Login': function() {
					var userid = $('#dialog-login #userid').val();
                    var password = $('#dialog-login #password').val();
                    $.post('/member_area/login/ajax', {userid: userid, password: password},
                    function(data)
                    {
                        if(data != 'ok'){ 
                            $('#dialog-login-message').html(data); 
                        }else{ 
                            $('#dialog-login #userid').val('');
                            $('#dialog-login #password').val('');
                            $(this).dialog('close')
                            document.location.href = '/member_area/index'; 
                        }
                    });
				},
				Cancel: function(){
					$('#dialog-login #userid').val('');
                    $('#dialog-login #password').val('');
                    $(this).dialog('close');
				}
			},
			close: function(){
				$('#dialog-login #userid').val('');
                $('#dialog-login #password').val('');    
			}
		});
        $('.dialog-login').click(function(){
            $('#dialog-login').dialog('open');
		});
        $('#dialog-forgot').dialog({
			autoOpen: false,
			height: 270,
			width: 350,
			modal: true,
			buttons: {
				'Retrieve Password': function() {
					var email = $('#dialog-forgot #email').val();
                    $.post('/member_area/forgot/ajax', {email: email},
                        function(data)
                        {
                            $('#dialog-forgot-message').html(data); 
                            $('#dialog-forgot #email').val('');
                        }
                    );
				},
				Cancel: function(){
					$('#dialog-forgot #email').val('');
                    $(this).dialog('close');
				}
			},
			close: function(){
				$('#dialog-forgot #email').val('');
			}
		});
        $('.dialog-forgot').click(function(){
            $('#dialog-forgot').dialog('open');
		});
        $('#dialog-oc').dialog({
			autoOpen: false,
			height: 350,
			width: 600,
			modal: true
		});
    },
    slideSwitch:function(){
		var $active = $('.slideshow span.active');
    	if ( $active.length == 0 ) $active = $('#slideshow span:last');
    	var $next =  $active.next().length ? $active.next() : $('.slideshow span:first');
    	$active.addClass('last-active');
    	$next.css({opacity: 0.0})
    		.addClass('active')
    		.animate({opacity: 1.0}, 1000, function() {
    			$active.removeClass('active last-active');
    	});
	},
    externalWarning:function(){
        // Creating custom :external selector
        $.expr[':'].external = function(obj){
          return !obj.href.match(/^mailto\:/)
                  && (obj.hostname != location.hostname);
        };
        
        // Add 'external' CSS class to all external links
        $('a:external').addClass('external');
        
        $('.external').click(function(){
            var link = $(this).attr('href');
            
            if(link == 'javascript:void(null)')
                return true;
        
            $('<div>You are currently leaving our site, and headed towards: <br /><b> '+(link)+' </b><br /> are you sure you want to proceed?</div>').dialog({
              title: "External Link",
              modal : true,
              overlay: {
                backgroundColor: '#000',
                opacity: 0.5
              },
              buttons: {
                'Okay': function() {
                  $(this).dialog('close').remove();
                  window.open(link);
                },
                'Cancel': function() {
                  $(this).dialog('close').remove();
                  return false;
                }
              }
            });
            
            return false;
            }
        );
    },
    displayTable:function(){
        // format datagrid
    	var oTable;
    	oTable = $('.display').dataTable({
    		"sPaginationType": "full_numbers", 
    		"iDisplayLength": 100,
    		"fnDrawCallback": function(){
    			$('table.display tbody tr td.edit').css({'font-weight' : 'bold'});
    			$('table.display tbody tr td.edit').click(function(){ 
    				document.location.href= $(this).attr('url');
    			});
                $('table.display tbody tr td.ajax').unbind();
                $('table.display tbody tr td.ajax').click(function(){ 
    				$(this).find('img').attr('src','/img/ajax-loader-small.gif');
    				var val = $(this).attr('rel');
    				//var val = getVal.split('_'); // name | id
    				$.get('/admin/ajax/toggleOption/'+val, {},
    				function(data){ 
    					$('data',data).each( function(i){
    						var field = $(this).find('field').text();
    						var id = $(this).find('id').text();
    						var image = $(this).find('image').text();
                            var row = $(this).find('row').text();
    						$('table.display tbody tr td.ajax[rel="'+row+'"]').find('img').attr('src',image);
    					});
    				});
    			});
    		}
    	});
    },
    view_oc:function(id){
        $('#dialog-oc').dialog('open');
        $('.dialog-oc-data').html('<div style="text-align:center"><img src="/img/ajax-loader.gif" /></div>');
        $.ajax({
            type: 'GET',
            dataType : 'xml', 
            url: '/ajax/content/' + id,
            success: function(xml){
               $(xml).find('content').each(function(){
                    $('.dialog-oc-data').html($(this).find('data').text());
               });
            }
        });
	}
};
