// jQuery.noConflict();

$(document).ready(function(){
	//------------------------------------------------------------------
	//*** replace all ck inserted movies with swfobject ***
	
	//needed for accessibility options
	var cnt = 1;
	var next_img = null;
	//find all embed objects
	$('div.flash-movie').each(function(){
									   
		flash_preview_img = '/content/images/test.gif';
		var next_img = $(this).siblings('img');
		var flash_preview_img = null;
		var flash_div = $(this).children("a");
		var flash_href = flash_div.attr('href');
		var flash_extension = flash_div.attr('href').substring(flash_div.attr('href').lastIndexOf(".")+1,flash_div.attr('href').length);
		var flash_preview_img = '/content/images/test.gif';
		
		/**/
		switch(flash_extension){
			case "flv":
				if(next_img.length > 0 && next_img.attr('src').length > 0){
					flash_preview_img = next_img.attr('src');
					//remove the image from the page
					next_img.remove();
				}
				flash_href = flash_href + '&image=' + flash_preview_img;
				flash_div.attr('href',flash_href);
			break;
		}
		
		flash_div.media();				
		//if there is an img next to the flash video then use that image as the image preview
		
		if(next_img.length > 0 && next_img.attr('src').length > 0){
			flash_preview_img = next_img.attr('src');
			//remove the image from the page
			next_img.remove();
		}
		
							
		//set default player location
		//$.fn.media.defaults.flvPlayer = '/css/skins/default/flash/player.swf';
		//run media plugin on all a links with media class
		//$('#'+flash_div+' a').media();
	});
	
	$('embed').each(function(){
							
			//if they are flash movies
			if($(this).attr('type') == 'application/x-shockwave-flash'){
				
				//get the source fo the flash movie
				if($(this).attr('src')){
					
					flash_src = $(this).attr('src');
					flash_div = 'flash-'+cnt;
					flash_extension = $(this).attr('src').substring($(this).attr('src').lastIndexOf(".")+1,$(this).attr('src').length);
					flash_preview_img = '/content/images/test.gif';
					
					//give the element and id
					$(this).attr('id','embed-'+cnt);
					
					switch(flash_extension){	
						default:
						if(jQuery.fn.flash){
							//replace the embed obect with a holding div
							$(this).replaceWith('<div id="'+flash_div+'"></div>');
							//use swfobject to embded the flash movie
							$('#'+flash_div).flash({ 
								// test.swf is the flash document 
								swf: flash_src,
								width : 425,
								height : 344
							}); 
						}else{
							alert('You need to have the swfobject plugins installed.');
						}
						break;
					}
					
				}
				
			}
		cnt ++;
	});

	//*** replace all ck inserted movies with swfobject ***
	//------------------------------------------------------------------
	

	//------------------------------------------------------------------
	//--- user expiry message ---
	if ($('#expiry-outer-wrapper').length > 0){ // check if element is on page
		//scroll the message box to the top offset of browser's scrool bar
		$(window).scroll(function()
		{
			$('#expiry-outer-wrapper #expiry').animate({top:$(window).scrollTop()+"px" },{queue: false, duration: 350});  
		});
		//when the close button at right corner of the message box is clicked 
		$('#close-message-expiry').click(function()
		{
			//the messagebox gets scrool down with top property and gets hidden with zero opacity 
			$('#expiry-outer-wrapper #expiry').animate({ top:"+=15px",opacity:0 }, "slow");
		});
	}// check if element is on page
	//--- user expiry message ---
	//------------------------------------------------------------------
	

	//------------------------------------------------------------------
	//--- jquery nyroModal ---/
	$(function() {
		if ($('.nyroModal').length > 0 ){ // check if element is on page	
			$.nyroModalSettings({
				bgColor: '#000000',
				width: 670,
				//height: 370,
				height: null,
				minWidth: 500, // Minimum width
				//minheight: 294, // Minimum width
				resizeable: true, // Indicate if the content is resizable. Will be set to false for swf
				autoSizable: true, // Indicate if the content is auto sizable. If not, the min size will be used
				title: null // Modal title
			});
		 }//check
	});

	if ($('.nyroModal').length > 0 ){ // check if element is on page	
		$('.nyroModal').nyroModal({
			width: 800,
			height: 400
		});	
	}//check

	//nyromodal used with ajax paging links
	if ($('.nyroModal').length > 0 ){ // check if element is on page	
		$(".nyroModal").livequery('click',function(e) {
			e.preventDefault();
			$(this).nyroModalManual({
				width: 800,
				height: 400
			});
			return false;
		});
	}//check		
	//--- jquery nyroModal ---/
	//------------------------------------------------------------------
	

	//replace all fck inserted movies with swfobject
	//needed for accessibility options
	var cnt = 1;
	//find all embed objects
	$('embed').each(function(){
			//if they are flash movies
			if($(this).attr('type') == 'application/x-shockwave-flash'){
				
				//get the source fo the flash movie
				if($(this).attr('src')){
					
					flash_src = $(this).attr('src');
					flash_div = 'flash-'+cnt;
					flash_extension = $(this).attr('src').substring($(this).attr('src').lastIndexOf(".")+1,$(this).attr('src').length);
					
					switch(flash_extension){
						case "flv":
						if(jQuery.fn.media){
							//replace the embed obect with a holding div
							$(this).replaceWith('<div id="'+flash_div+'"><a class="{width:425, height:344}" href="'+flash_src+'"></a></div>');
						
							//set default player location
							$.fn.media.defaults.flvPlayer = '/css/skins/default/flash/mediaplayer.swf';
							//run media plugin on all a links with media class
							$('#'+flash_div+' a').media();
						}else{
							alert('You need to have the media and meta plugins installed.');
						}
						break;
						default:
						if(jQuery.fn.flash){
							//replace the embed obect with a holding div
							$(this).replaceWith('<div id="'+flash_div+'"></div>');
							//use swfobject to embded the flash movie
							$('#'+flash_div).flash({ 
								// test.swf is the flash document 
								swf: flash_src,
								width : 425,
								height : 344
							}); 
						}else{
							alert('You need to have the swfobject plugins installed.');
						}
						break;
					}
				}
			}
		cnt ++;
	});
	
	
	//apply form validation
	if(jQuery.fn.validate){			
		if($('#form-login-pop').length > 0 ){ // check if element is on page
			$("#form-login-pop").validate({
				highlight: function(element, errorClass) {
					$(element).animate({
						opacity: 0.9
					}, 400, function () {
						$(element).css('color','#000000'); //red background for invalid data
						$(element).css('background-color','#FBE3E4'); //red background for invalid data
						$(element).css('border-color','#FBC2C4'); //red border for invalid data
					});
				},
				unhighlight: function(element, errorClass) {
					$(element).animate({
						opacity: 1.0
					}, 400, function () {
						$(element).css('color','#000000'); //red background for invalid data
						$(element).css('background-color','#E6EFC2'); //green background for valid data
						$(element).css('border-color','#C6D880'); //green border for valid data
					});
				},
				rules: {
					txt_login_email: "required",
					txt_login_password: "required"
				},
				messages: {
					txt_login_email: "Please enter your email.",
					txt_login_password: "Please enter your password."
				}
			});
		}
	}

	/*
	//function formValidate() { 
	if ($('#form-login-pop').length > 0 ){ // check if element is on page
		$("#form-login-pop").validate({ 
			rules: {
				txt_login_email: "required",
				txt_login_password: "required"
			},
			messages: {
				txt_login_email: "Please enter your email.",
				txt_login_password: "Please enter your password."
			}
		});
	}
	//});	
	*/

	$("#form-login-pop").submit(function() 
	{ 
		// we want to store the values from the form input box, then send via ajax below	
		var txt_login_email     = $('#txt_login_email').attr('value');
		var txt_login_password     = $('#txt_login_password').attr('value');
		//var chk_user_remember     = $('#chk_user_remember').attr('value');
		var chk_user_remember = "";
		//returns true or false.
		if ( $('#chk_user_remember').attr('checked') == true ){
			chk_user_remember = 1;
		}
		else
		{
			//alert ("false");
		}	
		var action     = $('#action').attr('value');
		var hid_Form     = $('#hid_Form').attr('value');
		var hid_Website     = $('#hid_Website').attr('value');
		var txt_send     = 1;
		//document.getElementById("username").value 
		//alert ("chk_user_remember:"+chk_user_remember);
		//alert ( $('#chk_user_remember').attr('checked') ); //returns true or false.
 
		if ($("#form-login-pop").valid()) {
			/*$.ajax({
				type: "POST",
				url: "/clogin.php", // /clogin.php init
				data: "txt_login_email="+ txt_login_email +"& txt_login_password="+ txt_login_password +"& action="+ action
                +"& hid_Form="+ hid_Form +"& hid_Website="+ hid_Website +"& txt_send="+ txt_send,	
				success: function(html){
					//$('#div-form-register').hide();
					//$('#results').fadeIn();
					$("#results").html(html);
					//$('#div-form-login-pop').hide();
					//$("#results").html('grove');
					//alert ("submit");
				}
			});*/
				
			$.ajax({
				url: "/clogin.php", // /clogin.php init	
				type: "POST",		
				data: "txt_login_email="+ txt_login_email +"& txt_login_password="+ txt_login_password 
				+"& chk_user_remember="+ chk_user_remember +"& action="+ action
                +"& hid_Form="+ hid_Form +"& hid_Website="+ hid_Website +"& txt_send="+ txt_send,		
				cache: false,
				success: function (xml) //html obj,target,msg xml
				{		
					//XML RESPONSE
					$(xml).find('ajaxresults').each(function(){					 
						//var action = $(this).attr('action');
						var success_login = $(this).find('success_login').text();
						var html_data = $(this).find('html_data').text();		
						//alert('response:'+response);
						//alert('html_data:'+html_data);
						//add the content retrieved from ajax and put it in the #content div
	
						//add the content retrieved from ajax and put it in the #content div
						$("#results").html(html_data);	//fade in div
						//$('#paging').fadeIn('slow');
						
						if (success_login == 1){
							$('#div-form-login-pop').hide();
						}
					});				
				
					//HTML RESPONSE
					//add the content retrieved from ajax and put it in the #content div
					//$('#paging').html(html);					
					//display the body with fadeIn transition
					//$('#paging').fadeIn('slow');	
				}		
			});
		}
		
		return false;
	});
	
	
	
	//if the livequery plugin is detected
	if(jQuery.fn.ajaxify){
		if($('#content-loading').length > 0 ){ // check if element is on page
			//alert("grove");
			$('#main #main-center-panel div.col-1').css({'position':'relative'});
			$('#main #main-center-panel #load-content').css({'position':'relative'});
			$('#content-loading').css({
			  'clear':'both',
			  'float':'left',
			  'width':'100%',
			  'text-align':'left',
			  //'position':'absolute',
			  'top':'0',
			  'left':'0',
			  'font-size':'1.3em'
			  });
			
			$('a.ajax').livequery(function(){
		  
				$(this).ajaxify({
					onStart:function(options){
						$('#content-loading').fadeIn('slow');
					},
					onError:function(options,data){			
						$('#content-loading').html("<p class='error'>Error: Couldn't open &quot;"+options.link+"&quot;.</p>");	
					},
					onSuccess:function(options,data){
						$('#content-loading').css('display','none');
					},
					onComplete:function(options){
						$('#content-loading').css('display','none');
					},
					target: '#load-content',
					loadHash:'attr:href',
					loading_txt:'Page loading...',
					loading_img:'/img/site/jquery/ajax-loader-2.gif',
					loading_target:'#content-loading'			
				});
			
			});	
		}
	}
	
	/* EMBED MEDIA */
	if(jQuery.fn.media){
		//set default player location
		$.fn.media.defaults.flvPlayer = '/css/skins/default/flash/mediaplayer.swf';
		//run media plugin on all a links with media class
		$('a.media').media();
	}
	
	
	if(jQuery.fn.showhide){
		//apply show hide plugin for use in gallery
		if($('a.showhide').length > 0 ){
			$('a.showhide').showhide({
				animate:true,
				event:'click',
				target:'#large_img',
				closer_img:'/img/site/buttons/btn-closer.gif',
				show_closer:true
			});
		}
	}
	
	
	//-------------------------------------------------------------------------------------------------
	//--- CONTACT FORM ---
	if ($('#form-contact').length > 0){ // check if element is on page
		$("#form-contact").livequery(function(){
			$(this).validate({
				rules: {
					txt_firstname: "required",
					txt_surname: "required",
					txa_address: "required",
					txt_tel: "required",
					txt_email: {
						required: true,
						email: true
					},
					txa_message: "required"
				},
				messages: {
					txt_firstname: "Please enter your firstname.",
					txt_surname: "Please enter your surname.",
					txa_address: "Please enter your address.",
					txt_tel: "Please enter your telephone number.",
					txt_email: "Please enter a valid email address.",
					txa_message: "Please enter your message."
				}
			});
		});	
	}// check if element is on page
	//--- CONTACT FORM ---	
	//-------------------------------------------------------------------------------------------------



	//-------------------------------------------------------------------------------------------------
	//--- CONTACT SUBS FORM - MANUALLY REQUEST SUBSCRIPTION ---
	if ($('#form-contact-subs').length > 0){ // check if element is on page
		$("#form-contact-subs").livequery(function(){
			$(this).validate({
				rules: {
					txt_firstname: "required",
					txt_surname: "required",
					txa_address: "required",
					txt_tel: "required",
					txt_email: {
						required: true,
						email: true
					},
					txa_message: "required"
				},
				messages: {
					txt_firstname: "Please enter your firstname.",
					txt_surname: "Please enter your surname.",
					txa_address: "Please enter your address.",
					txt_tel: "Please enter your telephone number.",
					txt_email: "Please enter a valid email address.",
					txa_message: "Please enter your message."
				}
			});
		});	
	}// check if element is on page
	//--- CONTACT SUBS FORM - MANUALLY REQUEST SUBSCRIPTION ---	
	//-------------------------------------------------------------------------------------------------



	//-------------------------------------------------------------------------------------------------
	//--- SUBSCRIPTION USERS ---	
	//user register form
	if ($('#form-register').length > 0){ // check if element is on page
		$("#form-register").livequery(function(){
			$(this).validate({
				rules: {
					txt_title: "required",
					txt_firstname: "required",
					txt_surname: "required",
					txt_address: "required",
					txt_postcode: "required",
					txt_phone: "required",
					txt_reg_email: {
						required: true,
						email: true
					},
					txt_password: "required",
					txt_repeat_password: {
						required: true,
						equalTo: "#txt_password"
					}
				},
				messages: {
					txt_title: "Please enter your title.",
					txt_firstname: "Please enter your first name.",
					txt_surname: "Please enter your surname.",
					txt_address: "Please enter your address.",
					txt_postcode: "Please enter your postcode.",
					txt_phone: "Please enter your telephone number.",
					txt_reg_email: "Please enter a valid email address.",
					txt_password: "Please enter a password.",			
					txt_repeat_password: { 
						required: "Please confirm password.", 
						equalTo: "Please enter the same password as above." 
					} 				
				}
			});	
		});
	}// check if element is on page


	//user edit form
	if ($('#form-useredit').length > 0){ // check if element is on page
		$("#form-useredit").livequery(function(){
			$(this).validate({
				rules: {
					txt_title: "required",
					txt_firstname: "required",
					txt_surname: "required",
					txt_address: "required",
					txt_postcode: "required",
					txt_phone: "required",
					txt_reg_email: {
						required: true,
						email: true
					},
					//txt_password: "required",
					txt_repeat_password: {
						//required: true,
						equalTo: "#txt_password"
					}
				},
				messages: {
					txt_title: "Please enter your title.",
					txt_firstname: "Please enter your first name.",
					txt_surname: "Please enter your surname.",
					txt_address: "Please enter your address.",
					txt_postcode: "Please enter your postcode.",
					txt_phone: "Please enter your telephone number.",
					txt_reg_email: "Please enter a valid email address.",
					//txt_password: "Please enter a password.",			
					txt_repeat_password: { 
						//required: "Please confirm password.", 
						equalTo: "Please enter the same password as above." 
					} 				
				}
			});	
		});
	}// check if element is on page
	
	
	//user reminder form
	if ($('#form-reminder').length > 0){ // check if element is on page
		$("#form-reminder").livequery(function(){
			$(this).validate({
				rules: {
					f_email: {
						required: true,
						email: true
					}
				},
				messages: {
					f_email: "Please enter a valid email address."				
				}
			});	
		});
	}// check if element is on page
	//--- SUBSCRIPTION USERS ---
	//-------------------------------------------------------------------------------------------------


	
	//-------------------------------------------------------------------------------------------------
	//--- NEWSLETTERS ---
	//user register form
	if ($('#form-newsletter-signup').length > 0){ // check if element is on page
		$("#form-newsletter-signup").livequery(function(){
			$(this).validate({
				rules: {
					txt_firstname: "required",
					txt_surname: "required",
					txt_reg_email: {
						required: true,
						email: true
					}
				},
				messages: {
					txt_firstname: "Please enter your first name.",
					txt_surname: "Please enter your surname.",
					txt_reg_email: "Please enter a valid email address."			
				}
			});	
		});
	}// check if element is on page
	//--- NEWSLETTERS ---
	//-------------------------------------------------------------------------------------------------

	
	
	/*$(document).pngFix();*/
	
	//*** BANNERS ***

    /*$('.slideshow-tb1').cycle({
		fx: 'fade' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
	});*/
	
    $('.slideshow-lb2').cycle({
		fx: 'fade' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
	});

	/*$('.slideshow-rb1').cycle({
		fx: 'fade' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
	});*/
	
    $('.slideshow-rb2').cycle({
		fx: 'scrollDown', // choose your transition type, ex: fade, scrollUp, shuffle, scrollDown etc...
    	delay:  -2000 
	});	

}); //jquery load


