/* vim: set expandtab tabstop=4 shiftwidth=4: */

var browserVersion = parseInt(navigator.appVersion);
var isNetscape = navigator.appName.indexOf("Netscape") != -1;
var isIE = navigator.appName.indexOf("Microsoft") != -1;
var agent = navigator.userAgent.toLowerCase();
var isWindows = agent.indexOf("win") != -1;
var isMac = agent.indexOf("mac") != -1;
var isNix = agent.indexOf("X11") != -1;

function doConfirm(text)
{
    var val = confirm(text);
    return (!val) ? false : true;
}

function selectHomepageItem(buttonId) {
	var id = buttonId.replace("button-", "");
	var itemId = "item-"+id;
	$(".homepage-slideshow .selected").removeClass("selected");
	$(".homepage-slideshow #"+buttonId).addClass("selected");
	$(".homepage-slideshow #"+itemId).addClass("selected");
}

//JQUERY
$(document).ready( function() {
	// Add first and last classes to main menu
	$('.main-menu ul.level1 li:first').addClass('firstItem');
	$('.main-menu ul.level1 li:last').addClass('lastItem');
		
	var docHeight = $(document).height();
	var bdHeight = docHeight - 420;
	
	$("#bd").css('min-height', bdHeight+'px');
	
	// Header Newsletter Signup
	var registrationForm = $('#hd .newsletter-registration-form');
	registrationForm.wrap('<div class="newsletter-registration" />');
	
    registrationForm.find('form').attr('action', '/newsletter-registration/');
	registrationForm.find('.messages').hide();
	registrationForm.find('input').each(function() {
		$(this).addClass($(this).attr('type'));
		
		if ($(this).attr('type') == 'submit') {
		  $(this).parent().siblings('#' + $(this).attr('id') + '-label').remove();
		  
		  $(this).after('<img src="/images/layout/newsletter-signup/submit-button.png" class="submit-image" />');
		  registrationForm.find('img.submit-image').click(function() {
		      registrationForm.find('input[type=submit]').click();	
		  });
											  
          $(this).hide();
		}
	});
	
	var newsletterRegistration = $('#hd .newsletter-registration');
	
	newsletterRegistration.prepend('<div class="newsletter-button">Newsletter Signup</div>');
	newsletterRegistration.children('.newsletter-button').click(function() {
		$(this).parent().toggleClass('clicked');
	});
	
	// Body Newsletter Signup
	var bodyNewsletterRegistration = $('#body div.newsletter-registration-form');
	
	bodyNewsletterRegistration.before(
	   bodyNewsletterRegistration.children('.messages')
	);
	
	
	//Header Search
	var defaultText = "Search...";
    $('#hd .header-search input#keywords').val(defaultText);
    $('#hd .header-search input#keywords').focus(function() {
        $(this).val("");
    });
    $('#hd .header-search input#keywords').blur(function() {
        if($(this).val()=="") {
            $(this).val(defaultText);
        }
    });
    
    //Sidebar     
    $("#sidebar").css('min-height', $("#bd").height()+'px')
    
    $(".x-marketing-button .content-item").each( function() { 
    	  var colour = $(this).children(".background").text();
    	  var arrow = ' <img src="/images/x-marketing/white-arrow.png" alt="'+$(this).children(".text").text()+'" />';
    	  $(this).children(".text").children("a").append(arrow);
    	  $(this).addClass(colour.toLowerCase());
    	  $(this).children(".background").remove();
    });

	
	//Homepage slideshow
	if ($(".homepage-slideshow").length) {
		selectHomepageItem($(".homepage-slideshow .buttons .slideshow-button:first").attr('id'));
		$(".homepage-slideshow .slideshow-button").click( function() {
			selectHomepageItem(this.id);
		});
	}
	
	// Homepage map
	if ($(".map-box").length) {
		var imgSrc = $(".map-box .snippet-map img").attr('src');
		$(".map-box").css('background', "url('"+imgSrc+"') no-repeat right 15px");
		$(".map-box .snippet-map img").remove();
	}
	
	// Testimonials
	if ($(".testimonial-scroller").length) {
		// Add nav arrows
		$('.testimonial-scroller div.lastItem').after('<a id="testimonial-prev"><img src="/images/icons/testimonial-prev.png" alt="previous" /></a>');
		$('.testimonial-scroller div.lastItem').after('<a id="testimonial-next"><img src="/images/icons/testimonial-next.png" alt="next" /></a>');
		
		$(".testimonial-scroller div.firstItem").addClass('selected');
		
		$('.testimonial-scroller a').click( function() {
			var dir;
			
			if ($(this).attr('id')=='testimonial-prev') {
				dir = 'prev';
			} else if ($(this).attr('id')=='testimonial-next') {
				dir = 'next';
			}
			if (dir=='next') {
				if ($('.testimonial-scroller div.selected').hasClass('lastItem')) {
					$('.testimonial-scroller div.selected').removeClass('selected');
					$('.testimonial-scroller div.firstItem').addClass('selected');
				} else {
					$('.testimonial-scroller div.selected').next().addClass('next');
					$('.testimonial-scroller div.selected').removeClass('selected');
					$('.testimonial-scroller div.next').addClass('selected');
					$('.testimonial-scroller div.next').removeClass('next');
				}
			} else if(dir=='prev') {
				if ($('.testimonial-scroller div.selected').hasClass('firstItem')) {
					$('.testimonial-scroller div.selected').removeClass('selected');
					$('.testimonial-scroller div.lastItem').addClass('selected');
				} else {
					$('.testimonial-scroller div.selected').prev().addClass('prev');
					$('.testimonial-scroller div.selected').removeClass('selected');
					$('.testimonial-scroller div.prev').addClass('selected');
					$('.testimonial-scroller div.prev').removeClass('prev');
				}
			}
			
		});
		
		
	}

	// Event Listings
    $(".standard-listings .eventItem").addClass("listingItem");
    
	$(".standard-listings .eventItem").each( function() { 
		var date = $(this).children(".date").html();
		$(this).children(".date").html("<strong>Date:</strong> "+date);
        //$(this).children('.read-more').after('<div class="clear"><!-- --></div>');
	});
    
    // Search Listings
    $(".standard-listings .result").addClass("listingItem");
    
    //Listing pages
    $(".listingItem").each( function() { 
        $(this).children('.read-more').after('<div class="clear"><!-- --></div>');
    });
	
	//Pagination
	if ($("div.pagination").length && !$("div.pagination a").length) {
		$("div.pagination").addClass("hidden");
	}
	
	// Contact forms
	if ($("div.onlineForm").length) {
		$("div.onlineForm input:text").addClass("text");
	}
    
    if ($("div.subfeature.form-right").length) {
        $("div.onlineForm").addClass("form-right");
    }
	
	// FAQ pages
	if ($("div.faq-item").length) {
		$("div.faq-item h2.question").click( function() {
			if ($(this).hasClass("selected")) {
				$("div.faq-item .selected").removeClass("selected");
			} else {
				$("div.faq-item .selected").removeClass("selected");
				$(this).addClass("selected");
				$(this).siblings("div.answer").addClass("selected");
			}
		});
	}
    
    // Event Details
    if ($("div.event div.time").length < 2) {
        $("div.event div.time").remove();
        $("div.event div.content").css('margin-top', '25px');
    }
    
    
    // Event Guest Details
    $('#eventGuestDetails input.notRequiredGuest').change(function() {
        if ($(this).attr('checked')) {
            $(this).parents('dl').nextAll('dl').toggle();
        } else {
            $(this).parents('dl').nextAll('dl').toggle();
        }
    });
    //facebook like
    
    var currentUrl = window.location.href;
   
    $(".news-feature").append('<div class="facebook-like"><fb:like href="'+encodeURI(currentUrl)+'" layout="standard" show_faces="false" width="100" font="arial"></fb:like></div>');
    
    $(".home .yui-gb").append('<div class="facebook-like"><fb:like href="'+encodeURI(currentUrl)+'" layout="standard" show_faces="false" width="75" font="arial"></fb:like></div>');
    // Event booking
    if (($("div.basket").length) &&(!$('div.basket').hasClass('eventBooking'))) {
        $("div.basket").addClass("eventBooking");
    }
    
    if ($("form#customerInfoForm").length) {
        $("form#customerInfoForm input:text").addClass('text');
        if ($("div.basketContent .totalPrice").text()!='£0') {
            var wpLogo = '<a href="http://www.rbsworldpay.com/" target="_blank" title="Payment Processing - RBS WorldPay - Opens in new browser window"><img src="http://www.rbsworldpay.com/images/poweredByRBSWorldPay.gif" border="0" alt="RBS WorldPay Payments Processing"></a>';
            $("form#customerInfoForm .label-submitCheckout").html(wpLogo);
            var mcLogo = '<img src=http://www.rbsworldpay.com/images/cardlogos/mastercard.gif border=0 alt="Mastercard payments supported by RBS WorldPay">';
            var visaLogo1 = '<img src=http://www.rbsworldpay.com/images/cardlogos/VISA.gif border=0 alt="Visa Credit payments supported by RBS WorldPay">';
            var visaLogo2 = '<img src=http://www.rbsworldpay.com/images/cardlogos/visa_debit.gif border=0 alt="Visa Debit payments supported by RBS WorldPay">';
            var visaLogo3 = '<img src=http://www.rbsworldpay.com/images/cardlogos/visa_electron.gif border=0 alt="Visa Electron payments supported by RBS WorldPay">';
            var amexLogo = '<img src=http://www.rbsworldpay.com/images/cardlogos/AMEX.gif border=0 alt="American Express payments supported by RBS WorldPay">';
            var maestroLogo = '<img src=http://www.rbsworldpay.com/images/cardlogos/maestro.gif border=0 alt="Maestro payments supported by RBS WorldPay">';
            var jcbLogo = '<img src=http://www.rbsworldpay.com/images/cardlogos/JCB.gif border=0 alt="JCB payments supported by RBS WorldPay">';
            var soloLogo = '<img src=http://www.rbsworldpay.com/images/cardlogos/SOLO.gif border=0 alt="Solo payments supported by RBS WorldPay">';
            var logos = '<div class="eventPaymentLogos">'+visaLogo1+visaLogo2+visaLogo3+mcLogo+amexLogo+maestroLogo+jcbLogo+soloLogo+'</div>';
            $("form#customerInfoForm").after(logos);
        }
    }
    
    // Lightbox
    if($('a.lightbox').html()){
        $('a.lightbox').lightBox();
        $('a.enlarge').lightBox();
    }
    
    //Advert form check 
    $("#advertisement-form dd#othersizepublication-element").prepend('<span> Advert Size (W X H):</span>');
      $("#advertisement-form").submit(function() {
        //if($('#sizeofpublication-OtherPleasespecifywidthheightandA4A5').attr('checked')){
            if($('input[name='+ "a4publication" +']:checked').length)
                {
                  if($('input[name='+ "a5publication" +']:checked').length)
                      {
                      alert('Please choose only one option for the size of publication.');
                          $('input[name='+ "a4publication" +']:checked').attr('checked', false);
                          $('input[name='+ "a5publication" +']:checked').attr('checked', false);
                          return false;
                      }
                }
            else if($('input[name='+ "a5publication" +']:checked').length){
                        if($('input[name='+ "a4publication" +']:checked').length)
                        {
                            alert('Please choose only one option for the size of publication.');
                            $('input[name='+ "a4publication" +']:checked').attr('checked', false);
                            $('input[name='+ "a5publication" +']:checked').attr('checked', false);
                            return false;
                        }
                    
                    }
                    
      
       if($('a4publication-OtherPleasespecifyWXH').attr('checked')){
             if($('input#hide1').val())
                     {
                            return true; 
                    }
             else  { 
                         alert('Please enter width and height of A4 publication.');
                         return false;
                    }
             }
       if($('#a5publication-OtherPleasespecifyWXH').attr('checked')){
             if($('input#hide2').val())
                    {
                        return true; 
                    }
             else { 
                       alert('Please enter width and height of A5 publication.');
                       return false;
                   }
              }
       if(!$('input#othersizepublication').val()){
           if($('input[name='+ "a4publication" +']:checked').length || $('input[name='+ "a5publication" +']:checked').length)
                 {
                     return true;
                 }
           else { 
                         alert('Please choose the size of publication.'); 
                         return false; 
                 }
         }
      else {
               if($('input[name='+ "a4publication" +']:checked').length || $('input[name='+ "a5publication" +']:checked').length)
                    {
                        alert('Please choose only one option for the size of publication.');
                        $('input[name='+ "a4publication" +']:checked').attr('checked', false);
                        $('input[name='+ "a5publication" +']:checked').attr('checked', false);

                        return false; 
                    }
              else {return true;}
                 
         }
                return true;
        });
});

