/* Global Function */
var pp_prompt_timeout = null;

function set_nav_select(id,select_class) {
	if(id) {
		jQuery(document).ready(function() {
			id = id.replace('#','');
			id = id.replace('.','');
			var d = document.getElementById(id);
			if(d) { 
				if(!select_class)
					select_class = 'select';
				var loc = window.location.pathname;
				var loc_lastslash = loc.lastIndexOf('/')+1;	
				var loc_len = loc.length;
				if(loc_lastslash == loc_len)
					loc = loc+'index.php';
				var ext_pos = loc.lastIndexOf('.')+1;	
				var loc_ext = loc; 
				if(ext_pos)
					loc_ext = loc.substr(0,ext_pos-1);

				var host = window.location.hostname;
				var protocol = window.location.protocol;
				host = protocol+'//'+host;
				var pg = loc.substring(loc.lastIndexOf('/')+1);
				var links = d.getElementsByTagName('a');
				if(links) {
					var links_len = links.length;
					var href = '';
					var mhref = '';
					var href_lastslash = 0;
					var href_len = 0;
					if(links_len) {
						for(i = 0; i < links_len; i++) {
							href = links[i].getAttribute('href');
							mhref = href.match(/http/gi);
							if(mhref)
								href = href.replace(host,'');
							href_lastslash = href.lastIndexOf('/')+1;	
							href_len = href.length;
							if(href_lastslash == href_len)
								href = href+'index.php';
							if(href == pg || href == loc || href == loc_ext)
								links[i].className += ' '+select_class;
						}
					}
				}
			}	
		});	
	}	
}	

function popup(content,width,height) {
	var str = '';
	str += '<div id="pp-popup">';
		str += '<div id="pp-popup-A"></div>';
		str += '<div id="pp-popup-C" class="pp-popup-specs">';
			str += '<div id="pp-popup-C-A" class="pp-popup-specs"></div>';
			str += '<div id="pp-popup-C-C" class="pp-popup-specs cf">';
				str += '<a href="javascript:;" class="pp-popup-close">Close</a>';
				str += content;
			str += '</div>'; 
		str += '</div>'; 
	str += '</div>'; 

	jQuery(document).ready(function() {
		jQuery('body').append(str);

		if(jQuery('#pp-popup').length) {
			if(width) {
				jQuery('#pp-popup-C-C').width(width)
				jQuery('#pp-popup-C-A').width(width)
				jQuery('#pp-popup-C-C').width(width)
			}	
			if(height) {
				jQuery('#pp-popup-C-C').height(height)
				jQuery('#pp-popup-C-A').height(height)
				jQuery('#pp-popup-C-C').height(height)
			}	
			jQuery('.pp-popup-close,#pp-popup-A').click(function() {
				remove_element('#pp-popup');
			});
			jQuery(document).keyup(function(e) {
				if(e.keyCode == 27) 
					remove_element('#pp-popup');
			});

			var obj_width = jQuery('#pp-popup-C-C').width();
			var obj_height = jQuery('#pp-popup-C-C').height()+15;

			jQuery('#pp-popup').css({'visibility':'visible','display':'none'});

			var pl = jQuery('#pp-popup-C-C').css('padding-left');
			var pr = jQuery('#pp-popup-C-C').css('padding-right');
			var pt = jQuery('#pp-popup-C-C').css('padding-top');
			var pb = jQuery('#pp-popup-C-C').css('padding-bottom');

			var ie = navigator.appVersion.indexOf('MSIE 6');
			if(ie != -1) {
				var bindWindowHeight = function() {
					var dh = jQuery(window).height();
					if(dh) {
						if(jQuery('#pp-popup').length)
							jQuery('#pp-popup').height(dh);
						if(jQuery('#pp-popup-A').length)
							jQuery('#pp-popup-A').height(dh);
					}	
				};	
				bindWindowHeight();

				jQuery(window).resize(function() {
					bindWindowHeight();
				});
				jQuery(window).scroll(function() {
					var st = jQuery(this).scrollTop();
					if(jQuery('#pp-popup').length)
						jQuery('#pp-popup').css({'top':st+'px'});
				});
			}

			if(pl)
				pl = parseInt(pl);
			if(pr)
				pr = parseInt(pr);
			if(pt)
				pt = parseInt(pt);
			if(pb)
				pb = parseInt(pb);
			
			var tpw = pl+pr;
			var tph = pt+pb;

			jQuery('#pp-popup-C-A').width(obj_width);
			jQuery('#pp-popup-C-A').height(obj_height+tph);
			jQuery('#pp-popup-C-C').width(obj_width-tpw);
			jQuery('#pp-popup-C-C').height(obj_height);

			var half_obj_width = Math.floor(obj_width/2);
			var half_obj_height = Math.floor(obj_height/2);

			jQuery('#pp-popup-C').css({
				'margin-left':-half_obj_width+'px',
				'margin-top':-half_obj_height+'px'
			});

			var bindWindowResize = function() {
				var win_height = jQuery(window).height();

				if(win_height <= obj_height)
					jQuery('#pp-popup-C').css({'top':half_obj_height+'px'});
				else	
					jQuery('#pp-popup-C').css({'top':'50%'});
			};		
			bindWindowResize();

			jQuery(window).resize(function() {
				bindWindowResize();
			});

			jQuery('#pp-popup').fadeIn('slow');
		}
	});
}

