var latestKeyword,latestLocation;$(function(){$('#user-lookup').show();$('.secondaryError').hide();$('#user-lookup').submit(function(e){e.preventDefault();$('.alert').removeClass('alert');$('.secondaryError').hide();var pass=true;if(!$('#user-location').val()){$('#user-location').closest('.box').addClass('alert');pass=false;}
if(!$('#user-keyword').val()){$('#user-keyword').closest('.box').addClass('alert');pass=false;}
if(pass){populateClassifications();populateLocations();$('#results').slideDown('fast');$('html,body').animate({scrollTop:$("#results").offset().top},'slow');}});$("#price-table").delegate("select","change",function(){getPrice($(this));});$('#SL, #HW, #MW').mouseover(function(){var infoBoxId='#'+$(this).attr('id')+'-info';$('.hover').removeClass('hover');$(this).addClass('hover');$('#SL-info, #HW-info, #MW-info').hide();$(infoBoxId).show();});});function populateClassifications(){var userClassification=$('#user-keyword').val();if(userClassification!=latestKeyword){latestKeyword=userClassification;$('td.keyword').addClass('loading').removeClass('solo').find('span').remove();;$.ajax({url:'/prices/keyword.do',data:'keyword='+userClassification,dataType:"json",success:function(data){var data=data[0];if(data.status=="hasresults"&&data.results.length>1){var keywordOptions='';for(result in data.results){keywordOptions+="<option value='"+data.results[result].id+"'>"+data.results[result].term+"</option>";}
var bestDefaultOption=smartLookup(userClassification,data.results);$('.keyword select').html(keywordOptions).each(function(){this.selectedIndex=bestDefaultOption;$(this).change();});$('td.keyword').removeClass('loading');$('#show-options').text('Update advertising options');}
else if(data.status=="hasresults"&&data.results.length==1){var keywordOptions="<option value='"+data.results[0].id+"'>"+data.results[0].term+"</option>";$('.keyword select').html(keywordOptions).after("<span>"+data.results[0].term+"</span>");$('.keyword').addClass('solo').removeClass('loading');getPrice($('.keyword select'));}
else{var keywordOptions="<option value=''>-</option>";$('.keyword select').html(keywordOptions).after("<span class='none'>None available</span>");$('.secondaryError').show();$('.keyword').addClass('solo').removeClass('loading');clearPrice($('.keyword select'));}},error:function(e){alert(e);}});}}
function populateLocations(){var userLocation=$('#user-location').val();if(userLocation!=latestLocation){latestLocation=userLocation;$('td.loc').addClass('loading').removeClass('solo').find('span').remove();$('#user-location').closest('.box').removeClass('alert2');$.ajax({url:'/prices/fulllocations.do',data:'location='+userLocation,dataType:"json",success:function(data){var data=data[0];var slFail=false;var hwMwFail=false;if(data.locs.status=="hasresults"&&data.locs.results.length>1){var locationOptions='';for(result in data.locs.results){locationOptions+="<option value='"+data.locs.results[result].id+"'>"+data.locs.results[result].term+"</option>";}
var bestDefaultOption=smartLookup(userLocation,data.locs.results);$('#HW .loc select,#MW .loc select').html(locationOptions).each(function(){this.selectedIndex=bestDefaultOption;$(this).change();});$('#HW td.loc, #MW td.loc').removeClass('loading');}
else if(data.locs.status=="hasresults"&&data.locs.results.length==1){var locationOptions="<option value='"+data.locs.results[0].id+"'>"+data.locs.results[0].term+"</option>";$('#HW td.loc select, #MW td.loc select').html(locationOptions).after("<span>"+data.locs.results[0].term+"</span>");$('#HW td.loc, #MW td.loc').addClass('solo').removeClass('loading');getPrice('#HW td.loc select');getPrice('#MW td.loc select');}else{hwMwFail=true;var locationOptions="<option value=''>-</option>";$('#HW td.loc select, #MW td.loc select').html(locationOptions).after("<span class='none'>None available</span>");$('#HW td.loc, #MW td.loc').addClass('solo').removeClass('loading');clearPrice($('#HW td.loc'));clearPrice($('#MW td.loc'));}
if(data.sllocs.status=="hasresults"&&data.sllocs.results.length>1){var locationOptions='';for(result in data.sllocs.results){locationOptions+="<option value='"+data.sllocs.results[result].id+"'>"+data.sllocs.results[result].term+"</option>";}
var bestDefaultOption=smartLookup(userLocation,data.sllocs.results);$('#SL .loc select').show().html(locationOptions).each(function(){this.selectedIndex=bestDefaultOption;$(this).change();});$('#SL td.loc').removeClass('loading');}
else if(data.sllocs.status=="hasresults"&&data.sllocs.results.length==1){var locationOptions="<option value='"+data.sllocs.results[0].id+"'>"+data.sllocs.results[0].term+"</option>";$('#SL .loc select').html(locationOptions).after("<span>"+data.sllocs.results[0].term+"</span>");$('#SL td.loc').addClass('solo').removeClass('loading');getPrice('#SL .loc select');}else{slFail=true;var locationOptions="<option value=''>-</option>";$('#SL .loc select').html(locationOptions).after("<span class='none'>None available</span>");$('#SL td.loc').addClass('solo').removeClass('loading');clearPrice($('#SL .loc select'));}
if(hwMwFail&&slFail){$('#user-location').closest('.box').addClass('alert2');}},error:function(e){alert(e);}});}}
function getPrice($selectEl){var rowId='#'+$($selectEl).closest('tr').attr('id');var formId=rowId+'-form';if($(rowId).find('[name="classification"]').val()&&$(rowId).find('[name="location"]').val()&&$(rowId).find('[name="type"]').val()){var queryData=$(formId).serialize();var $priceField=$($selectEl).closest('tr').find('td.price');$priceField.html('<img src="/img/ajax-loader.gif" alt="Calculating price" />');$.ajax({url:'/prices/price.do',data:queryData,dataType:"json",success:function(data){$priceField.html('&pound;'+data[0].price);s.tl($('<a href="#">link</a>'),'o','yell/marketing/nmrates/newprice');},error:function(e){alert(e);}});}else{}}
function clearPrice($selectEl){$($selectEl).closest('tr').find('td.price').html('<span class="na">n/a</span>');}
function smartLookup(userTerm,serviceOptions){var suggestedOption=0;var matchFound=false;for(i in serviceOptions){if(!matchFound&&serviceOptions[i].term.toLowerCase()==userTerm.toLowerCase()){suggestedOption=i;matchFound=true;}}
if(!matchFound){var matchTest=new RegExp('\\'+'b'+userTerm.toLowerCase()+'.*');for(i in serviceOptions){if(!matchFound&&matchTest.test(serviceOptions[i].term.toLowerCase())){suggestedOption=i;matchFound=true;}}}
return(suggestedOption);}
$(function(){$('.h').bind('mouseenter mouseleave',function(){$(this).toggleClass('hover');});$('a.play-video').click(function(){GB_show($(this).attr('href'),'400','600','iframe');return false;});$('.gallery').addClass('website-gallery').append('<a href="#" class="next-link">Next</a><a href="#" class="prev-link">Prev</a>').find('img').hide().eq(0).show();$('.next-link').click(function(e){e.preventDefault();var $gallery=$(this).closest('.website-gallery');$gallery.find('img:visible').hide().appendTo($gallery);$gallery.find('img').eq(0).show();});$('.prev-link').click(function(e){e.preventDefault();var $gallery=$(this).closest('.website-gallery');$gallery.find('img:visible').hide().prependTo($gallery);$gallery.find('img').eq(-1).show();});$('#home-promo ul').show().find('li a:first').addClass('current');$('#home-promo>div').eq(0).show();$('#home-promo>div').eq(1).removeClass('loading').hide();$('#home-promo>div').eq(2).removeClass('loading').hide();t=setTimeout('cyclePromos()',interval);$("#home-promo>div").hover(function(){freezeCycling=true;},function(){freezeCycling=false;});$('#home-promo ul a').bind('activate mouseover',function(){freezeCycling=true;if(!$(this).hasClass('current')){var targetDiv=$(this).attr('href');var prevLink=$('#home-promo .current');prevLink.removeClass('current');$(this).addClass('current');Cufon.replace(prevLink);Cufon.replace($(this));$('#home-promo>div:visible').fadeOut('fast',function(){if(!activeAnimation){activeAnimation=true;$(targetDiv).fadeIn('fast',function(){activeAnimation=false;});}});}
return false;}).bind('deactivate mouseout',function(){freezeCycling=false;return false;}).click(function(){return false;});$('#login').click(function(){$(this).closest('form').submit();});});var freezeCycling=false,activeAnimation=false,t,interval=5000;function cyclePromos(){clearTimeout(t);if(!freezeCycling){if($('#home-promo a.current').parent().next().children('a').length){$('#home-promo a.current').parent().next().children('a').trigger('activate').trigger('deactivate');}else{$('#home-promo ul li a').eq(0).trigger('activate').trigger('deactivate');}
t=setTimeout('cyclePromos()',interval);}else{t=setTimeout('cyclePromos()',interval);}}
