function widget(object) {
	var widget_background = '<div id="widget_background"></div>';
	var widget_box   = 	'<div id="widget">';
	widget_box		+=		'<center>';
	widget_box		+= 			'<div class="widget_outer_transparent_container"></div>';
	widget_box		+=			'<div class="widget_outer_container">';
	widget_box  	+=		 		'<div class="widget_close">&#10006;</div>';
	//widget_box  	+= 				'<div class="widget_container_top"><div class="drag_handle"></div></div>';
	widget_box  	+= 				'<div class="widget_container">';
	widget_box  	+= 					'<div class="widget_inner_container">';
	if (object.logo) {
		widget_box		+= 					'<div class="widget_logo"><img src="'+href_url+object.logo+'"></div>';
	}
	if (object.title) {
		widget_box		+= 					'<div class="widget_title drag_handle">'+object.title+'</div>';
	}
	if (object.sub_title) {
		widget_box		+= 					'<div class="widget_sub_title">'+object.sub_title+'</div>';
	}
	if (object.welcome) {
		widget_box		+=								'<div class="widget_content">';
		widget_box		+=									'<div style="padding:20px 0 0 80px">';
		widget_box		+=										'<table cellspacing="0" cellpadding="0">';
		widget_box		+=											'<tr>';
		widget_box		+=												'<td style="line-height:20px;color:#777;font-size:18px;font-weight:normal">Please choose your country&nbsp;&nbsp;</td>';
		widget_box		+=												'<td style="text-align:right;vertical-align:middle;">';
		widget_box		+=													'<select style="font-size:18px;font-weight:normal;color:#777" onchange="set_location(\'/\', $(this).val())">';
		widget_box		+=														'<option value=""></option>';
		$.each(countries_list, function(country_title, country_value) {
			widget_box		+=													'<option value="'+country_value+'" style="font-size:18px;font-weight:normal;color:#777">'+country_title+'</option>';
		});
		widget_box		+=													'</select>';
		widget_box		+=												'</td>';
		widget_box		+=											'</tr>';
		widget_box		+=										'</table>';	
		widget_box		+=									'</div>';								
		widget_box		+=								'</div>';
	} else {
		widget_box		+=					'<div class="widget_content">';
		widget_box		+=						'<div class="images_container"><div class="image"><img src="'+href_url+object.parameters.large_images_sets[0].image+'"></div></div>';
		widget_box		+=						'<div class="data">';
		widget_box		+=							'<div class="features">';
		widget_box		+=								'<div class="features_title">Features</div>';
		$.each(object.parameters.features_list, function(i, item) {
			widget_box		+=							'<div class="feature">'+item+'</div>';
		});
		widget_box  	+= 							'</div>';
		widget_box		+=							'<div class="measurements">';
		widget_box		+=								'<div class="measurements_title">Measurements</div>';
		if (object.parameters.measurements) {
			widget_box		+=							'<div class="measurement">External: '+object.parameters.measurements.external_width+'x'+object.parameters.measurements.external_depth+'x'+object.parameters.measurements.external_height+' (WxDxH)</div>';
		}
		widget_box  	+= 							'</div>';
		widget_box  	+= 						'</div>';
		widget_box		+=						'<div class="actions">';
		if (object.parameters.assembly && object.parameters.assembly.instructions) {
			widget_box  	+= 							'<div class="link"><a href="'+href_url+object.parameters.assembly.instructions+'" target="_blank">Assembly Instructions</a> &rsaquo;</div> &nbsp; ';
		}
		if (object.groups && object.groups[0]) {
			widget_box  	+= 							'<div class="link"><a href="'+href_url+'categories/customer_service?category='+object.groups[0]+'" target="_blank">Customer Service</a> &rsaquo;</div>';
		}
		widget_box  	+= 							'<div class="addthis_toolbox addthis_default_style addthis_32x32_style">';
		widget_box  	+= 								'<a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a><a class="addthis_counter addthis_bubble_style"></a>';
		widget_box  	+= 							'</div>';	
		widget_box  	+= 						'</div>';
		widget_box  	+= 					'</div>';
	}
	widget_box  	+= 				'</div>';
	//widget_box  	+= 				'<div class="widget_container_bottom"></div>';
	widget_box		+=			'</div>';
	widget_box		+=		'</center>';
	widget_box  	+= 	'</div>';
	
	$('body').prepend(widget_background);
	$('body').prepend(widget_box);
	
	$('#widget_background').height($(document).height());
	$('#widget').height($(document).height());
	$('#widget .widget_outer_transparent_container').height($('#widget .widget_outer_container').height());
	
	var widget_position_top = (($(window).height()/2) - $('#widget .widget_outer_container').height()/2) + $(window).scrollTop();
	
	$('#widget .widget_outer_transparent_container').css({top:widget_position_top});
	$('#widget .widget_outer_container').css({top:widget_position_top});
	if ($('.drag_handle').length > 0) {
		//$('#widget .widget_outer_container').draggable({handle:'.drag_handle'});
	}
	//$('#widget').disableSelection();
	
	$('#widget_background').css('opacity', 0.5).hide(); /* If this opacity is not set, then in IE the fade-in starts from 100% and the goes transparent */
	$('#widget').hide();
		
	if ($.browser.msie) {
		$('#widget_background').show(0, function() {
			$('#widget').show(0);
		});
	} else if (mobile) {
		// We first need to hide all YouTube objects, since otherwise they will appear above the widget
		$('.frame .media').hide();
		// Now we show the widget, without animation
		$('#widget_background').show();
		$('#widget').show();
		FB.XFBML.parse();
		gapi.plusone.go();
	} else {
		//$('#widget_background').show('slide',{'direction':'left'}, 500, function() {
		$('#widget_background').fadeIn('fast', function() {
			//$('#widget').show('slide',{'direction':'right'}, 500, function() {
			$('#widget').fadeIn('fast', function() {
				// If we have like-buttons / g+ buttons - we need to re-render them
				FB.XFBML.parse();
				gapi.plusone.go();
			});
			//$('#widget').fadeIn('fast');
		});
	}
	
	if (object.fb_post) {
		// Post to wall
		FB.api('/me/feed', 'post', {message:object.fb_post, name:object.fb_post, link:http_url+referrer}, function(response) {
			if (!response || response.error) {
				//alert('Error occured');
		  	} else {
				//alert('Post ID: ' + response.id+"\n"+'Referrer: '+http_url+referrer);
		  	}
		});
	}
	
	if (object.callback) {
		//callback_function_string = callback.toString().replace(/"/g,'\'');
		$('#widget .widget_continue').bind('click', object.callback);
		if (object.cancel_callback) {
			$('#widget .widget_cancel_callback').bind('click', object.cancel_callback);
		}
		if (object.alt_callback) {
			$('#widget .widget_alt_callback').bind('click', object.alt_callback);
		}
		if (object.dont_touch_widget_close) {
			$('#widget .widget_close').bind('click', function() {
				widget_close();
			});
		} else if (object.cancel_callback) {
			$('#widget .widget_close').bind('click', object.cancel_callback);	
		} else {
			$('#widget .widget_close').bind('click', object.callback);
		}
	} else {
		$('#widget .widget_close').bind('click', function() {
			widget_close();
		});
		// For iPhone users
		$('#widget .widget_close').bind('tap', function() {
			widget_close();
		});
	}
	
}