function remove_element(id) {
	if(!id)
		return;
	id = id.replace('#','');	
	id = id.replace('.','');	
    if(jQuery('#'+id).length) {
		jQuery('#'+id).fadeOut('slow',function() {
			jQuery(this).remove();
		});
	}
}

function dialog(title,html,auto_close,type) {
	if(!type)
		type = 'default';
	if(auto_close == null || auto_close == undefined)
		auto_close = true;
	var str = '';
	var dh = jQuery(window).height();
	str += '<div id="pp-prompt" class="'+type+'">';
		str += '<div id="pp-prompt-BG" style="height:'+dh+'px"></div>';
		str += '<div id="pp-prompt-1">';
			str += '<div id="pp-prompt-A"></div>';
			str += '<div id="pp-prompt-C">';
				str += '<a href="javascript:;" class="pp-prompt-close">Close</a>';
				if(title)
					str += '<h1>'+title+'</h1>';
				if(html)	
					str += html; 
			str += '</div>';
		str += '</div>';
	str += '</div>';

	if(jQuery('#pp-prompt').length == 0) {
		jQuery('body').append(str);
		jQuery('.pp-prompt-close,#pp-prompt-BG').click(function() {
			dialog_close(); 
		});

		if(jQuery('#pp-prompt-C').length) {
			var h = jQuery('#pp-prompt-C').height();
			var pl = jQuery('#pp-prompt-C').css('padding-top');
			var pr = jQuery('#pp-prompt-C').css('padding-bottom');
			if(pl)
				pl = parseInt(pl);
			if(pr)
				pr = parseInt(pr);
			var th = h+pl+pr;
			jQuery('#pp-prompt').height(dh);
			jQuery('#pp-prompt-1').height(th);
			jQuery('#pp-prompt-A').height(th);

			var ie = navigator.appVersion.indexOf('MSIE 6');
			if(ie != -1) {
				jQuery(window).resize(function() {
					var dh = jQuery(window).height();
					if(dh) {
						if(jQuery('#pp-prompt').length)
							jQuery('#pp-prompt').height(dh);
						if(jQuery('#pp-prompt-BG').length)
							jQuery('#pp-prompt-BG').height(dh);
					}	
				});
				jQuery(window).scroll(function() {
					var st = jQuery(this).scrollTop();
					if(jQuery('#pp-prompt').length)
						jQuery('#pp-prompt').css({'top':st+'px'});
				});
			}

			jQuery('#pp-prompt-1').css({'top':'-'+th+'px'});

			jQuery('#pp-prompt').css({'visibility':'visible'});
			/*
			jQuery('#pp-prompt').animate({'opacity':'1.0'});
			*/
			jQuery('#pp-prompt-1').animate({'top':'0'},'slow',function() {
				if(auto_close)
					pp_prompt_timeout = setTimeout('dialog_close()',3000);
			});
		}
	}	
}		

function dialog_close() {
	if(pp_prompt_timeout)
		clearTimeout(pp_prompt_timeout);

    if(jQuery('#pp-prompt').length) {
        var h = jQuery('#pp-prompt-C').height();
        var pl = jQuery('#pp-prompt-C').css('padding-top');
        var pr = jQuery('#pp-prompt-C').css('padding-bottom');
        if(pl)
            pl = parseInt(pl);
        if(pr)
            pr = parseInt(pr);
        var th = h+pl+pr;
		jQuery('#pp-prompt-1').animate({'top':'-'+th+'px'},function() {
			jQuery('#pp-prompt').remove();
		});
		/*
		jQuery('#pp-prompt').animate({'opacity':'0'},'slow',function() {
		});
		*/
	}
}


