/*general.js - jquery and JS scripts for the template*/

var docUrl = document.domain;

function get_company_data(cref) {
ajaxurl = docUrl+"/wp-content/plugins/UserReg/search_ajax.php";
//drop jquery AJAX requests here - may not need the above createXMLHttpRequest
jQuery.ajax({
type:"POST",
url: ajaxurl,
data: "id="+cref,
dataType:"html",
success:function(html) {

$('#contentbox_searchbox').html(html);
var lpos = $('.ur_contact_content').position();
var rpos = $('#ur_location_desc').position();
var dwidth = rpos.left - (lpos.left+4);
var lheight = lpos.top - rpos.top;
$('#ur_location_desc').css('height',lheight-5+'px');
$('#maincontent').append('<div class=\"horizlineGreen\"></div>');
$('.horizlineGreen').css('height','1px').css('width',dwidth+'px').css('background-color','#016701').css('position','absolute').css('left',lpos.left+5+'px').css('top',lpos.top+'px');
doMapsUpdate(); 

$('p.coref').each(function() {
var ftr = $(this).html();
var ftrArr = new Array();
for (i=0;i<ftr.length;i++) {
ftrArr.push(ftr.charCodeAt(i));
}

$(this).html('');
for (c in ftrArr) {
var img = ftrArr[c]+".png";
var path = docUrl+"/wp-content/themes/GargoylePie/img/skia/"+img;
$(this).append('<img src="'+path+'" alt="'+String.fromCharCode(ftrArr[c])+'" />');
}
});


}
});
}//end function

function doMapsUpdate() {
if ($('#gp_search_gmaps_standalone').length > 0) {//this relates to the customer display page - this way we can set the zoom level.
//we need to get the long/lat from the correct field.
var d = $('.map-location').metadata({type:'attr',name:'data'});
var lat = d.lat;
var lng = d.lng;
//now produce the google map for this page....
geocoder = new google.maps.Geocoder();
var myLatlng = new google.maps.LatLng(lat,lng);
var myOptions = {
zoom: 14,
center: myLatlng,
navigationControl: true,
mapTypeId: google.maps.MapTypeId.ROADMAP,
scaleControl: false,
mapTypeControl:false
};
var map = new google.maps.Map(document.getElementById('gp_search_gmaps_standalone'), myOptions);

//add the marker
var marker = new google.maps.Marker({position: myLatlng,map: map}); 
    

}//end if

}//end function


function link_to(cref,url) {
ajaxurl = docUrl+"/wp-content/plugins/UserReg/redirect_ajax.php";
//drop jquery AJAX requests here - may not need the above createXMLHttpRequest
jQuery.ajax({type:"POST",url: ajaxurl,data: "id="+cref,dataType:"html",success:function(html) {
//window.open(url);

}});}//end function