function widget_close() {	
	if ($.browser.msie) {
		$('#widget').hide(0, function() {
			$('#widget_background').hide(0, function() {
				$('#widget_background').remove();
				$('#widget').remove();
				// We need to show all YouTube objects, since they will hidden so won't appear above the widget
				if (mobile) {
					$('.frame .media').show();
				}
			});
		});
	} else if (mobile) {
		$('#widget').hide().remove();
		$('#widget_background').hide().remove();
		// We need to show all YouTube objects, since they will hidden so won't appear above the widget
		if (mobile) {
			$('.frame .media').show();
		}
	} else {
		//$('#widget').hide('slide',{'direction':'left'},500,function() {
		$('#widget').fadeOut('fast', function() {
			//$('#widget_background').hide('slide',{'direction':'right'},500,function() {
			$('#widget_background').fadeOut('fast', function() {
				$('#widget_background').remove();
				$('#widget').remove();
			});
		});
	}
}

// Capturing 'escape' keystore for widget close
$(document).keyup(function(e) {
	//if (e.keyCode == 13) { $('.save').click(); } // Enter
	if (e.keyCode == 27) { widget_close(); } // ESC
});

/* ## */

function set_location(referrer, location) {
	if (location != '') {
		$.get('/', {'location':location, 'referrer':referrer}, function(referral_target) {
			window.location = referral_target;
        });
    }
}

 // base_path is the current URL
