

$(document).ready(function() {
	// $('#q').liveSearch({url: '/b2b/company/search/q/'});
});

var Upload = {
	init: function(){
		if($('#swfUploadContainer').size() > 0){
			var settings = {
				flash_url : "../../../flash/swfupload.swf",
				upload_url: "/b2b/frontend_dev.php/sfUploadManager/create",
				file_size_limit : "10 MB",
				file_types : "*.*",
				file_types_description : "All Files",
				file_upload_limit : 100,
				file_queue_limit : 0,
				custom_settings : {
					progressTarget : "fsUploadProgress",
					cancelButtonId : "btnCancel"
				},
				// debug: true,

				// Button settings
				button_image_url: "../../../images/upload.png",
				button_width: "65",
				button_height: "29",
				button_placeholder_id: "swfUploadContainer",
				button_text: '<span class="theFont">Select Files</span>',
				button_text_style: ".theFont { font-size: 16; }",
				button_text_left_padding: 12,
				button_text_top_padding: 3,
				
				// The event handler functions are defined in handlers.js
				file_queued_handler : fileQueued,
				file_queue_error_handler : fileQueueError,
				file_dialog_complete_handler : fileDialogComplete,
				upload_start_handler : uploadStart,
				upload_progress_handler : uploadProgress,
				upload_error_handler : uploadError,
				upload_success_handler : uploadSuccess,
				upload_complete_handler : uploadComplete
				// queue_complete_handler : queueComplete	// Queue plugin event
			};

			var swfu = new SWFUpload(settings);
			
		}
	}
}

$(document).ready(function() { Upload.init(); });

var Forms = {
	init: function(){
		this.urlCheck();
		
		var companyForms = new Array('video', 'images', 'product', 'contacts', 'mydetails')
		/*
		if($('#contact-form').size()>0){
			$('#contact-contents').hide('blind',{},1000);
			
			$('#contact-details').click(function(){
				
				if($('.show-hide-contact').html() == "+"){
					$('.show-hide-contact').html('-');
					$('#contact-contents').show('blind',{},1000);
				}else{
					$('.show-hide-contact').html('+');
					$('#contact-contents').hide('blind',{},1000);
				}
			})
		}
		*/
		return;
		for(var i=0; i<companyForms.length; i++){

			var formBlock = companyForms[i];
	
			$('#'+formBlock+'-contents').hide('blind',{},1000);

			$('#'+formBlock+'-titlebar').click(function(){
				alert('click');
				alert($('#'+formBlock+'-contents').css('display'));
				if($('#'+formBlock+'-contents').css('display') == "block"){
					$('#'+formBlock+'-show-hide').html('-');
					$('#'+formBlock+'-contents').show('blind',{},1000);
				}else{
					$('#'+formBlock+'-show-hide').html('+');
					$('#'+formBlock+'-contents').hide('blind',{},1000);
				}
			})

		}
		
		
		
		
		
	},
	
	urlCheck: function(){
		$('#company_url,#contact_url,#contact_linkedin_url').blur(function(){
			if($(this).val() == '') return;
			if($(this).val().search('http://') == (-1)){
				!$(this).val('http://'+$(this).val());
			}
		});
	}
}


$(document).ready(function() { Forms.init(); });

function showHideSection(obj,target){
	var formBlock = target;

	// $('#'+formBlock+'-contents').hide('blind',{},1000);

	    if($('#'+formBlock+'-contents').css('display') == "block"){
		// if($('#'+formBlock+'-show-hide').html() == "+"){
			$('#'+formBlock+'-show-hide').html('+');
			$('#'+formBlock+'-contents').hide('blind',{},500);
		}else{
			$('#'+formBlock+'-show-hide').html('-');
			$('#'+formBlock+'-contents').show('blind',{},500);
		}

	
}