function dropdown(id) {
	if(id) {
		id = id.replace('#','');
		id = id.replace('.','');
		jQuery(document).ready(function() {
			if(jQuery('#'+id+' li').length) {
				jQuery('ul.pp-submenu',this).css({'position':'absolute','z-index':'100','opacity':'0','visibility':'hidden'});
				jQuery('#'+id+' li').hover(function() {
					if(jQuery('ul.pp-submenu',this).length) {
						jQuery('a',this).addClass('over');
						jQuery('ul.pp-submenu',this).css({'visibility':'visible'});
						jQuery('ul.pp-submenu',this).stop().animate({'opacity':'1.0'},'fast');
					}
				},function() {
					if(jQuery('ul.pp-submenu',this).length) {
						jQuery('a',this).removeClass('over');
						jQuery('ul.pp-submenu',this).stop().animate({'opacity':'0'},'fast',function() {
							jQuery(this).css({'visibility':'hidden'});
						});
					}
				});
				var bindDropDownPosition = function() {
					if(jQuery('ul.pp-submenu').length) {
						var obj_pos_left = 0;
						var obj_width = 0;
						var win_width = jQuery(window).width();

						jQuery('ul.pp-submenu').each(function(i) {
							var ul = jQuery(jQuery('ul.pp-submenu')[i]);
							obj_pos_left = Math.floor(ul.offset().left);
							obj_width = ul.width();
							if((obj_pos_left + obj_width) > win_width)
								ul.css({'right':'0','left':'auto'});
						});
					}
				};
				bindDropDownPosition();

				jQuery(window).resize(function() {
					bindDropDownPosition();
				});
			}
		});
	}
}

function expandcollapse(id) {
	if(id) {
		id = id.replace('#','');
		id = id.replace('.','');
		jQuery(document).ready(function() {
			if(jQuery('ul.pp-submenu').length) {
				jQuery('ul.pp-submenu').each(function(i) {
					jQuery(this).css({'zoom':1});
					var h = jQuery(this).height();
					jQuery(this).attr('rev',h);
				});
			}	
			if(jQuery('#'+id+' a.menu-item').length) {
				jQuery('ul.pp-submenu',this).css({'visibility':'hidden','height':'0','padding':'0','zoom':'1','line-height':'0','overflow':'hidden'});
				jQuery('#'+id+' li.menu-list-item').click(function() {
					if(jQuery('ul.pp-submenu',this).length) {
						if(jQuery('ul.pp-submenu',this).height() == 0) {
							var h = jQuery('ul.pp-submenu',this).attr('rev');
							jQuery('ul.pp-submenu',this).css({'visibility':'visible','line-height':'normal'});
							jQuery('ul.pp-submenu',this).addClass('menuopen');
							jQuery('ul.pp-submenu',this).stop().animate({'height':h+'px'},'fast');
						}	
						else {
							jQuery('ul.pp-submenu',this).removeClass('menuopen');
							jQuery('ul.pp-submenu',this).stop().animate({'height':'0'},'fast',function() {
								jQuery(this).css({'visibility':'hidden'});
							});
						}
					}
				});

				jQuery('#'+id+' li').hover(function() {
					if(jQuery('ul.pp-submenu',this).length) {
						jQuery('a:first',this).addClass('over');
					}
				},
				function() {
					if(jQuery('ul.pp-submenu',this).length) {
						jQuery('a:first',this).removeClass('over');
					}
				});
			}
		});
	}
}


jQuery(document).ready(function() {
	if(jQuery('.exact-target-email').length) {
		jQuery('.exact-target-email').click(function() {
			jQuery.ajax({ 
				url: '/includes/ajax.php', 
				data: 'cmd=get_widget&widget=email',
				context: jQuery(this), 
				success: function(data) {
					popup(data,340,290);
				}
			});
		});
	}
	if(jQuery('.sms-signup').length) {
		jQuery('.sms-signup').click(function() {
			jQuery.ajax({ 
				url: '/includes/ajax.php', 
				data: 'cmd=get_widget&widget=sms',
				context: jQuery(this), 
				success: function(data) {
					popup(data,340,160);
				}
			});
		});
	}
	if(jQuery('.contact-us-widget').length) {
		jQuery('.contact-us-widget').click(function() {
			jQuery.ajax({ 
				url: '/includes/ajax.php', 
				data: 'cmd=get_widget&widget=contact',
				context: jQuery(this), 
				success: function(data) {
					popup(data,340,600);
				}
			});
		});
	}

	jQuery('.location-get-directions-link').click(function() {
		var obj = jQuery(this).closest('.location-get-directions').find('.location-get-directions-request');
		if(obj.length) { 
			if(obj.css('display') == 'block') {
				jQuery('span',this).html('Get');
				obj.hide();
			}   
			else {
				jQuery('span',this).html('Hide');
				obj.show();
			}
		}
	});
	jQuery('.location-get-directions-form').submit(function(e) {
		e.preventDefault();
		if(jQuery('.location-get-directions-form-address',this).length) {
			var val = jQuery('.location-get-directions-form-address',this).val();
			var daddr = jQuery('.location-get-directions-form-destination-address',this).val();
			if(val != '') {
				window.open("http://maps.google.com/maps?f=d&hl=en&saddr="+escape(val)+"&daddr="+daddr,'_blank');
				return false;
			}
			else 
				alert('Please enter an your address.');
		}
	});
});