function set_scale(scale, doc_type) {
    if (scale != '') {
        $.get(base_path,{'scale':scale,'doc_type':doc_type},
            function(data) {
                window.location = base_path;
            }
        );
    }
}

function switch_service_theme(theme_color, theme_path, base_theme) {
     // A valid category has been selected
    if (theme_color) {
        $('#service_box .top_corners').css('background','url('+theme_path+theme_color+'/sidebar__box__175__top_corners.png)');
        $('#service_box .mid_body').css('background','url('+theme_path+theme_color+'/sidebar__box__175__mid_bg.png)');
        $('#service_box .btm_corners').css('background','url('+theme_path+theme_color+'/sidebar__box__175__btm_corners.png)');
        $('#service_box .mid_title, #service_box .info_form a, #service_box .tiny_arrow').css('color', '#FFF');
     // The dummy option'select a category' has been selected
    } else {
         // The current page has a theme_color setting
        if (base_theme != 'default') {
            $('#service_box .top_corners').css('background','url('+theme_path+base_theme+'/sidebar__box__175__top_corners.png)');
            $('#service_box .mid_body').css('background','url('+theme_path+base_theme+'/sidebar__box__175__mid_bg.png)');
            $('#service_box .btm_corners').css('background','url('+theme_path+base_theme+'/sidebar__box__175__btm_corners.png)');
         // The current page uses the default theme
        } else {
            $('#service_box .top_corners').css('background','url('+theme_path+base_theme+'/sidebar__box__175__top_corners.png)');
            $('#service_box .mid_body').css('background','url('+theme_path+base_theme+'/sidebar__box__175__mid_bg.png)');
            $('#service_box .btm_corners').css('background','url('+theme_path+base_theme+'/sidebar__box__175__btm_corners.png)');
            $('#service_box .mid_title, #service_box .info_form a, #service_box .tiny_arrow').css('color', '#939598');
        }
    }
}

function update_service_links(service_links,category,product) {
    var link_href;
    for (var link_id in service_links) {
        link_href  = service_links[link_id].base_url;
        link_href += service_links[link_id].vars.category && category ? '&category='+category : '';
        link_href += service_links[link_id].vars.product && product ? '&product='+product : '';
        $('#'+link_id+'_link').attr('href',link_href);
    }
}

/* ## */

function toggle_advanced_search(action) {
    if (action == 'open') {
        $('#show_advanced_search').toggle();
        $('#hide_advanced_search').toggle();
        $('#closed_btm_corners').toggle();
        $('#advanced_search_form').toggle('slide', {direction:'up'}, 350);
    } else if (action == 'close') {
        $('#hide_advanced_search').toggle();
        $('#show_advanced_search').toggle();
        $('#advanced_search_form').toggle('slide', {direction:'up'}, 350);
        setTimeout('$(\'#closed_btm_corners\').toggle();', 370);
    }
}

function toggle_language_menu() {
     // Open menu
    if (!$('#lang_switch').hasClass('on')) {
        $('#lang_switch').toggleClass('on');
        $('#lang_menu').toggle('slide', {direction:'up'}, 150);
     // Close menu
    } else {
        $('#lang_menu').toggle('slide', {direction:'up'}, 150);
        setTimeout('$(\'#lang_switch\').toggleClass(\'on\');', 160);
    }
}