var Tags = {
	init: function(){
		this.tag_list = ['gsm', 'gprs', 'edge', 'umts', 'cdma', 'evdo', 'wifi', 'gps', 'bluetooth', 'wimax', 'rfid', 'wireless-usb', 'zigbee', 'lan', 'pan', 'wan', 'sdr', 'web-design', 'motion-capture', 'animation', 'gaming', 'entertainment', 'visual-effects', 'film', 'television', 'social-networking', 'identity', 'mobile-web', 'mobile-applications', 'mobile-software', 'iphone', 'digital-signage', 'security', 'elearning', 'payments', 'voip', 'infrastructure'];
		
		$('#company_tags').autocomplete({
		        // serviceUrl: 'service/autocomplete.ashx', // Page for processing autocomplete requests
		        minChars: 2, // Minimum request length for triggering autocomplete
		        delimiter: /(,|;)\s*/, // Delimiter for separating requests (a character or regex)
		        maxHeight: 400, // Maximum height of the suggestion list, in pixels
		        width: 300, // List width
		        zIndex: 9999, // List's z-index
		        deferRequestBy: 0, // Request delay (milliseconds), if you prefer not to send lots of requests while the user is typing. I usually set the delay at 300 ms.
		        params: { }, // Additional parameters
		        onSelect: function(data, value){
					$('#company_tags').val($('#company_tags').val()+', ');
			    }, // Callback function, triggered if one of the suggested options is selected,
		        lookup: Tags.tag_list // List of suggestions for local autocomplete
		    });
	}
}


$(document).ready(function() { 
	if($('#company_tags').size() > 0){Tags.init();} 
});

var Search = {
	init: function(){
		this.AjaxManager = $.manageAjax.create('search', { 
		    queue: 'clear',  
		    cacheResponse: true,
		    abortOld: true,
		    cacheResponse: true
		});

		
		this.q = '';
		var me = this;
		$('#digi-search').keyup(function(e) { 
			var query = $('#digi-search').val();

			if(query.length >= 2){
				if(me.q != query){
					me.query(query);
				}
			}
		});
		
	},
	
	query: function(val){
		this.q = val;
		
		// $.get("/b2b/company/search", { q: val },
		//   function(data){
		//     $('.companies-list tbody').html(data);
		//   });

		this.AjaxManager.add({ 
		  success: function(data) { 
			  $('.companies-list tbody').html(data);
		  }, 
		  url: '/b2b/company/search?q='+val 
		});
	}
}

$(document).ready(function() { 
	if($('#digi-search').size() > 0){Search.init();} 
});


var ToolTips = {
	init: function(){
		this.config = { 
					position: {
				                corner: {
				                     tooltip: 'leftMiddle',
				                     target: 'rightMiddle'
				                 }
				               },
					style: { name: 'dark', tip: true,}
				};
		
		if(!User.isAuthenticated()){
			// this.config.content = 'Please log in to add a favorite';
		}
		
		$('.tooltip[title]').qtip(this.config );
	},
	
	initOne: function(e){
		$('#'+e+'[title]').qtip(this.config);
	}
	
}

$(document).ready(function() { 
//  now initialized company/_list && company/show to prevent doubles!
//	ToolTips.init();
});

var Favorites = {
	init:function(){
		if(!User.isAuthenticated()) return;
		this.initAdd();
		this.initRemove();
	},
	
	initAdd: function(){
		$('.add-favorite').click(function(e){
			// $(this).replaceWith('mk');
			var me = this;
			$('div.qtip').hide();
			$(me).fadeOut('fast');
			
			$.get("/b2b/company/addfavorite", { id: this.value},
			  function(data){
				$(me).replaceWith(data);
				$(me).fadeIn('slow',function(){
					ToolTips.initOne(me.id);
				});
				
			  });
		});
	},
	
	initRemove: function(){
		$('.my-favorite').click(function(e){
			// $(this).replaceWith('mk');
			var me = this;
			$('div.qtip').hide();
			$(me).fadeOut('fast');
			
			$.get("/b2b/company/removefavorite", { id: this.value},
			  function(data){
				$(me).replaceWith(data);
				$(me).fadeIn('slow',function(){
					ToolTips.initOne(me.id);
				});
				
			  });
		});
	}
	
}

