var embedCodeTemplate = new String('<iframe width="430" height="340" style="overflow: hidden; border:0;" src="http://www.vitaminwater.com.php5-17.websitetestlink.com/player/?image=#currentImage#&amp;video=#currentVideo#"></iframe>');//('<embed width="430" height="340" flashvars="image=#currentImage#&amp;video=#currentVideo#" quality="high" bgcolor="#ffffff" name="player" id="player" src="http://vitaminwater.dev084.zfort.net/flash/player.swf" type="application/x-shockwave-flash" style="visibility: visible;">');
function load_popup(link, div_id){
		$(link).click(
			function (){
	            currentPopup = '#'+ div_id.attr('id');
				$.blockUI({
					message: div_id,
					focusInput: false,
					forceIframe: true,
					css: {
						top: '10px',
						width: '450px'
					}
				});
				return false;
			}
		);
	}

	function load_download_popup(link, div_id){
		$(link).click(
			function (){
				var str = new String(location.href);
	            $.getJSON('/wsdl/main.ajax.php', 'action=checkLoggedIn&return='+str.replace(/\//gi, '\\/'), function(r){
	            	if(r.Response.Authorized)
	            	{
	            		currentPopup = '#'+ div_id.attr('id');
						$.blockUI({
							message: div_id,
							focusInput: false,
							forceIframe: true,
							css: {
								top: '50px',
								width: '450px'
							}
						});
		            }
	            	else
	            	{
	            		SetCookie('EnterPoint', r.Response.EnterPoint);
		            	location.href = '/join/';
		            }
		        });
				return false;
			}
		);
	}
	
	function load_favorites_popup(link, div_id){
		$(link).click(
			function (){
				var str = new String(location.href);
				var obj = $(this);
	            $.getJSON('/wsdl/main.ajax.php', 'action=checkLoggedIn&return='+str.replace(/\//gi, '\\/'), function(r){
	            	if(r.Response.Authorized)
	            	{
	            		saveToFavorites(obj.attr('rel'));
		            }
	            	else
	            	{
	            		SetCookie('EnterPoint', r.Response.EnterPoint);
		            	location.href = '/join/';
		            }
		        });
				return false;
			}
		);
	}
	
	function load_snapped_favorites_popup(link, div_id){
		$(link).click(
			function (){
				var str = new String(location.href);
				var obj = $(this);
	            $.getJSON('/wsdl/main.ajax.php', 'action=checkLoggedIn&return='+str.replace(/\//gi, '\\/'), function(r){
	            	if(r.Response.Authorized)
	            	{
	            		saveSnappedToFavorites(obj.attr('rel'));
		            }
	            	else
	            	{
	            		SetCookie('EnterPoint', r.Response.EnterPoint);
		            	location.href = '/join/';
		            }
		        });
				return false;
			}
		);
	}

	function initHardCopyForm()
	{
		$("#hardcopy_form").validate({
			errorElement: "li",
			errorContainer: $("#errors"),
			errorPlacement: function(error, element) {
				error.appendTo( $('#errors') );
			},
			rules: {
				txtFirstName: {
					required: true,
					maxlength: 255
				},
				txtLastName: {
					required: true,
					maxlength: 255
				},
				txtProgramName: {
					required: true,
					maxlength: 255
				},
				txtCompanyName: {
					required: true,
					maxlength: 255
				},
				txtAddress1: {
					required: true,
					maxlength: 255
				},
				txtCity:  {
					required: true,
					maxlength: 255
				},
				txtState: {
					required: true,
					maxlength: 255
				},
				txtZip:  {
					required: true,
					maxlength: 5,
					minlength: 5
				},
				txtPhone:  {
					required: true,
					maxlength: 15
				},
				txtFax:  {
					required: false,
					maxlength: 15
				},
				txtEmail: {
					required: false,
					email: true,
					maxlength: 255
				},
				ddlMediaType: {
					required: true,
					maxlength: 255
				},
				txtComments: {
					required: false
				}
			}, 
			messages: {
				txtFirstName: {
					required: 'First name is required'
				},
				txtLastName: {
					required: 'Last name is required'
				},
				txtProgramName: {
					required: 'Program name is required'
				},
				txtCompanyName: {
					required: 'Company name is required'
				},
				txtAddress1: {
					required: 'Address 1 is required'
				},
				txtCity:  {
					required: 'City is required'
				},
				txtState: {
					required: 'State is required'
				},
				txtZip:  {
					required: 'ZIP is required',
					minlength: 'ZIP format is incorrect',
					maxlength: 'ZIP format is incorrect'
				},
				txtPhone:  {
					required: 'Phone is required'
				},
				txtEmail: {
					required: 'Email is required',
					email: 'Email format is incorrect'
				},
				ddlMediaType: {
					required: 'Media type is required'
				}
			},
			submitHandler: function(form){
				$.getJSON('/wsdl/main.ajax.php', 'action=hardcopy&'+$("#hardcopy_form").formSerialize(), function(r) {
					$.unblockUI();
				});
				return false;
				
			}
		});
	}

	function SetCookie(sName, sValue)
	{
		date = new Date();
		var new_day = date.getDate() + 1;  
		date.setDate(new_day); 
		var lifetime = date.toGMTString();
		document.cookie = sName + "=" + escape(sValue) + "; expires=" + lifetime + '; path=/';
	}

	function RemoveCookie(sName, sValue)
	{
		date = new Date();
		var new_day = date.getDate() - 1;  
		date.setDate(new_day); 
		var lifetime = date.toGMTString();
		document.cookie = sName + "=" + escape(sValue) + "; expires=" + lifetime + '; path=/';
	}


	function GetCookie( check_name ) {
		var a_all_cookies = document.cookie.split( ';' );
		var a_temp_cookie = '';
		var cookie_name = '';
		var cookie_value = '';
		var b_cookie_found = false; 

		for ( i = 0; i < a_all_cookies.length; i++ )
		{
			a_temp_cookie = a_all_cookies[i].split( '=' );
			cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
			if ( cookie_name == check_name )
			{
				b_cookie_found = true;
				if ( a_temp_cookie.length > 1 )
				{
					cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
				}
				return cookie_value;
				break;
			}
			a_temp_cookie = null;
			cookie_name = '';
		}
		if ( !b_cookie_found )
		{
			return '';
		}
	}
	
	function countDownloads(linkId)
	{
		$.getJSON('/wsdl/main.ajax.php', 'action=countDownloads&id='+linkId);
	}
	
	function saveToFavorites(eventId)
	{
		$.getJSON('/wsdl/main.ajax.php', 'action=saveToFavorites&event='+eventId, function(r){
			if(r.Response.SaveError.Code != 0)
			{
				$('.favError').html(r.Response.SaveError.Message).show();
				$('.favSuccess').hide();
			}
			else
			{
				$('.favError').html(r.Response.SaveError.Message).hide();
				$('.favSuccess').show();
			}
			$.blockUI({
				message: $('#favorites_popup'),
				focusInput: false,
				forceIframe: true,
				css: {
					top: '100px',
					width: '450px'
				}
			});
		});
		return false;
	}
	
	function saveSnappedToFavorites(snappedId)
	{
		$.getJSON('/wsdl/main.ajax.php', 'action=saveSnappedToFavorites&snapped='+snappedId, function(r){
			if(r.Response.SaveError.Code != 0)
			{
				$('.favError').html(r.Response.SaveError.Message).show();
				$('.favSuccess').hide();
			}
			else
			{
				$('.favError').html(r.Response.SaveError.Message).hide();
				$('.favSuccess').show();
			}
			$.blockUI({
				message: $('#favorites_popup'),
				focusInput: false,
				forceIframe: true,
				css: {
					top: '100px',
					width: '450px'
				}
			});
		});
		return false;
	}