function toggle_dialog(dialog_id) {
    $('#'+dialog_id).toggle('slide', {direction:'left'}, 250);
}

/* ## */

function field_enable(field_id) {
    $('#'+field_id).attr('disabled','');
    $('#'+field_id).removeClass('disabled');
}

function field_disable(field_id) {
    $('#'+field_id).attr('disabled','disabled');
    $('#'+field_id).addClass('disabled');
}

function field_reset_text(field_id) {
    $('#'+field_id).val('');
}

function field_reset_select(field_id) {
    $('#'+field_id+' option:first').attr('selected','selected');
}

function field_redirect(base_url, get_values) {
    var get_vars = '';
    for (var get_name in get_values) {
        get_vars += '&'+get_name+'='+$('#'+get_values[get_name]).val();
    }
   	window.location = base_url+get_vars;
}

function field_numeric_only(e) {
    var keynum;
    var keystr;
    var keychar;
    var numcheck;
    var allowed_keys = '|7|8|9|16|35|37|39|45|46|48|49|50|51|52|53|54|55|56|57|58|96|97|98|99|100|101|102|103|104|105|';
    
    if(window.event) {
        keynum = e.keyCode;
    }
    else if(e.which) {
        keynum = e.which;
    }
    
    var keystr = new RegExp('/\|'+keynum.toString()+'\|/');
    if (allowed_keys.search(keystr) != '-1') {
        return true;
    } else {
        return false;
    }
}

/* ## */

function select_loading_animation(list_id, list_width, action) {
     // Loading animation - on
    if (action == 'on') {
         // Hide the list
        $('#'+list_id).hide();
         // Clone the matching loading animation and attach it to the parent of the list
        $('#select_loading_animation_'+list_width).clone().prependTo($('#'+list_id).parent());
         // The loading animation is preloaded with display:none, so we need to display:block the clone 
        $('#'+list_id).parent().find('img').css({'display':'block'});
     // Loading animation - off
    } else if (action == 'off') {
         // Remove the local clone of the loading animation
        $('#'+list_id).parent().find('img').remove();
         // Show the list
        $('#'+list_id).show();
    }
}

 // Dummy loading animation for a categories list that doesn't change from one selection to another,
 // for example when the trigger is a countries list
function update_categories_list(trigger_id, target_id, target_width, url_redirect, country_list_id, category_list_id, category_id) {
     // One of the valid trigger fields has been selected
    if ($('#'+trigger_id).val() != '') {
        /*
        if (target_id == 'where_buy_countries_list') {
        	$.get('/', {action:'filter_where_to_buy_countries', category_id:category_id}, function(data) {
        		$(target_id+' option').remove();
        		if (data.countries) {
        			$(data.countries).each(function (key, value) {
        				$(target_id).append('<option value="'+key+'">'+value+'</option>');
        			});
        		}
        	}, 'json');
        }
        */
         // Turn on loading animation
        select_loading_animation(target_id, target_width, 'on');
         // Enable and reset the categories list
        field_enable(target_id)
        if (!url_redirect) {
			field_reset_select(target_id);
		}
         // Turn off loading animation
        setTimeout('select_loading_animation(\''+target_id+'\', \'\', \'off\')', 2000);

     // The dummy "Please select a trigger" field has been selected
    } else {
         // Disable and reset the categories drop down
        field_disable(target_id);
        if (!url_redirect) {
			field_reset_select(target_id);
		}
    }
	if (url_redirect && country_list_id && category_list_id) {
		// If the above variables exist - that means we need to automatically redirect (like changing the next field) to another page with specific values
		field_redirect(url_redirect,{'country':country_list_id, 'category':category_list_id});
	}
	
}