$(document).ready(function() { 
	// called in company/list and company/show (used for ajax reloads)
	// Favorites.init();
});


var Mobile = {
	init: function(){
		// if($('#send2Mobile').size() == 0) return;
		$('#getXomo').click(function(){
			
			if($('#xomo-mobile').val().length > 10){
				$('#getXomo').html('Sending ...');
				$.get("/b2b/contact/sms", { mobile: $('#xomo-mobile').val(), type: 'xomo' },
						function(data){
						  if(data == 'error'){
							alert('Sorry there was an error sending an SMS to your mobile.  Please ensure that you have entered the correct number.');
						  }else{
						 	  // $('#send-to-mobile').html(data);
							  $('#getXomo').html('Sent!');
							
						  }
						} 
					);
			}
		})
		
		
		$('#getBB').click(function(){
			
			if($('#xomo-bb').val().length > 10){
				$('#getBB').html('Sending ...');
				$.get("/b2b/contact/sms", { mobile: $('#xomo-bb').val(), type: 'bb' },
						function(data){
						  if(data == 'error'){
							alert('Sorry there was an error sending an SMS to your mobile.  Please ensure that you have entered the correct number.');
						  }else{
							  $('#getBB').html('Sent!');
						  }
						} 
					);
			}
		})
		
		$('#send2Mobile').click(function(){
			if($('#mobilephone').val().length > 10){
				$('#send2Mobile').html('Sending ...');
				$.get("/b2b/contact/sms", { mobile: $('#mobilephone').val(), type: 'sms' },
						function(data){
						  if(data == 'error'){
							alert('Sorry there was an error sending an SMS to your mobile.  Please ensure that you have entered the correct number.');
						  }else{
							$('#send2Mobile').html('Sent!');
						  }
						} 
					);
			}
		})
		
		
	}
}

$(document).ready(function() { 
	Mobile.init();
});

