
$(document).ready(function() {

	
	
	
	$('.jwplayer_nocontrol').each(function(){
		
		var element = $(this).children('img');
		var height = parseInt($(element).attr('height'));
		var width = $(element).attr('width');
		var image = $(element).attr('src');
		var file = $(element).attr('rel');
		var id = $(this).attr('id');
		
		
		jwplayer(id).setup({
			'flashplayer': '/scripts/jwplayer/player.swf',
			'file': file,
			'controlbar': 'none',
			'width': '100%',
			'height': '100%',
			'image': image,
		    'backcolor': 'FFFFFF',
		    'frontcolor': 'FFFFFF',
		    'lightcolor': 'FFFFFF',
		    'screencolor': 'FFFFFF',
			'wmode' : 'opaque'
		});	
		
		$(this).append('<div style="clear:both;"> </div>');
	});	

	$('.jwplayer').each(function(){
		
		var element = $(this).children('img');
		var height = parseInt($(element).attr('height'));
		var width = $(element).attr('width');
		var image = $(element).attr('src');
		var file = $(element).attr('rel');
		var id = $(this).attr('id');
		
		height += 23; // cope with toolbar
		
		jwplayer(id).setup({
			'flashplayer': '/scripts/jwplayer/player.swf',
			'file': file,
			'controlbar': 'bottom',
			'width': width,
			'height': height,
			'image': image
		});	
	});
	

    
    fname = "";
    fpath = "";
    var errors = "";
    var temp_folder = Math.round(new Date().getTime() / 1000) + '-' + Math.floor(Math.random()*1100000); 
	$('#top-menu ul li li a.selected').parent().parent().parent().css('background','url(/images/top-menu-selected.png) no-repeat scroll 20px top transparent');
	$('#top-menu ul li a.selected').parent().css('background','url(/images/top-menu-selected.png) no-repeat scroll 20px top transparent');

    // setup first news tab
	$('.tab-news-item:first').show();
	$('.tab-news-item:first').show();
	$('.tab:first').addClass('selected');

	//setup controls for tab buttons
	$('.tab a').click(function(e){
		e.preventDefault();
		$('.tab').removeClass('selected');
		$(this).parent().parent().addClass('selected');
		
		
		var tab_id = $(this).attr('href');
		$('.tab-news-item').not(tab_id).slideUp('slow',function(){
			
			//$('.tabs').css('height',);
			$(tab_id).slideDown('slow',function(){
				
			});
		});
		
	});
    

	
	
	$('.feature_item_yes').click(function(){
		$('.feature_item_yes').css('background-image','url(/images/tick_Q.png)');  
		$('.feature_item_desc').slideUp('fast');
		$(this).next('.feature_item_desc').slideDown('fast');
		 $(this).css('background-image','url(/images/tick_green.png)');
	});
	
	$('#subscribe_submit').click(function(){
		 $.post("/ajax/qmailer_subscribe.php", $("#subscribe_multi_form").serialize(),function(data){
			 if(data.result=='SUCCESS'){
				 $('#below_fold').html('<h2>Thank you</h2><p>You are now subscribed.</p>');
			 }else{
				 alert("Errors:\n" + data.errors);
			 }
		 },'json');
	});
    
    //jgueryui error box setup
    
    $( "#errors" ).dialog({
            autoOpen: false,
            show: {effect: "fade", duration: 300},
            hide: {effect: "explode", duration: 250},
            width: 700,
            height: 210,
            resizable: false
            
        });
    

    
    $('#quote_site_examples').click(function() {
            if ($(this).val() == "(enter the URLs here)") {
            $(this).val('');
            }
    });
    
    
    $('#quote_extra_info ').click(function() {
            if ($(this).val() == "(confidentiality or transparancey issues, for example)") {
            $(this).val('');
            }
    });    
    
    
    $('#quote_submit').click(function(e){
        errors = '';
        //Validate quote form
        if (!$('#quote_first_name').val()) {
        errors += "Please enter your <span class=\"quote_error_field_text\">first name</span><br />";
        $('#l_first_name').addClass("form_label_error");
        } else {
         $('#l_first_name').removeClass("form_label_error");     
        }
        if (!$('#quote_last_name').val()) {
        errors += "Please enter your <span class=\"quote_error_field_text\">surname</span><br />";
        $('#l_last_name').addClass("form_label_error");
        } else {
         $('#l_last_name').removeClass("form_label_error");   
        }
        if (!$('#quote_email_address').val()) {
        errors += "Please enter your <span class=\"quote_error_field_text\">contact email address</span><br />";
        $('#l_email').addClass("form_label_error");
        } else {
         $('#l_email').removeClass("form_label_error");   
        }
         if (!$('#quote_phone_number').val()) {
        errors += "Please enter your <span class=\"quote_error_field_text\">telephone number</span><br />";
        $('#l_phone').addClass("form_label_error");
        } else {
         $('#l_phone').removeClass("form_label_error");     
        }
        if (!$('#quote_site_description').val()) {
        errors += "Please enter a <span class=\"quote_error_field_text\">description of how you would like your site to work</span><br />";
         $('#l_site').addClass("form_label_error");
        } else {
         $('#l_site').removeClass("form_label_error");      
        }
        if (!$('#quote_design_required').val()) {
        errors += "Please select if you <span class=\"quote_error_field_text\">already have a design or layout for your site</span><br />";
        $('#l_design').addClass("form_label_error");
        } else {
         $('#l_design').removeClass("form_label_error");   
        }
        if (!$('#quote_hosting_required').val()) {
        errors += "Please select if you <span class=\"quote_error_field_text\">already have hosting for your site</span><br />";
        $('#l_hosting').addClass("form_label_error");
        } else {
         $('#l_hosting').removeClass("form_label_error");     
        }
        if (!$('#quote_seo_required').val()) {
        errors += "Please select if you require <span class=\"quote_error_field_text\">Search Engine Optimisation services for your site</span><br />";
        $('#l_seo').addClass("form_label_error");
        } else {
         $('#l_seo').removeClass("form_label_error");      
        }
        if (!errors) {
            //post form via ajax
             $.post("/ajax/quote_submit.php", $('#quote_form').serialize(),function(data){
             if(data.result=='SUCCESS'){
             $('#quote_form').html(data.message);
             }else{
                 $('#errors p').html('Quote form post failed\n' + data.message);
             }
         },'json');
        } else {
            $('#errors p').html(errors);
            $( "#errors" ).dialog( "open" );
            //alert(errors);
        }
      
    });

  
	
});






function confirm_delete(del_url,message) {
    if(!message){
		message = 'Are you sure?';
    }
  	if (confirm(message)) {
    document.location = del_url;
  }
}


function replaceURLWithHTMLLinks(text) {
	var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
	return text.replace(exp,"<a href='$1' target='_blank'>$1</a>"); 
}

jQuery.fn.fitToParent = function()
{
    this.each(function()
    {
        var width  = $(this).width();
        var height = $(this).height();
        var parentWidth  = $(this).parent().width();
        var parentHeight = $(this).parent().height();

        if(width/parentWidth < height/parentHeight)
        {
                newWidth  = parentWidth;
                newHeight = newWidth/width*height;
        }
        else
        {
                newHeight = parentHeight;
                newWidth  = newHeight/height*width;
        }
        margin_top  = (parentHeight - newHeight) / 2;
        margin_left = (parentWidth  - newWidth ) / 2;

        $(this).css({'margin-top' :margin_top  + 'px',
                     'margin-left':margin_left + 'px',
                     'height'     :newHeight   + 'px',
                     'width'      :newWidth    + 'px'});
    });
};