Cufon.replace('#header #top-heading div.col-2 p em', { fontFamily: 'AvantGarde-MediumObl', hover: true });
Cufon.replace('#banner-wrapper div.col-1 p', { fontFamily: 'AvantGarde-Medium', hover: true });
Cufon.replace('#main #main-left-panel .side-panel .item h3', { fontFamily: 'AvantGarde-Medium', hover: true });
Cufon.replace('#main #main-center-panel div.col-1 h3', { fontFamily: 'AvantGarde-Medium', hover: true });
Cufon.replace('#main #main-center-panel ul.listing li h3', { fontFamily: 'AvantGarde-Demi', hover: true });
Cufon.replace('h4', { fontFamily: 'AvantGarde-Medium', hover: true });
Cufon.replace('#main #main-left-panel .side-panel .item p a', { fontFamily: 'AvantGarde-Medium', hover: true });
Cufon.replace('#news #main #main-center-panel div.col-1 ul.listing li h3', { fontFamily: 'AvantGarde-Medium', hover: true });


//run cufon font replacement calls
jQuery.each(jQuery.browser, function(i) {
	//non IE only css amend using jquery
	if($.browser.msie == false || ($.browser.msie == true && $.browser.version == 8)){		
		Cufon.replace('h2', { fontFamily: 'AvantGarde-Medium', hover: true });
	}else{		
		Cufon.replace('#main-outer-wrapper h2', { fontFamily: 'AvantGarde-Medium', hover: true });
	}
});