var Dialog = {
	init: function(){
		var name = $("#fullname"),
					email = $("#youremail"),
					message = $("#message"),
					allFields = $([]).add(name).add(email).add(message),
					tips = $("#validateTips");


		$("#dialog-email").dialog({
					bgiframe: true,
					autoOpen: false,
					width: 400,
					modal: true,
					buttons: {
						'Send Message': function() {
							var bValid = true;
							allFields.removeClass('ui-state-error');

							bValid = bValid && Dialog.checkLength(name,"name",3,30);
							bValid = bValid && Dialog.checkLength(email,"email",6,80);
							bValid = bValid && Dialog.checkLength(message,"message",5,1000);

							// bValid = bValid && Dialog.checkRegexp(name,/^[a-z]([0-9a-z_])+$/i,"Please enter your name");
							// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
							bValid = bValid && Dialog.checkRegexp(email,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"Please enter a valid email.  eg. name@company.com");
							// bValid = bValid && Dialog.checkRegexp(message,/^([0-9a-zA-Z])+$/,"Enter your message");

							var me = this;
							if (bValid) {
								$.get("/b2b/company/sendemail", { name: name.val(), email: email.val(), message:message.val(), company_id: $('#email-companyid').val() },
										function(data){
											alert(data);
											
											allFields.val('').removeClass('ui-state-error');
											$(me).dialog('close');
										} 
								);
								
								
							
							}
						},
						Cancel: function() {
							$(this).dialog('close');
						}
					},
					close: function() {
						// allFields.val('').removeClass('ui-state-error');
					}
				});
				
		$('#email-company').click(function(){
			$('#dialog-email').dialog('open');
		})
		
		
		var fwd_name = $("#forward-fullname"),
					fwd_friend = $("#forward-friend"),
					fwd_email = $("#forward-youremail"),
					fwd_message = $("#forward-message"),
					fwd_allFields = $([]).add(fwd_friend).add(fwd_name).add(fwd_email).add(fwd_message),
					fwd_tips = $("#forward-validateTips");
					
		$("#dialog-forward").dialog({
					bgiframe: true,
					autoOpen: false,
					width: 400,
					modal: true,
					buttons: {
						'Send to Friend': function() {
							var bValid = true;
							allFields.removeClass('ui-state-error');

							bValid = bValid && Dialog.checkLength(fwd_friend,"your friends email",6,80)
							bValid = bValid && Dialog.checkLength(fwd_name,"forward-name",3,30);
							bValid = bValid && Dialog.checkLength(fwd_email,"forward-email",6,80);

							bValid = bValid && Dialog.checkRegexp(fwd_email,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"Please enter a valid email.  eg. name@company.com");
							bValid = bValid && Dialog.checkRegexp(fwd_friend,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"Please enter a valid email to send to.  eg. name@company.com");
							

							var me = this;
							if (bValid) {
								$.get("/b2b/company/forwardto", { to: fwd_friend.val(), name: fwd_name.val(), email: fwd_email.val(), message:fwd_message.val(), company_id: $('#email-companyid').val() },
										function(data){
											alert(data);
											
											// fwd_allFields.val('').removeClass('ui-state-error');
											$(me).dialog('close');
										} 
								);
								
								
							
							}
						},
						Cancel: function() {
							$(this).dialog('close');
						}
					},
					close: function() {
						// allFields.val('').removeClass('ui-state-error');
					}
				});
				
		$('#forward-company').click(function(){
			$('#dialog-forward').dialog('open');
		})
		
	},
	 updateTips: function(t) {
				$(".validateTips").text(t).effect("highlight",{},1500);
			},

	checkLength: function(o,n,min,max) {

				if ( o.val().length > max || o.val().length < min ) {
					o.addClass('ui-state-error');
					Dialog.updateTips("Length of " + n + " must be between "+min+" and "+max+".");
					return false;
				} else {
					return true;
				}

			},

	checkRegexp: function(o,regexp,n) {

				if ( !( regexp.test( o.val() ) ) ) {
					o.addClass('ui-state-error');
					Dialog.updateTips(n);
					return false;
				} else {
					return true;
				}

			}
	
}

$(document).ready(function() { 
	Dialog.init();
});

var activeSponsor = 0;
var SponsorsArr = new Array("gold1", "gold2", "gold3", "silver1", "silver2", "silver3", "silver4", "silver5", "silver6", "gold1", "gold2", "gold3", "bronze1", "bronze2", "bronze3", "bronze4", "bronze5", "bronze6");


function changeSponsors(){
	$('#all_'+SponsorsArr[activeSponsor]).fadeOut(500,function(){
		activeSponsor++
		if(activeSponsor==SponsorsArr.length){activeSponsor = 0;}
		
		$('#all_'+SponsorsArr[activeSponsor]).fadeIn(500,null);
	});
}


$(document).ready(function() {
	if($('#allsponsors').size() > 0){
	$(document).everyTime(10000, function(i) {
		changeSponsors();
	});
	}
});

var activeSSponsor = 1;
var activeBSponsor = 1;
function changeSilverSponsors(){
	$('#silver'+activeSSponsor).fadeOut(500,function(){
		activeSSponsor++
		if(activeSSponsor>6){activeSSponsor= 1;}
		$('#silver'+activeSSponsor).fadeIn(500,null);
	});
}

function changeBronzeSponsors(){
	$('#bronze'+activeBSponsor).fadeOut(500,function(){
		activeBSponsor ++
		if(activeBSponsor >6){activeBSponsor = 1;}
		$('#bronze'+activeBSponsor ).fadeIn(500,null);
	});
}

$(document).ready(function() {
	if($('#allsponsors').size() <= 0){
		return;	
		}
	
	$(document).everyTime(15000, function(i) {
		changeSilverSponsors();
	});
	
	$(document).everyTime(15000, function(i) {
		changeBronzeSponsors();
	});
});