$(document).ready(function(){


$('#mainmenu > li, div#backbtn, p#to-top').hover(function(){$(this).css('color','#ffff00');$('ul', this).fadeIn(300);},function(){$(this).css('color','#ffffff');$('ul', this).fadeOut(300);}).mousedown(function(){$(this).css('background-position','0px -25px')}).mouseup(function(){$(this).css('background-position','0px 0px')});


$('#backbtn').click(function() {window.history.back();});

//glossa-z a-z header. We'll position it in the green bar below the menu

$('.letterbox, .gp_cal_month').clone().appendTo('.menuspacer, .menuspacerfooter');
$('#letterholder, #gp_cal_spanholder > .gp_cal_month').remove();

$('.letterbox, .gp_cal_month').hover(function(){$(this).css('color','#ffff00');},function(){$(this).css('color','#ffffff')}).mousedown(function(){$(this).css('background-position','0px -25px')}).mouseup(function(){$(this).css('background-position','0px 0px')});
$('#nav_mid > p, #nav_up p, #nav_down p').hover(function(){$(this).css('color','#ffff00');},function(){$(this).css('color','#ffffff')});
$('#nav_up, #nav_mid, #nav_down').mousedown(function() {if ($(this).css('background-position-y')) {var ypos = parseInt($(this).css('background-position-y'))+"px";}else {var ypos = $(this).css('background-position').split(" ")[1];}$(this).css('background-position','-75px '+ypos)}).mouseup(function() {if ($(this).css('background-position-y')) {var ypos = parseInt($(this).css('background-position-y'))+"px";}else {var ypos = $(this).css('background-position').split(" ")[1];}$(this).css('background-position','0px '+ypos)});

// rabmcnab - commented out search results

//$('#gp_search_results > li').live('focus',function() {$(this).siblings('li').blur().end().css('background-color','#016701').css('color','#ffffff').addClass('has-focus');
//do an ajax call to get the data from the server for this result. We'll return it as HTML and do a straight insert into #contentbox_searchbox
//var cref = $(this).attr('id');
//get_company_data(cref);
//test data to ensure the element is focussing correctly
//});

//$('#gp_search_results > li').live('blur',function() {$(this).css('background-color','#ffffff').css('color','#000000').removeClass('has-focus')});
//$('#gp_search_results > li').live('mousedown',function() {$(this).focus()});

//now the actual controls
$('#nav_down').bind('mousedown',function() {$('#gp_search_results > li.has-focus').next().focus();})//end down function
$('#nav_up').bind('mousedown',function() {$('#gp_search_results > li.has-focus').prev().focus();})//end down function
$('#nav_mid').bind('mousedown',function() {self.location.href = document.referrer});

//if we click on the gp_search_view_link from the main search page all we need to do is focus the #gp_search_results > li and that will trigger the page change

$('.gp_search_view_link').click(function() {
var cid = $(this).attr('id');
$('#gp_search_results li[id ='+cid+']').focus();
});

//check for the gp_search container then push each postcode into an array before doing the GoogleMaps thing
if ($('#gp_search_gmaps_frame').length > 0) {
	if ($('#search_result_div').length > 0 ) {

$('#gp_search_gmaps_frame').jMapping({side_bar_selector: '#search_result_div',map_config: function(map){
      map.addControl(new GSmallZoomControl());
     } 
      });
} else if ($('#ur_bottom_content').length > 0) {
$('#gp_search_gmaps_frame').jMapping({side_bar_selector: '#ur_bottom_content',map_config: function(map){
      map.addControl(new GSmallZoomControl());
} 
      });
}
}//end content_searchbox check


//do search screen controls
$('.expandform').click(function() {
$('.searchform').slideUp();
$(this).next('dd').slideDown();

});//end form slidedown

//do mailform check
$('#emailform input[type=submit], #urloginform input[type=submit]').click(function(e) {
e.preventDefault();
var isGood=true;
var errArr = new Array();
if ($('.errormsg').length > 0) {$('.errormsg').remove();}
//check the "required" fields in each of the displayed fieldsets are filled in and create a DOM element containing a notice for each one if it's not.

$('#emailform, #urloginform').find('.required').each(function(){
var msg="This field is required";
if (!$(this).val().length) {
var w = $(this).width()+"px";
var pos = $(this).position();
var posleft = (pos.left+$(this).width())+"px";
var postop = pos.top+"px";
var cVar = $('.errormsg').length;
$(this).parent().append('<p class="errormsg" id="msg'+cVar+'"></p>');
var el = "#msg"+cVar;
$(el).css('position','absolute').css('left',posleft).css('top',postop).css('color','#ffffff').css('font-weight','bold').css('font-size','0.8em').css('text-align','center').html(msg).end();
isGood = false;}
});//end find required


if (isGood) {$(this).parent('form').submit();}

});//end function

doMapsUpdate();

$('a.company_link').live('click',function(e) {var id = $(this).attr('id');var url = this.href;$(this).attr('target','_blank');link_to(id,url);});

$('p#to-top').click(function() {
window.scrollTo(0,0);

});

});//end document ready