function update_products_list(list_id, GET_product, category_id, list_width, lang_prefix) {
     // One of the valid category fields has been selected
    if (category_id != '') {
         // Turn on loading animation
        list_width != 'no-effect' ? select_loading_animation(list_id, list_width, 'on') : false;
         // Send the request for the products list
        $.get('/', {'products_list':'1', 'category':category_id, 'lang-prefix':lang_prefix},
            function(products) {
                if (products[0]['id']) {
                	var options  = '<option value=""></option>';
	                var selected = '';
	                for (i=0; i < products.length; i++) {
						selected = products[i]['id'] != GET_product ? '' : ' selected';
	                    options += '<option value="'+products[i]['id']+'"'+selected+'>'+products[i]['title']+'</option>';
	                }
                } else {
                	var options  = '<option value="">'+products+'</option>';
                }
                $('#'+list_id).html(options);
	             // Enable the products drop down
	            field_enable(list_id);
	             // Turn off loading animation
	            list_width != 'no-effect' ? setTimeout('select_loading_animation(\''+list_id+'\', \'\', \'off\')', 2000) : false;
            }, 'json'
        )
     // The dummy "Please select a category" field has been selected
    } else {
        var options = '<option value="">Select a product...</option>';
        $('#'+list_id).html(options);
         // Disable the products drop down
        field_disable(list_id);
    }
}

/* ## */

function bookmark_page(title, url) {
     // Firefox
    if (window.sidebar) {
    	window.sidebar.addPanel(title, url,"");
      // IE Favorite
    } else if( window.external ) {
    	window.external.AddFavorite(url,title); 
    }
     // Opera Hotlist    	
    else if(window.opera && window.print) {
    	return true; 
    }
}

 // Uses animate() as an alternative for slideUp() - this is a quick fix for the annoying flicker in IE
 // Credit: http://siderite.blogspot.com/2009/08/jquery-slideup-flickers-in-internet.html
function slideup_alt(jquery_object) {
    var h = jquery_object.height();
    jquery_object.animate ({ height: '1px' }, 'fast',
        function() { 
            jquery_object.hide();
            jquery_object.height(h);
        } 
    );
}

function faq_toggle(question) {
     // Toggle on
    if (!$(question).parent().hasClass('selected')) {
        $(question).parent().addClass('selected');
        $(question).parent().find('.answer').slideDown('fast');
     // Toggle off
    } else {
        $(question).parent().removeClass('selected');
        slideup_alt($(question).parent().find('.answer'));
    }
}

function switch_powered_by(item) {
	setTimeout(function() {
		if (item) {
			$('.powered_by .container:visible').hide();
			setTimeout(function() {
				$('.powered_by #powered_by_'+item).show('slide', {'direction':'down'}, 300);
			}, 250);
		} else {
			$('.powered_by .container:visible').hide('slide', {'direction':'left'}, 200);
		}
	}, 250);
}


//Waze
g_waze_config = {
	div_id:"map",
	locale : "israel",
	center_lon:34.7898,
	center_lat:32.08676,
	zoom:1,
	token:"7311150a-eab8-4d24-a594-49b745683a59",
	alt_base_layer:"israel_colors",
	alt_map_servers:"http://ymap1.waze.co.il/wms-c/",
	callback:onInit,
	init_on_page_load:false
};

//called when map loads
function onInit () {	
	//This is for text boxes
	lll = new OpenLayers.Layer.Text("text", {location: "?action=get_maps_val"});
	g_waze_map.map.addLayer(lll);
	//map.zoomToMaxExtent();
}

/* ## */

$(document).ready(function() {
	
	/* jQuery - Datepicker */
	var date_format = 'dd/mm/yy'; // This is what the user will see
    //var date_format_alt = 'dd/mm/yy'; // This is what you need
    //$.datepicker.setDefaults($.datepicker.regional['he']);
	//$('#param__personal_details__birth_date__visual').datepicker({ changeYear:true, yearRange:'-80:-10', defaultDate:'-20y', dateFormat: date_format, altField: '#param__personal_details__birth_date', altFormat: date_format_alt});
	$('#param__form_fields__purchased_date').datepicker({changeYear:true, dateFormat:date_format});
	$('#date_of_purchase').datepicker({changeYear:true, dateFormat:date_format});
	
	$('.ui-datepicker').hide(); // Sometimes the datepicker appears at the bottom of the page, but it shouldn't.. it's a bug in jQuery	
	
});

